生成的野牛解析器的异常行为

时间:2019-05-29 10:57:02

标签: c compiler-construction grammar bison context-free-grammar

我通过Flex / Bison创建了解析器,该解析器在解析过程中意外失败。这是显示问题的简化示例

Lexer.l:

%{
#include "Parser.h"
%}
%option noyywrap nodefault

%%
"foo"               {   return FOO;                          }
"bar"               {   return BAR;                          }
"("                 {   return OP;                           }
")"                 {   return CP;                           }
[ \t\n]+            {   /*    DO NOTHING  */                 }
.                   {   YY_FATAL_ERROR("unknown character"); }
%%

和Parser.y(启用了跟踪和详细信息):

%{
#include <stdio.h>
int yylex();
void yyerror (char const *s);
%}

%debug
%verbose
%error-verbose
%token FOO BAR OP CP

%%
program_expr :   foo_expr bar_expr   {}
;
foo_expr :   /*  NOTHING  */  {}
    |   OP FOO CP           {}
;
bar_expr :   /*  NOTHING  */  {}
    |   OP BAR CP           {}
;
%%

int main(int argc, char** argv)
{
    yydebug = 1;
    yyparse();
    return 0;
}

void yyerror (char const *s) {  fprintf(stderr, "%s\n", s); }

但是,如果我指定类似(bar)的输入,则生成的解析器将失败-在这种情况下,解析树应包含foo表达式,该表达式为空。它报告:

  

开始解析

     

进入状态0

     

读取令牌:下一个令牌是令牌OP()

     

转移令牌操作符()

     

进入状态1

     

读取令牌:下一个令牌是令牌BAR()

     

语法错误,意外的BAR,期望FOO

     

错误:弹出令牌OP()

     

现在堆叠0

     

清理:丢弃超前令牌BAR()

     

现在堆叠0

以下是shift/reduce automata的生成描述中的一段文字:

state 0
    0 $accept: . program_expr $end
    OP  shift, and go to state 1
    OP        [reduce using rule 2 (foo_expr)]
    $default  reduce using rule 2 (foo_expr)
    program_expr  go to state 2
    foo_expr      go to state 3


state 1
    3 foo_expr: OP . FOO CP
    FOO  shift, and go to state 4
state 2
    0 $accept: program_expr . $end
    $end  shift, and go to state 5
state 3
    1 program_expr: foo_expr . bar_expr
    OP  shift, and go to state 6
    $default  reduce using rule 4 (bar_expr)
    bar_expr  go to state 7

但是我无法理解这些状态的含义/语法。我的语法/解析器有什么问题?

2 个答案:

答案 0 :(得分:2)

Bison默认情况下会生成LALR(1)解析器。 LALR(1)代表从左向右解析器向前看1个令牌。

您的语法不是LALR(1)。在OP上,尚不清楚是否需要foo或bar。那是减少/减少冲突。

看这里: https://en.wikipedia.org/wiki/LALR_parser

但是通常Bison可以生成LR解析器。这里至少有一个Wiki条目声称: https://en.wikipedia.org/wiki/GNU_Bison

您的案件是一次“神秘冲突”:https://www.gnu.org/software/bison/manual/html_node/Mysterious-Conflicts.html#Mysterious-Conflicts

答案 1 :(得分:2)

如果您只接受(bar)作为输入,则可以使用以下内容:

program_expr :   foo_expr bar_expr   {}
    |            bar_expr            {}
;    

代替此:

program_expr :   foo_expr bar_expr   {}
;

测试输出:

> echo "(bar)" | ./Parser 
Starting parse
Entering state 0
Reading a token: Next token is token OP ()
Shifting token OP ()
Entering state 1
Reading a token: Next token is token BAR ()
Shifting token BAR ()
Entering state 6
Reading a token: Next token is token CP ()
Shifting token CP ()
Entering state 11
Reducing stack by rule 6 (line 20):
   $1 = token OP ()
   $2 = token BAR ()
   $3 = token CP ()
-> $$ = nterm bar_expr ()
Stack now 0
Entering state 4
Reducing stack by rule 2 (line 14):
   $1 = nterm bar_expr ()
-> $$ = nterm program_expr ()
Stack now 0
Entering state 2
Reading a token: Now at end of input.
....
相关问题