野牛令牌是字符串的其余部分 [英] Bison token is rest of the string
问题描述
我写了一个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;
进一步阅读:
- Re: [Flex-help] yytext return as char*
- 21.3 A Note About yytext And Memory
这篇关于野牛令牌是字符串的其余部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!