本文介绍了yylineno在yacc文件中始终具有相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于编译器中的一个项目我在语法分析器中有一个问题,当我去在符号表中添加一个符号,我总是在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 { 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.elvalue: ID { printf("<-ID"); add_data_to_symbol_table((char*)($1),scope,yylineno); printf("lineNO:%d",yylineno); } ;in the output when i give an inputwith different lines it doesnt recognize the new lineif(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 yylinenoto 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文件中始终具有相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-17 09:23