Il 17/01/2010 7.18, Tom Stellard ha scritto:
Hi,
I am trying to add line numbers to my parser's error messages. I have
included the options, %locations and %define api.pure in my bison
definition file, but it seems to be generating yyerror with this signature:
yyerror(const char *msg);
instead of the signature I am expecting:
yyerror (YYLTYPE *locp, const char * msg)
I have attached the bison definition file I am trying to use.
Thanks.
-Tom
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison
bison manual reports
(http://www.gnu.org/software/bison/manual/bison.html#Error-Reporting):
if '%locations %define api.pure' is passed then the prototypes for
|yyerror| are:
void yyerror (char const *msg); /* Yacc parsers. */
void yyerror (YYLTYPE *locp, char const *msg); /* GLR parsers. */
so bison generates the right signature (yyerror(const char *msg);),
because you don't specify a GLR parser (don't use GLR parser unless you
really need it).
If you need to track line error, you can access to the line using yylloc
(YYLTYPE struct):
void yyerror (const char *s)
{
fprintf(stderr, "%s line:%d", s,yylloc.first_line);
}
Of course your lexer, for each token matched, has to fill yylloc;
typically using flex you have to define a macro (YY_USER_ACTION) like
the following one:
#define YY_USER_ACTION {yylloc.first_line = yylineno; \
yylloc.first_column = colnum; \
colnum=colnum+yyleng; \
yylloc.last_column=colnum; \
yylloc.last_line = yylineno;}
see flex manual for YY_USER_ACTION:
http://flex.sourceforge.net/manual/Misc-Macros.html#Misc-Macros
and also for yylineno (using %option yylineno flex will manage line
number for you):
http://flex.sourceforge.net/manual/Options-Affecting-Scanner-Behavior.html#Options-Affecting-Scanner-Behavior
The parser generated by bison, for each rule, merges the locations using
YYLLOC_DEFAULT macro.
If you need you can redefine YYLTYPE and YYLLOC_DEFAULT according to
your purposes.
Luca
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison