我写了一个flex和野牛, 我遇到了一个问题,该问题可以通过以下程序进行说明.
I have written a flex and bison, I am facing a problem which is illustrated via the below program.
该程序用于解析由等号(=)分隔的键/值对 我希望我的野牛脚本能将键和值标记化并打印出来.
The program is intended to parse the key-value pairs separated by an equals (=) sign I am hoping that my bison script tokenizes the key and values and prints them.
下面是我的flex程序的代码段
Below is the snippet of my flex program
%{ /* file : kvp.l */ #include <stdio.h> #define YYSTYPE char* #include "kvp.tab.h" %} %% [a-zA-Z0-9][_a-zA-Z0-9]* { yylval=yytext; return IDENTIFIER; } "=" { yylval=yytext; return EQUALS_OP; } . ; /* Do Nothing */ %% int yywrap() { return 1; }下面是Bison程序
%{ /* file : kvp.y */ #include <stdio.h> #include <stdlib.h> /* interface to the lexer */ int yylex(); int yyerror (char const *s); %} %token IDENTIFIER EQUALS_OP %start stmt %% stmt: stmt pair | pair; pair: IDENTIFIER EQUALS_OP IDENTIFIER { printf("YACC : Key = \"%s\"\n", $1); printf("YACC : Equals = \"%s\"\n", $2); printf("YACC : Value = \"%s\"\n", $3); }; %% int yyerror (char const *s) { fprintf (stderr, "Error String = \"%s\"\n", s); } int main( int argc, char* argv[]) { yyparse(); return 0; }我从该程序中得到的结果如下.
The result which I am getting out of this program is as below.
student@debian:~/stack-overflow$ flex kvp.l student@debian:~/stack-overflow$ bison -d --file-prefix=y kvp.y student@debian:~/stack-overflow$ gcc lex.yy.c y.tab.c -o parser student@debian:~/stack-overflow$ ./parser earth=planet YACC : Key = "earth=planet" YACC : Equals = "=planet" YACC : Value = "planet"可以看到正在打印的令牌是不正确的,所以从令牌开始的整个字符串都被打印了. 请告诉我该程序在哪里出错.
As one can see the tokens which are being printed is not right, the entire string beginning from the token is being printed. Please tell me where I am going wrong in this program.
推荐答案问题是您正在保存指向yytext的指针,该指针随后在Flex扫描仪中进行了更新.如果将其更改为将字符串的副本保存在yytext中,则将获得更一致的结果,例如,
The problem is that you are saving a pointer to yytext, which is later updated in the flex scanner. If you change that to save a copy of the string in yytext, you will get more consistent results, e.g.,
yylval = strdup(yytext);而不是
yylval = yytext;进一步阅读:
- 回复:[Flex-help] yytext返回为char **
- 21.3关于yytext和内存的注释
- Re: [Flex-help] yytext return as char*
- 21.3 A Note About yytext And Memory
更多推荐
野牛令牌是字符串的其余部分
发布评论