本文介绍了野牛的减少没有按预期进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写EPL解析器,所以我正在学习flex和bison.我尝试将其与以下规则(SQL)结合使用:

I'm trying to write a EPL parser,so I'm learning flex and bison.I try using it with following rules(SQL):

SELECT { cout<<"SELECT detected"<<endl;return SELECT; }
FROM { cout<<"FROM detected"<<endl;return FROM;}
[a-zA-Z][0-9a-zA-Z]* { cout<<"IDENTIFIER detected"<<endl;yylval.c=yytext;
                         return IDENTIFIER; }
'$' { return DOL;}
[ \t] { cout<<"space founded:"<<int(yytext[0])<<endl; }
\n { return EOL;}
. {}

和野牛规则是:

sel_stmt : {cout<<"VOID"<<endl;}
         | SELECT identifier_expr FROM identifier_expr {    cout<<"select statement founded"<<endl; }
         ;

identifier_expr : DOL IDENTIFIER {
$$=$2;
cout<<"ident_expr:"<<$$<<endl;
}
;

所有令牌和非终结符的类型均为"char *"

all tokens and non-terminals have type "char*"

因为来自stdin的输入是从$ ddd中选择$ abc"词法分析器在"identifier_expr"操作中返回令牌FROM时发生倒退,输出为"ident_expr:abc来自"为什么会这样?

as the input from stdin is "select $abc from $ddd"reudction happend when lexer returned token FROM,in "identifier_expr" action ,output is"ident_expr:abc from"why this happened?

推荐答案

如果要在flex操作之外使用令牌字符串(yytext),则必须创建的副本. yytext指向的字符串是一个临时值,重新输入词法分析器后将立即对其进行修改.

You must create a copy of the token string (yytext) if you want to use it outside of the flex action. The string pointed to by yytext is a temporary value, and will be modified as soon as the lexer is re-entered.

请参见 bison FAQ flex手册或任何数量的SO问题(由于许多发问者误诊了该问题,因此较难搜索).

See the bison FAQ, the flex manual, or any number of SO questions (which are harder to search for because many of the questioners misdiagnose the problem).

这篇关于野牛的减少没有按预期进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 12:41