yylineno在yacc文件中始终具有相同的值 [英] yylineno has always the same value in yacc file

查看:1451
本文介绍了yylineno在yacc文件中始终具有相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于编译器中的一个项目我在语法分析器中有一个问题,
当我去在符号表中添加一个符号,我总是在yylineno ...相同的值...



我在开始时做到了:

 %{

int yylex(void);
int yyerror(char * yaccProvidedMessage);
extern int yylineno; //我从词法分析器声明yylineno
extern char * yytext;
extern FILE * yyin;

int scope = 0;
int max_scope;
%}

和在语法中,当我去添加符号表中的东西: / p>

ie

  lvalue:ID {

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

}
;

在输出中,当我给输入
不同的行,它不识别新行

  if(x <= 2)
{

if(t <
{
k = 2;
}
}

lineNO从不更改,始终以1为值。

解决方案

假设您使用



yylineno flex ,那么你应该添加一行

  %选项yylineno 

flex 规范。但是请注意,不建议直接将 yylineno 导出到您的语法,因为您的语法可能会请求从令牌器预先查找令牌,因此 yylineno 可能已经更新。处理 yylineno 的自称方式是通过 yylval 。我还看到 bison 有新的行编号功能(见 @ 1



PS:

看看我谈论 bison ,其中你只提到 yacc 。如果您致力于 yacc ,请将其传递到 yylval


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?

解决方案

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.

这篇关于yylineno在yacc文件中始终具有相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆