c - Bison 语法错误 mac

标签 c bison flex-lexer yacc lex

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

运行时的终端输出:

syntax error, unexpected identifier, expecting string 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)); 
        }

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

版本:

bison 3.0.4_1

最佳答案

诊断

我已经通过 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.

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

我在你的代码前加了一行注释,所以我的第13行对应你的第12行,也就是%define行。当我把它改成阅读时

%define "parse.error" "verbose"

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"
             ^^^^^^^^^^^^^

有一个 %error-verbose 指令名义上已弃用,但它仍然可用。正式地,它在 3.0.x 中被替换为 %define parse.error verbose。但是,使用 %error-verbose 可以在 Bison 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。

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

There are many features of Bison’s behavior that can be controlled by assigning the feature a single value. For historical reasons, some such features are assigned values by dedicated directives, such as %start, which assigns the start symbol. However, newer such features are associated with variables, which are assigned by the %define directive:

并继续讨论它们是什么。

如果您设法找到 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 文件,似乎在 Bison 3.0 中添加了 parse.error(而不是在 Bison 2.7 中)——这就是您在 Bison 2.3 中遇到麻烦的原因。 NEWS 文件也为此使用了无引号符号。

关于c - Bison 语法错误 mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50221650/

相关文章:

c++ - 从flex+bison输出AST到main.cpp

bison - 如何在Flex规则中引用lex或解析参数?

c - 如何从未排序的数组中获取最小元素的索引?

c - 二维数组传递

c - getdate.y语法疑惑

regex - 文本消息与数千个正则表达式的高效匹配

c - 有配置文件读/写C代码生成器吗?

c++ - 如何解决 Visual Studio 2015 中的此编译错误?

c - 如何从 main() 访问函数变量

c - 有没有办法直接在函数参数中格式化字符串而不是使用临时字符串?