(数组解析器)令牌解析flex/bison奇怪行为

(数组解析器)令牌解析flex/bison奇怪行为,bison,flex-lexer,Bison,Flex Lexer,我目前正试图从我用Flex和Bison编写的解析器中解析数组 例如,如果我输入“int testarr[90];”,flex解析器将正确匹配数组标识符“testarr”,但当令牌返回给Bison时,相应的令牌(%token ID)将返回字符串“testarr[90]”。这怎么可能 Flex代码的相关部分(regexp规则) 这里是Bison部分,在这里我得到了“testarr[90]”而不是“testarr”作为令牌ID [...other code] %union { int int

我目前正试图从我用Flex和Bison编写的解析器中解析数组

例如,如果我输入“int testarr[90];”,flex解析器将正确匹配数组标识符“testarr”,但当令牌返回给Bison时,相应的令牌(%token ID)将返回字符串“testarr[90]”。这怎么可能

Flex代码的相关部分(regexp规则)

这里是Bison部分,在这里我得到了“testarr[90]”而不是“testarr”作为令牌ID

[...other code]

%union
{
    int intValue;
    double doubleValue;
    char *stringValue;
}

%error-verbose
%token TYPE NUM ID DECIMAL

%type <stringValue> TYPE
%type <doubleValue> DECIMAL
%type <intValue>    NUM
%type <stringValue> ID    
%type <doubleValue> E

[...other code and rules]

// here output from printf is "testarr[90]" instead of "testarr"
ARRAY:      ID '[' NUM ']'   { printf("%s", $<stringValue>1); exit(1); };
[…其他代码]
%联合
{
int值;
双重价值;
char*stringValue;
}
%错误详细
%令牌类型NUM ID DECIMAL
%类型类型
%类型十进制
%类型NUM
%类型ID
%E型
[…其他守则和规则]
//这里printf的输出是“testarr[90]”,而不是“testarr”
数组:ID'['NUM']'{printf(“%s”,$1);出口(1);};

我认为您需要
strcpy
文本
yytext
,因为扫描下一个令牌将使旧指针无效(或者更确切地说,它会将终止的
\0
移过下一个令牌的末尾).

我认为您需要
strcpy
查看
yytext
,因为扫描下一个令牌将使旧指针无效(或者更确切地说,它会将终止的
\0
移过下一个令牌的末尾)。

谢谢您的建议。如前所述,这个问题在这里有一个重复的问题->并且解决方案是正确的,只是在lex部分将
yylval.stringValue=yytext
更正为
yylval.stringValue=strdup(yytext)
,问题已经解决。谢谢你的帮助。谢谢你的建议。如前所述,这个问题在这里有一个重复的问题->并且解决方案是正确的,只是在lex部分将
yylval.stringValue=yytext
更正为
yylval.stringValue=strdup(yytext)
,问题已经解决。谢谢你的帮助。
[...other code]

%union
{
    int intValue;
    double doubleValue;
    char *stringValue;
}

%error-verbose
%token TYPE NUM ID DECIMAL

%type <stringValue> TYPE
%type <doubleValue> DECIMAL
%type <intValue>    NUM
%type <stringValue> ID    
%type <doubleValue> E

[...other code and rules]

// here output from printf is "testarr[90]" instead of "testarr"
ARRAY:      ID '[' NUM ']'   { printf("%s", $<stringValue>1); exit(1); };