How does flex support bison-location exactly?

前端 未结 8 1121
轮回少年
轮回少年 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:12

    I think I managed to make it work ( credit goes to the writer of the bison manual ltcalc lexical analyzer). By default, bison creates yylloc that contains

    { first_line, first_column , last_line , last_column }
    

    We only need to update those values in our lexical analyzer. Ex :

    [ \t]     { ++yylloc.last_column; }
    [\n]      { yyloc.last_column = 0; return EOL; }
    [a-zA-Z]+ { 
                yylloc.last_column += strlen(yytext);
                return IDENTIFIER;
              }
    

    Now in bison, to retrieve those fields:

    statement : IDENTIFIER '=' expression 
                { printf("%d - %d\n", @1.last_line, @1.last_column); }
    

    By default these fields are initialized to one, we should initialize the column fields to zero otherwise they will report the wrong column.

提交回复
热议问题