2017-11-22 12 views
1

この文法は、演算子の優先順位を指定していても競合しています。ドラゴンの本でさえ、そのような方法で解決されました(最初の7行で実装された方法)が、まだ矛盾します!以下 はYACCで実装コードですシフトyaccの算術式の競合を減らします

%right THEN_KW 
%right ELSE_KW 
%left XOR_KW OR_KW 
%right '=' 
%left AND_KW ALSO_KW 
%left EQ_KW LT_KW GT_KW LE_KW GE_KW 
%left PLUS_KW MINUS_KW 
%left MULT_KW DIV_KW MOD_KW 
%right NOT_KW 
arthlogicexpr -> operand | arthlogicexpr arthop arthlogicexpr 

arthop -> '+' | '-' | '*' | '/' |'%' 

operand -> variable 

variable -> IDENTIFIER 

parser.outputでERROは次のとおりです。他の状態についての

state 141 

    78 arthlogicexpr: arthlogicexpr . arthop arthlogicexpr 
    78    | arthlogicexpr arthop arthlogicexpr . 

    '+' shift, and go to state 103 
    '-' shift, and go to state 104 
    '*' shift, and go to state 105 
    '/' shift, and go to state 106 
    '%' shift, and go to state 107 

    '+' [reduce using rule 78 (arthlogicexpr)] 
    '-' [reduce using rule 78 (arthlogicexpr)] 
    '*' [reduce using rule 78 (arthlogicexpr)] 
    '/' [reduce using rule 78 (arthlogicexpr)] 
    '%' [reduce using rule 78 (arthlogicexpr)] 
    $default reduce using rule 78 (arthlogicexpr) 

    arthop go to state 109 

詳細:あなたが警告を回避したい場合は

state 103 

    79 arthop: '+' . 

    $default reduce using rule 79 (arthop) 


state 104 

    80 arthop: '-' . 

    $default reduce using rule 80 (arthop) 


state 105 

    81 arthop: '*' . 

    $default reduce using rule 81 (arthop) 


state 106 

    82 arthop: '/' . 

    $default reduce using rule 82 (arthop) 


state 107 

    83 arthop: '%' . 

    $default reduce using rule 83 (arthop) 
+2

[バイソンシフト/ Cのような言語のためのエラーを減らす](HTTPSの可能性のある重複://のstackoverflowを。 com/questions/19085340/bison-shift-reduce-error-for-cのような言語) – rici

答えて

0

ます演算子の結合性を指定するか、 "arthlogicexpr"が演算子の両側にないように文法を構造化する必要があります。

又は arthlogicexpr( -

はあなたの文法はそれが

arthlogicexpr(C arthlogicexpr(+、B)を、)を意味するかどうかに関して曖昧である入力

a + b - c 

を考えます、+、arthlogicexpr(B、 - 、C))

1

紛争解決が実行される方法のためですが、ちょうど行ったように、あなたは、演算子を考慮することはできません。ルールとトークンの間に優先順位を指定するため、同じ方法で処理してはならないルールの違いを伝える必要があります。そして、exp: exp "+" expexp: exp "*" expに相当するものとして扱いたくありません。

したがって、オペレーターごとに1つずつ、4つのルールを保持してください。

本当にの場合は、優先度レベルごとに1つのルールを定義できますが、実際の付加価値IMHOではこれ以上複雑になりません。

ここでは、優先度の指示文(%rightなど)が役に立たないことを示す適切なツールがあります。これは、(あなたが文法を書いた方法のために)紛争解決がそれらを使用できないというヒントです。私はベンソンが警告します。

また、そこを見ている必要があります

関連する問題