OK, so I suppose my question is quite self-explanatory.
I'm currently building a parser in Bison, and I want to make error reporting somewhat better.
Currently, I've set %define parse.error verbose
(which actually gives messages like syntax error, unexpected ***********************, expecting ********************
.
All I want is to add some more information in the error messages, e.g. line number (in input/file/etc)
My current yyerror
(well nothing... unusual... lol) :
void yyerror(const char *str)
{
fprintf(stderr,"\x1B[35mInterpreter : \x1B[37m%s\n",str);
}
P.S.
%locations
directive, which most likely is very close to what I need - however, I still found no complete working example and I'm not sure how this is to be used.So, here I'm a with a step-by-step solution :
%locations
directive in our grammar file (between %}
and the first %%
)#include "mygrammar.tab.h"
), at the top%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! :-)