Advertisement

yacc/bison locations

Started by January 07, 2004 09:24 PM
1 comment, last by RenderTarget 20 years, 10 months ago
So I''m trying to get bison (1.85) to report line numbers for syntax errors. I have flex assigning to yylloc, and the %locations directive in the grammar file, and all that business. I can get the @n values beautifully for non-syntax-errors. But in cases where bison can''t match a rule, it fires off the syntax error message before I can set a line number for it to use (it fires it off before performing the error action in any rule). I even tried it in the YYLLOC_DEFAULT, but that''s performed afterwards too. I guess I need to know what yyerror() needs to access when reporting a syntax error. Is there an internal variable which stores the correct yylloc for the erroneous token? I like pie.
[sub]My spoon is too big.[/sub]
What I did in this situation (if I understand your problem correctly), is use the yylineno variable. In some implementations it is updated automagically. If not then in your flex/lex file just add the rule below to your grammer

''\n'' { yylineno++;}

or something similar and you can then access the line number bison choked on in yyerror.

Hope this helps,
Grunhund
Advertisement
Yeah, that''s pretty much what I needed, thanks! Though I eschewed the yylineno and declared my own iLineCur variable, which I increment myself in my ''\n'' action. It''s slightly more efficient, because with %option yylineno, every action is forced to scan for ''\n'' instead of the one I devote to just that. (In other words, if %option yylineno is active, yytext will be scanned an additional time, to count ''\n'' characters. Since I only have one rule which matches ''\n'', the counting is easier, and I save on the rescan.)

I like pie.
[sub]My spoon is too big.[/sub]

This topic is closed to new replies.

Advertisement