help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: yyerror() location tracking


From: Luca
Subject: Re: yyerror() location tracking
Date: Sun, 17 Jan 2010 15:15:34 +0100
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.9.1.5) Gecko/20091204 Thunderbird/3.0

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


_______________________________________________
address@hidden 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]