yylineno has always the same value in yacc file [duplicate]



This question already has an answer here:

for one project in compilers i have one problem in the syntax analyzer,
when i go to add a symbol in a symbol table, i take always the same value in yylineno…

i did this in the begining:


    int yylex(void);
    int yyerror(char* yaccProvidedMessage);    
    extern int yylineno;     //i declare yylineno from the lexical analyzer
    extern char *yytext;
    extern FILE *yyin;       

    int scope=0;  
    int max_scope;

and in the grammar when i go to add something in symbol table:


lvalue: ID {



in the output when i give an input
with different lines it doesnt recognize the new line



the lineNO never change,always have 1 as value…

any ideas?





Assuming you are using yylineno from flex, then you should probably add a line

%option yylineno

to your flex specification. Beware however that it is not advisable to export yylineno directly to your grammar, as your grammar may request look ahead tokens from the tokenizer and thus yylineno may already have been updated. The professed way of handling yylineno is through yylval. I’ve also seen that bison has new line-numbering features (see @1 and @@ etc.) that probably integrates more effortlessly with flex.

P.S: Look me talking about bison, where you only mentioned yacc. If you are committed to yacc, pass it through yylval.


Facebook Comments

Post a comment