Bison 语法错误 mac

Cod*_*ick 2 c yacc lex bison flex-lexer

我在 Mac 上使用 Bison,但似乎出现以下错误。我似乎无法解决这个问题。知道为什么我从下面显示的代码中收到语法错误吗?

运行时的终端输出:

语法错误、意外标识符、需要字符串 bison -d parser.y

解析器.y

 %{
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #define YYDEBUG 1
    extern FILE * yyin;
    FILE * outputFile;
    int yyerror(const char* s);
    int yylex(void);
    %}

    %define parse.error verbose
    %token PROGRAM BEGIN_WORD END VAR PRINT INTEGER STRING NUM ID

    %%
    start       : PROGRAM pname ';' VAR decList ';' BEGIN_WORD statList END { YYACCEPT; }
                ;
    pname       : ID { fprintf(outputFile, "#include <iostream>\nUsing namespace std;\nint main()\n{\n"); }
                ;
    decList     : dec ':' type
                ;
    dec         : ID ',' dec
                | ID
                ;
    statList    : stat ';'
                | stat ';' statList
                ;
    stat        : print
                | assign
                ;
    print       : PRINT '(' output ')' 
                ;
    output      : STRING ',' ID         
                | ID                        
                ;
    assign      : ID '=' expr
                ;
    expr        : term                      
                | expr '+' term             
                | expr '-' term             
                ;
    term        : term '*' factor           
                | term '/' factor           
                | factor
                ;
    factor      : ID
                | NUM
                | '(' expr ')'      
                ;
    type        : INTEGER
                ;
    %%

    int main (void) 
        {
        yyin=fopen("input.txt","r+");
        if(yyin==NULL)
        {
            return(printf("error: failed to open file\n"));
        }
        else 
        {
        outputFile = fopen("abc13.cpp","w");
            return(yyparse());
        }
        return 0;
        }

    int yyerror(const char * s) 
        { 
        return(fprintf(stderr, "%s\n", s)); 
        }
Run Code Online (Sandbox Code Playgroud)

不确定问题是否是我拥有的 Bison 版本,或者是因为我在 mac 上。

版本:

野牛 3.0.4_1

Jon*_*ler 5

诊断

我已经通过 HomeBrew ( https://brew.sh/ )安装了 Bison 3.0.4-1 ,当我在你的代码上运行它时,我没有收到任何警告或错误。当我使用/usr/bin/bison(Apple 提供的 Bison 2.3)时,我得到:

so-5022-1650.y:13.13-23: syntax error, unexpected identifier, expecting string.
Run Code Online (Sandbox Code Playgroud)

我诊断出您不小心使用了系统提供的 Bison 而不是 Brew 的 Bison 3.0.4(或从任何地方获得的)。检查您的 PATH 设置。

我在您的代码之前添加了一行注释,因此我的第 13 行对应于您的第 12 行,也就是该%define行。当我把它改成阅读时

%define "parse.error" "verbose"
Run Code Online (Sandbox Code Playgroud)

Bison 2.3 编译代码没有错误。因此,它甚至可能起作用。当我用 Bison 3.0.4 编译旧符号时,我收到警告:

so-5022-1650.y:13.13-25: warning: %define variable 'parse.error' requires keyword values [-Wdeprecated]
     %define "parse.error" "verbose"
             ^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

有一个%error-verbose名义上已弃用的指令,但它仍然可用。正式地,它%define parse.error verbose在 3.0.x 中被替换了。但是,使用%error-verboseBison 2.3 和 Bison 3.0.4 时没有警告。

答案的步骤

你希望%define parse.error verbose做什么?

Mac 上的 Bison 相当古老 — bison (GNU Bison) 2.3. 2.x 系列中曾有过 2.7.1 之前的版本,当前版本是 3.0.4。

在 Bison 3.0.4 文档中%define,它指出:

Bison 的许多行为特征都可以通过为特征分配单个值来控制。由于历史原因,某些此类功能由专用指令分配值,例如%start分配开始符号的 。但是,较新的此类功能与变量相关联,这些变量由%define指令分配:

并继续讨论它们是什么。

如果您设法找到 Bison 2.3 文档,%define则不是其中的一部分。(例如,我关于“Flex 和 Bison”的 O'Reilly 电子书没有涵盖它——它描述了 Bison 2.4.1 和 Flex 2.5.35。)

因此,您可能最好获取 Bison 3.0.4 并编译和安装它(或从 Mac 下载站点之一下载它,预先构建)。或者,您需要修改代码以使用旧版本的 Bison,这意味着不使用%define.


浏览 Bison 2.3 的新闻,似乎%define支持某些选项;它们是“2.2 中的新内容”,或者至少在 2.2 的注释中提到%define "global_tokens_and_yystype" "1"了可能性。这与您使用的语法不同。查看 Bison 3.0.4 NEWS 文件,它似乎parse.error是在 Bison 3.0 中添加的(而不是在 Bison 2.7 中)——这就是您在 Bison 2.3 中遇到问题的原因。NEWS 文件也为此使用了无引号符号。