gpt4 book ai didi

c - LEX- yylineno 返回 1

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:23 27 4
gpt4 key购买 nike

我尝试了很多网上给出的解决方案。我尝试过的解决方案之一来自此链接:Flex yylineno set to 1

但它们似乎都不适用于我生成符号表的代码。yylineno 值不会改变。它一直显示 1

我在输入文件中提供的输入是:

main()

while

varrrr

if

This is my code snippet:

%%

{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 has always the same value in yacc file但解决方案对我不起作用!

最佳答案

其他问题表明 Flex 有能力通过 %option yylineno 指令自动管理 yylineno。然而,与经典 Lex 相比,这是 Flex 中的扩展。

假设您无法升级到 Flex,你可能需要更换你的规则

\n { }

\n { yylineno++; }

顺便说一句,在格式字符串末尾使用换行符打印效果最好。当换行符被“打印”时,缓冲输出通常会被刷新——因此输出不一定会出现,直到您在它之后打印换行符。计划在格式字符串的末尾写换行符,除非你有一个不完整的行。仅当您需要双倍行距输出时才需要开头的换行符(或者您担心其他人草率地以换行符结束输出)。

关于c - LEX- yylineno 返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com