代码之家  ›  专栏  ›  技术社区  ›  KAction

Bison意外的标识符错误

  •  0
  • KAction  · 技术社区  · 12 年前
    %{
    #include <stdio.h>
    #include <string.h>
    
    void yyerror(const char *str)
    {
            fprintf(stderr,"error: %s\n",str);
    }
    
    int yywrap()
    {
            return 1;
    }
    
    int main()
    {
            yyparse();
    }
    %}
    
    %token TOKMACHINE TOKLOGIN TOKPASSWORD VALUE SPACE NEWLINE
    input: auth input | input;
    delim: SPACE | NEWLINE;
    auth: TOKMACHINE delim VALUE delim TOKLOGIN delim  VALUE delim  TOKPASSWORD delim VALUE delim
    {
        printf("Found auth {%s,%s,%s}", $1,$3,$5);
    };
    

    下面是简单的野牛语法,我想用它来解析 .netrc 文件 但我犯了错误 input 线路:

    netrc.y:23.1-5: syntax error, unexpected identifier:
    

    我是Flex/Bison的新手,但这个例子几乎是从 here

    2 回复  |  直到 12 年前
        1
  •  2
  •   Jens    12 年前

    看起来你错过了 %% 分隔符。做那个

    %}
    %token ...
    
    %%
    input : ...
    
        2
  •  2
  •   twalberg    12 年前

    你需要一个 %% 排在你的后面 %token 行将定义部分与语法部分分隔开。