Yylineno always has the same value in the yacc file

for one project in compilers, I have one problem in the parser, when I go to add a character to the character table, I always accept the same value in yylineno ...

I did this at the beginning:

%{ 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 grammar, when I want to add something to the character table:

i.e

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

on output, when I give input with different lines it does not recognize a new line

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

string NO never changes, always has value 1 as value ...

any ideas?

+3
source share
1 answer

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

 %option yylineno 

to your flex specification. Beware, however, that it is not practical to export yylineno directly to your grammar, as your grammar may require you to view tokens from the tokenizer and, therefore, yylineno may already be updated. The declared method for handling yylineno is yylval . I also saw that bison has new line numbering features (see @1 and @@ etc.), which may more easily integrate with flex .

PS: Listen, I'm talking about bison , where you mentioned only yacc . If you are committed to yacc , go through yylval .

+6
source

All Articles