Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

i.e

lvalue: ID {

        printf("<-ID");     
        add_data_to_symbol_table((char*)($1),scope,yylineno);
        printf("lineNO:%d",yylineno);

        }
        ;

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

if(x<=2)
{

    if(t<1)
    {
        k=2;   
    }
}

the lineNO never change,always have 1 as value...

any ideas?

like image 419
Anastasis Avatar asked Jan 13 '23 10:01

Anastasis


1 Answers

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.

like image 91
Bryan Olivier Avatar answered Jan 20 '23 15:01

Bryan Olivier