2017-02-16 5 views
1

Appelの "Modern Compiler Implementation in ML"の本でTiger Parserを生成するCh3プログラミング演習を行っています。私のtiger.grmファイルはhereです。私が診断しようとしているエラーは、単項演算子と二項マイナス演算子の規則に起因するreduce-reduceの競合です。私はMINUSよりも高い優先順位を持つUNARY定義され、%precを使用して、私のルールで明示的に設定しているML-Yacc Tiger Parserエラーを減らす/減らす

error: state 128: reduce/reduce conflict between rule 48 and rule 46 on OR 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on AND 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on GE 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on GT 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on LE 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on LT 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on NEQ 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on EQ 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on DIVIDE 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on TIMES 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on MINUS 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on PLUS 
error: state 128: reduce/reduce conflict between rule 48 and rule 46 on RPAREN 

state 128: 

    boolean : exp . AND exp 
    boolean : exp . OR exp 
    arithmetic : MINUS exp . (reduce by rule 46) 
    arithmetic : exp . PLUS exp 
    arithmetic : exp . MINUS exp 
    arithmetic : exp MINUS exp . (reduce by rule 48) 
    arithmetic : exp . DIVIDE exp 
    arithmetic : exp . TIMES exp 
    comparison : exp . EQ exp 
    comparison : exp . NEQ exp 
    comparison : exp . GT exp 
    comparison : exp . LT exp 
    comparison : exp . LE exp 
    comparison : exp . GE exp 

:ここでのyaccエラーです。もちろん、いずれかのルールを削除すると、競合はなくなりますが、文法はMINUS記号を誤って解析します。

私はこのエラーを診断できません - ご意見はありますか?

答えて

3

野生の推測:どちらかのルールでexpが空である可能性がありますか?もしそうなら、それはの前のように、expが任意であるところであいまいさを生むでしょう。

2

受け入れられた答え(彼/彼女は正しい)へのフォローアップとして、expepsilonに行くことを許した配列の生産に間違いがありました。ここで

は、コードの問題のビットは、(最後の行を参照)である:ここでは

sequence : LPAREN exp_sequence RPAREN() 
exp_sequence : (*epsilon*)() 
     | exp seq () 

seq : (*epsilon*)    () (*an exp sequence can be empty*) 
    | SEMICOLON exp exp_sequence() (*exps separated by semicolon*) 

は修正されたコードです:

sequence : LPAREN exp_sequence RPAREN() 
exp_sequence : (*epsilon*)() 
      | exp seq () 

seq : (*epsilon*)    () (*an exp sequence can be empty*) 
    | SEMICOLON exp seq() (*exps separated by semicolon*)