GNU Bison 输出错误 "syntax error, unexpected string, expecting ="
GNU Bison outputting error "syntax error, unexpected string, expecting ="
我一直在尝试编译我的 Bison 代码,但我的代码似乎有问题,但我就是无法弄清楚原因或位置。
这是我的 bison 代码,我是 运行 OSX 上的 GNU Bison 2.3。
我收到的错误是:
romans.y:9.9-21: syntax error, unexpected string, expecting =
这是一个错误,我似乎没有在我的 Linux 机器上收到,而是在 OSX 机器上收到
%{
// file created via echo
# include <stdio.h>
# include <stdlib.h>
int yyerror(char *s);
int yylex();
int yyparse();
%}
%output "roman.tab.c"
%token ARABIC_NUMERAL;
%token EOL
%%
calclist: /* nothing */ {}
| calclist arabic_numerals EOL { printf("%d\n", ); }
;
arabic_numerals: ARABIC_NUMERAL
| ARABIC_NUMERAL { $$ = $$ + ; }
;
/* ones:
| ONE {$$ = 1;}
| ONE ONE {$$ = 2;}
| ONE ONE ONE {$$ = 3;}
;
fives:
| FOUR {$$ = 4;}
| FIVE {$$ = 5;}
| FIVE ones { $$ = 5 +;}
;
tens:
| TEN {$$ = 10;}
| TEN TEN { $$ = 20;}
| TEN TEN TEN { $$ = 30;}
| TEN fives { $$ = 10 + }
| NINE { $$ = 9}
;
fifties:
| FIFTY { $$ = 50;}
|
:*/
%%
void yyerror(char *s)
{
printf("error: %s\n", s);
exit(0);
}
int
main()
{
// yydebug = 1;
yyparse();
return 0;
}
我的代码基于教授给我的程序,如下所示。当我尝试自己编译时,我遇到了完全相同的问题。是不是我系统bison版本问题?
%{
# include <stdio.h>
# include <stdlib.h>
void yyerror(char *s);
int yylex();
int yyparse();
%}
%output "brackets.c"
%token OP CP N EOL
%%
calclist: /* nothing */ {}
| calclist expr EOL { printf("Input conforms to grammar\n"); }
;
//expr: N N N { }
//;
expr: OP expr CP
| N
;
%%
void yyerror(char *s)
{
printf("error: %s\n", s);
}
int
main()
{
// yydebug = 1;
yyparse();
return 0;
}
您应该更新您的 bison 版本。在 OS X 上默认出现的那个是古老的并且缺少很多功能。
在该版本(但不是 2.4 或更高版本)中,%output
指令的语法具有等号:
%output="roman.tab.c"
您可以进行更改,但您的文件将无法在您的其他计算机或任何其他人的计算机(例如您学校的计算机)上运行。您还可以在 运行 bison
命令时设置输出文件名:
bison -d -o roman.tab.c roman.y
这避免了对 %output
指令的需要,并且适用于所有版本的 bison。
但总的来说,升级可能是你最好的选择。
请注意,在 macOS 上更新 Bison 可能很棘手。 Xcode 工具链 (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/bison
) 中的默认系统 Bison 在 10.14 Mojave 中为 2.3,正如@rici 解释的那样,它不支持 %output "brackets.c"
语法(它期望 %output="roman.tab.c"
,因此错误消息中的 =
引用)。
为了以既在您的路径中又在您的编译器路径中的方式更新 Bison,您需要在通过 Homebrew 安装后强制对其进行符号链接(Homebrew 特别需要 Java 8 以便安装 Bison):
brew cask install homebrew/cask-versions/adoptopenjdk8 # Homebrew Bison requires Java8
brew install bazel bison flex
# So that the system can find the new brew Bison instead of the old system Bison.
brew link bison --force
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile
export LDFLAGS="-L/usr/local/opt/bison/lib"
我一直在尝试编译我的 Bison 代码,但我的代码似乎有问题,但我就是无法弄清楚原因或位置。
这是我的 bison 代码,我是 运行 OSX 上的 GNU Bison 2.3。 我收到的错误是:
romans.y:9.9-21: syntax error, unexpected string, expecting =
这是一个错误,我似乎没有在我的 Linux 机器上收到,而是在 OSX 机器上收到
%{
// file created via echo
# include <stdio.h>
# include <stdlib.h>
int yyerror(char *s);
int yylex();
int yyparse();
%}
%output "roman.tab.c"
%token ARABIC_NUMERAL;
%token EOL
%%
calclist: /* nothing */ {}
| calclist arabic_numerals EOL { printf("%d\n", ); }
;
arabic_numerals: ARABIC_NUMERAL
| ARABIC_NUMERAL { $$ = $$ + ; }
;
/* ones:
| ONE {$$ = 1;}
| ONE ONE {$$ = 2;}
| ONE ONE ONE {$$ = 3;}
;
fives:
| FOUR {$$ = 4;}
| FIVE {$$ = 5;}
| FIVE ones { $$ = 5 +;}
;
tens:
| TEN {$$ = 10;}
| TEN TEN { $$ = 20;}
| TEN TEN TEN { $$ = 30;}
| TEN fives { $$ = 10 + }
| NINE { $$ = 9}
;
fifties:
| FIFTY { $$ = 50;}
|
:*/
%%
void yyerror(char *s)
{
printf("error: %s\n", s);
exit(0);
}
int
main()
{
// yydebug = 1;
yyparse();
return 0;
}
我的代码基于教授给我的程序,如下所示。当我尝试自己编译时,我遇到了完全相同的问题。是不是我系统bison版本问题?
%{
# include <stdio.h>
# include <stdlib.h>
void yyerror(char *s);
int yylex();
int yyparse();
%}
%output "brackets.c"
%token OP CP N EOL
%%
calclist: /* nothing */ {}
| calclist expr EOL { printf("Input conforms to grammar\n"); }
;
//expr: N N N { }
//;
expr: OP expr CP
| N
;
%%
void yyerror(char *s)
{
printf("error: %s\n", s);
}
int
main()
{
// yydebug = 1;
yyparse();
return 0;
}
您应该更新您的 bison 版本。在 OS X 上默认出现的那个是古老的并且缺少很多功能。
在该版本(但不是 2.4 或更高版本)中,%output
指令的语法具有等号:
%output="roman.tab.c"
您可以进行更改,但您的文件将无法在您的其他计算机或任何其他人的计算机(例如您学校的计算机)上运行。您还可以在 运行 bison
命令时设置输出文件名:
bison -d -o roman.tab.c roman.y
这避免了对 %output
指令的需要,并且适用于所有版本的 bison。
但总的来说,升级可能是你最好的选择。
请注意,在 macOS 上更新 Bison 可能很棘手。 Xcode 工具链 (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/bison
) 中的默认系统 Bison 在 10.14 Mojave 中为 2.3,正如@rici 解释的那样,它不支持 %output "brackets.c"
语法(它期望 %output="roman.tab.c"
,因此错误消息中的 =
引用)。
为了以既在您的路径中又在您的编译器路径中的方式更新 Bison,您需要在通过 Homebrew 安装后强制对其进行符号链接(Homebrew 特别需要 Java 8 以便安装 Bison):
brew cask install homebrew/cask-versions/adoptopenjdk8 # Homebrew Bison requires Java8
brew install bazel bison flex
# So that the system can find the new brew Bison instead of the old system Bison.
brew link bison --force
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile
export LDFLAGS="-L/usr/local/opt/bison/lib"