Bison : Line number included in the error messages

北慕城南 提交于 2020-05-22 07:11:44

问题


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.

回答1:


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




回答2:


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

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

/* scanner.lex */
...
%option yylineno



回答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 the error token to get 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 catalogue for internationalization of generated messages.
    • It uses a custom syntax error with location, lookahead correction and token internationalization.
    • It supports debug traces with semantic values.
    • It uses named references instead of the traditional $1, $2, etc.


来源:https://stackoverflow.com/questions/22407730/bison-line-number-included-in-the-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!