野牛YYSTYPE:尝试使用char*

nao*_*ken 5 bison flex-lexer

我需要使用flex和bison来解析一些代码.

YYSTYPE的默认类型是int,即使我从未以这种方式声明它.这是野牛的默认值吗?

传回字符串对我有很大的帮助.我看了这个:如何解决Bison警告"......没有声明类型" 这看起来是一个很好的方法.(我不需要联盟的全部功能,只需要char*部分,但我不妨使用联盟,因为它可能会在以后有用.)

它不适合我.我收到这些错误:

y:111.37-38: $1 of `ConstExpression' has no declared type
y:113.34-35: $1 of `ConstFactor' has no declared type
y:114.35-36: $1 of `ConstFactor' has no declared type
y:119.34-35: $1 of `Type' has no declared type
y:109.23-48: warning: type clash on default action: <str> != <>
y:115.23-27: warning: type clash on default action: <str> != <>
[...more of the same snipped...]
Run Code Online (Sandbox Code Playgroud)

以下是我的y语法文件中的声明:

%union {
     char *str;
 }

%type<str> ConstExpression ConstFactor Type
Run Code Online (Sandbox Code Playgroud)

这是我.l文件中的一行:

[a-zA-Z]+[a-zA-Z0-9]*   { yylval.str = strdup(yytext); return yident;}
Run Code Online (Sandbox Code Playgroud)

我还需要做些什么来解决错误?

Dig*_*oss 6

实际上,我很确定在没有看到语法的情况下我知道什么是错的.

有必要声明终端符号的类型,因为它们可以并且经常返回语义值.想想像有IDNUMBERyyval数据的东西.但yacc没有任何方法可以知道它只是一个'x'或更多,并且规则中的默认操作是:$$ = $1

例如,以下语法通过yacc很好,但尝试从%type下面删除一个或多个符号:

%union {
  char *s;
}

%type <s> r1 r2 'x'

%%

r1: r2;

r2: 'x'   { printf("%s\n", $1); };
Run Code Online (Sandbox Code Playgroud)