LEX- yylineno返回1 [英] LEX- yylineno returning 1

查看:405
本文介绍了LEX- yylineno返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试在网上给予了很多的解决方案。我有尝试过的解决方案是从这个链接:的Flex yylineno设置为1

但他们都不来,产生一个符号表的我的code工作。
yylineno 值不会改变。它不断显示1

我在输入文件提供的输入为:

 的main()而varrrr如果


  

这是我的code片断:


  %%{英镑} {includekey} {openarrow} {}的α+ {} closearrow
{printf的(\\ n%S:preprocessor指令在行中没有数:%d!,yytext中,yylineno); newfunction(yytext中,preprocessor,yyleng);}{mainkey} {openpara} {closepara} {printf的(\\ n%S:主要功能在行没有发现:%D,yytext中,yylineno); newfunction(yytext中,主,yyleng);}{字母} [{underkey} | {字母} | {数字}] + {openpara} {closepara} {printf的(\\ n%S:不在行发现没有参数用户自定义功能:%D,yytext中,yylineno) ; newfunction(yytext中,功能,yyleng);}{条件} {printf的(\\ n%S:如果在声明中遇到行不!%D,yytext中,yylineno); newfunction(yytext中,如果,yyleng);}{控制} {printf的(\\ n%S:控制线处遇到任何声明:%D,yytext中,yylineno); newfunction(yytext中,控制,yyleng);}{数据类型} {printf的(\\ n%S:数据类型在行没有发现:%D,yytext中,yylineno); newfunction(yytext中,数据类型,yyleng);}{阿尔法} * {printf的(\\ n%S:可变的线路没有发现数:%d!,yytext中,yylineno); newfunction(yytext中,变,yyleng);}{操作} {printf的(\\ n运算符%s的行处没有发现:%D,yytext中,yylineno);}\\ n {}。 {printf的(\\ n意外字符!);}
%%

另外,我说的法,而不是对yacc条款。虽然类似,我曾尝试<一个href=\"http://stackoverflow.com/questions/16443056/yylineno-has-always-the-same-value-in-yacc-file\">yylineno始终在YACC文件相同的值
但解决的办法并没有对我的工作!


解决方案

其它问题表明,Flex有能力管理 yylineno 通过自动 %选择yylineno 指令。然而,这是在Flex的一个扩展经典莱克斯比较。

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

  \\ n {}

  \\ N {yylineno ++; }


顺便说一句,印刷效果最好换行符在格式字符串的结尾。当换行'打印'缓冲输出通常刷新 - 这样的输出不一定会出现,直到你后,打印的换行符。计划在格式字符串的结尾,除非你有一个不完整的行写换行符。当你需要双空间输出(或您担心别人已经马虎关于结束与新行输出)只需要在开始换行。

I have tried a lot of solutions given online. One of the solutions i have tried is from this link: Flex yylineno set to 1

But none of them seem to work for my code of producing a symbol table. The yylineno value doesn't change. It keeps on showing 1

The input I provided in the input file was:

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!");}


%%

Also, I am talking in terms of lex, not yacc. Although similar, I have tried yylineno has always the same value in yacc file but the solution didn't work for me!

解决方案

Other questions indicate that Flex has the ability to manage yylineno automatically via the %option yylineno directive. That is, however, an extension in Flex compared with classic Lex.

Assuming that you cannot upgrade to Flex, you probably need to replace your rule

\n { }

with

\n { yylineno++; }


As an aside, printing works best with newlines at the end of the format string. The buffered output is normally flushed when the newline is 'printed' — so the output won't necessarily appear until you print the newline after it. Plan to write newlines at the end of the format string unless you have an incomplete line. Newlines at the beginning are only needed when you need to double-space the output (or you are concerned that someone else has been sloppy about ending the output with a newline).

这篇关于LEX- yylineno返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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