4

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?

Jack
  • 131,802
  • 30
  • 241
  • 343
Anastasis
  • 192
  • 1
  • 2
  • 12

1 Answers1

9

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.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18