Bison : Line number included in the error messages
Asked Answered
A

3

5

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.

  • I've gone through the latest Bison documentation, but I seem quite lost...
  • I've also had a look into the %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.
Aboutship answered 14/3, 2014 at 14:24 Comment(2)
Are you using flex to feed bison?Eastnortheast
@Eastnortheast Yes. However, just to let you know, I've found a solution to the above issue based on a "combination" of answers on SO. And I'm currently adding it as an answer myself.Aboutship
A
12

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! :-)

Aboutship answered 14/3, 2014 at 15:3 Comment(4)
You really only need the 3rd bullet point to use yylineno, though the other two things may be useful for other reasons.Didynamous
%locations (combined with %option bison-locations in the lexer file) gives you access to yylloc, which gives you both line and column information, but is a completely independent of yylineno.Didynamous
Might be worth mentioning that yylineo is provided by flex. I.e. it needs external declaration if yyerror is implemented elsewhere. @Chris Dodd: option bison-locations sounds great but it did not work for me. If added it gives me an error at the first rule of the lexer: yylval' is a pointer; did you mean to use '->'. I'm using bison %union fwiw... (flex 2.6.4/bison 3.0.4)Upheave
You also have to add extern int yylineno; in your bison file between %} and %%Nussbaum
M
3

Whats worked for me was adding extern int yylineno in .ypp file:

/* parser.ypp */
%{
    extern int yylineno;
%}

/* scanner.lex */
...
%option yylineno
Mastiff answered 9/5, 2020 at 10:56 Comment(0)
B
3

Bison ships with a number of examples to demonstrate its features, see /usr/local/share/doc/bison/examples on your machine (where the prefix /usr/local depends on your configuration.

These examples in particular might be of interest to you:

  • lexcalc uses precedence directives and location tracking. It uses Flex to generate the scanner.
  • bistromathic demonstrates best practices when using Bison.
    • Its hand-written scanner tracks locations.
    • Its interface is pure.
    • It uses %params to pass user information to the parser and scanner.
    • Its scanner uses the error token to signal lexical errors and enter error recovery.
    • Its interface is "incremental", well suited for interaction: it uses the push-parser API to feed the parser with the incoming tokens.
    • It features an interactive command line with completion based on the parser state, based on yyexpected_tokens.
    • It uses Bison's standard catalog for internationalization of generated messages.
    • It uses a custom syntax error with location, lookahead correction and token internationalization.
    • Error messages quote the source with squiggles that underline the error:
    > 123 456
    1.5-7: syntax error: expected end of file or + or - or * or / or ^ before number
        1 | 123 456
          |     ^~~
    
    • It supports debug traces with semantic values.
    • It uses named references instead of the traditional $1, $2, etc.
Bootery answered 9/5, 2020 at 13:36 Comment(5)
@SeedyRom Well, I wouldn't know about Flex, but the documentation of Bison is carefully written, kept up to date, and meant to be readable and read. Helpful criticism will be most welcome on [email protected].Bootery
I should also mention that there are countless examples of calculators in "Flex/Bison" over the Internet, many of which are badly written, and/or are obsolete and not updated to newer idioms, and/or are actually more about Lex/Yacc. The examples shipped with Bison should not have these defects. If they do, a bug report should suffice to have them fixed.Bootery
Thanks for pointing me towards the examples for bison, this helped me figure out exactly what I was looking for. As well as this answer https://mcmap.net/q/1921435/-what-do-flex-and-bison-need-from-each-other And yeah the calculator examples are honestly just distracting because they're all awkward or older than sin.Anecdotal
One classical way to thank is to upvote the answer ;-)Bootery
Thought I liked this last night! I was in the zone with 100 tabs open lol. Thanks for the help again! I hopefully plan to make a tutorial on my findings and how to properly use flex/bison for reentrant push parsers (no C++). I'll make sure to credit you.Anecdotal

© 2022 - 2024 — McMap. All rights reserved.