2017-04-13 102 views
0

我想制作一个编译器,现在我正在尝试制作解析器。 我得到这个状态的警告: 国家89如何删除移位/减少警告?

62 expr: '(' expr . ')' 
    66  | expr . '+' expr 
    67  | expr . '-' expr 
    68  | expr . '*' expr 
    69  | expr . '/' expr 
    70  | expr . '%' expr 
    74  | expr . '&' expr 
    75  | expr . '|' expr 
    77 cond: expr . 
    78  | '(' expr . ')' 
    82  | expr . '=' expr 
    83  | expr . "<>" expr 
    84  | expr . '<' expr 
    85  | expr . '>' expr 
    86  | expr . ">=" expr 
    87  | expr . "<=" expr 

    "<>" shift, and go to state 91 
    ">=" shift, and go to state 92 
    "<=" shift, and go to state 93 
    '+' shift, and go to state 94 
    '-' shift, and go to state 95 
    '|' shift, and go to state 96 
    '*' shift, and go to state 97 
    '/' shift, and go to state 98 
    '%' shift, and go to state 99 
    '&' shift, and go to state 100 
    '=' shift, and go to state 101 
    '<' shift, and go to state 102 
    '>' shift, and go to state 103 
    ')' shift, and go to state 119 


$default reduce using rule 77 (cond) 


State 119 

    62 expr: '(' expr ')' . 
    78 cond: '(' expr ')' . 

    "and"  reduce using rule 62 (expr) 
    "and"  [reduce using rule 78 (cond)] 
    "or"  reduce using rule 62 (expr) 
    "or"  [reduce using rule 78 (cond)] 
    ':'  reduce using rule 62 (expr) 
    ':'  [reduce using rule 78 (cond)] 
    ')'  reduce using rule 62 (expr) 
    ')'  [reduce using rule 78 (cond)] 
    $default reduce using rule 62 (expr) 

我给这部分语法是:

expr: 
    T_const | 
    T_char_const | 
    l_value | 
    '(' expr ')' | 
    func_call | 
    '+' expr | 
    '-' expr | 
    expr '+' expr | 
    expr '-' expr | 
    expr '*' expr | 
    expr '/' expr | 
    expr '%' expr | 
    T_true | T_false | 
    '!' expr | 
    expr '&' expr | 
    expr '|' expr 
; 

cond: 
    '(' cond ')' | 
    expr | 
    T_not cond | 
    cond T_and cond | 
    cond T_or cond | 
    expr '=' expr | 
    expr T_not_equal expr | 
    expr '<' expr | 
    expr '>' expr | 
    expr T_greater_equal expr | 
    expr T_less_equal expr 
; 

什么这里的问题是,我怎么能可能解决它,我已经定了吗?一些转移/减少问题,但通常我不明白这个问题是什么。 非常感谢您

+0

我在状态89结束时忘了那两条线:''[减少使用规则77(cond)] $默认减少使用规则77(条件) – Nwlis

+1

使用[编辑]编辑您的文章。 –

回答

1

您的问题所引述的语法具有生产:

cond: '(' cond ')' 

但一个输出文件中引用具有生产:

cond: '(' expr ')' 

还有一些其他的差异这就清楚地表明输出文件不是从引用的语法生成的。这使得回答你的问题的任务变得复杂,尽管在两种情况下问题都是一样的。我将输出文件用作答案剩余部分的参考。

既然你也有:

cond: expr 

有在其中cond得出一个括号字符串的任何方面的不确定性。 (所示的冲突为国家119表明,相当清楚。)假设,例如,分析器遇到

not (x) 

x只能(通过l_value)减少到expr,但随后有两种可能性:

not (expr) => not expr [ from expr: (expr) ] 
      => not cond [ from cond: expr ] 
not (expr) => not cond [ from cond: (eχpr) ] 

这种歧义存在于允许使用cond的所有环境中。

将表达式句法上的划分为布尔表达式和非布尔表达式是非常棘手的,并且通常是不必要的。您最终允许将任何表达式用作布尔值(cond: expr),并且很可能允许(或者您的用户期望您允许)将布尔值分配给变量。所以最简单和最常见的解决方案就是说,一个值是一个值,一个表达式是一个表达式,没有特殊的布尔值。

如果您确实需要在语法上将两者分开,您可以在this recent question中找到示例。