So, here I'm a with a step-by-step solution :
- We add the
%locations
directive in our grammar file (between%}
and the first%%
) - We make sure that our lexer file contains an include for our parser (e.g.
#include "mygrammar.tab.h"
), at the top - We add the
%option yylineno
option in our lexer file (between%}
and the first%%
)
And now, in our yyerror
function (which will supposedly be in our lexer file), we may freely use this... yylineno
(= current line in file being processed) :
void yyerror(const char *str)
{
fprintf(stderr,"Error | Line: %d\n%s\n",yylineno,str);
}
Yep. Simple as that! :-)