我尝试了很多网上给出的解决方案。我尝试过的解决方案之一来自此链接:Flex yylineno 设置为 1 https://stackoverflow.com/questions/13317634/flex-yylineno-set-to-1
但它们似乎都不适合我生成符号表的代码。
这yylineno
值不会改变。一直显示1
我在输入文件中提供的输入是:
main()
while
varrrr
if
这是我的代码片段:
%%
{pound}{includekey}{openarrow}{alpha}+{closearrow}
{printf("\n %s : Preprocessor Directive at line no: %d!", yytext, yylineno); newfunction(yytext,"Preprocessor",yyleng);}
{mainkey}{openpara}{closepara} {printf("\n %s : Main function found at line no: %d! ", yytext, yylineno); newfunction(yytext,"main",yyleng);}
{alpha}[{underkey}|{alpha}|{digit}]+{openpara}{closepara} {printf("\n %s : Userdefined function without parameters found at line no: %d!", yytext, yylineno);newfunction(yytext, "function",yyleng);}
{conditional} {printf("\n %s : If statement encountered at line no: %d!", yytext, yylineno);newfunction(yytext,"if", yyleng);}
{control} {printf("\n %s : Control statement encountered at line no: %d!", yytext, yylineno);newfunction(yytext,"control", yyleng);}
{datatypes} {printf("\n %s : Datatype found at line no: %d!", yytext, yylineno);newfunction(yytext, "datatype", yyleng);}
{alpha}* {printf("\n %s : Variable found at line no: %d!", yytext, yylineno);newfunction(yytext, "variable", yyleng);}
{operators} {printf("\n Operator %s found at line no: %d!", yytext, yylineno );}
\n { }
. {printf("\n Unexpected character!");}
%%
另外,我说的是 lex,而不是 yacc。虽然类似,但我已经尝试过yylineno 在 yacc 文件中始终具有相同的值 https://stackoverflow.com/questions/16443056/yylineno-has-always-the-same-value-in-yacc-file但该解决方案对我不起作用!