How does flex support bison-location exactly?

前端 未结 8 1148
轮回少年
轮回少年 2020-12-24 07:38

I\'m trying to use flex and bison to create a filter, because I want get certain grammar elements from a complex language. My plan is to use flex + bison to recognise the gr

8条回答
  •  没有蜡笔的小新
    2020-12-24 08:09

    I like Shlomi's answer.

    In addition I was looking for updating column location as well. Found http://oreilly.com/linux/excerpts/9780596155971/error-reporting-recovery.html which made more sense after reading Shlomi's answer.

    Unfortunately there is a typo on that page for yylloc. I've simplified it below a bit.

    In your parser add:

    %locations
    

    in your lexer:

    %{
    
    #include "parser.tab.h"
    
    int yycolumn = 1;
    
    #define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno; \
        yylloc.first_column = yycolumn; yylloc.last_column = yycolumn + yyleng - 1; \
        yycolumn += yyleng; \
        yylval.str = strdup(yytext);
    
    %}
    
    %option yylineno
    

    There might be something going on with column location which doesn't strictly keep track of columns but rather just keeps increasing. That's just my ignorance and appologize if it confuses anyone. I'm currently using column to keep a file character count which in my case is more beneficial than column location.

    Hope that helps.

提交回复
热议问题