2017-11-22 71 views
1

이 문법은 연산자의 우선 순위를 지정 했음에도 불구하고 충돌을 일으켰습니다. 드래곤 책에서도 그런 방법으로 해결되었지만 (아래의 처음 7 행으로 구현 된 방식) 충돌은 여전히 ​​발생합니다! 다음은 이 yacc를 구현하는 코드입니다Shift 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의 에로입니다 : 다른 상태에 대한

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

가능한 복제 (HTTPS [들소 Shift 키를/C와 같은 언어에 대한 오차를 줄이기] : // 유래합니다. co/questions/19085340/bison-shift-reduce-error-for-c 같은 언어) – rici

답변

0

당신 연산자 연관성을 지정해야하거나 "arthlogicexpr"이 연산자의 양쪽에 있지 않도록 문법을 구조화해야합니다.

입력을 감안

a + b - c 

문법은 그

arthlogicexpr (arthlogicexpr (a +, b) -, c) 수단 여부 모호

또는 arthlogicexpr을 (a, +, arthlogicexpr (b, -, c))

1

충돌 해결 방식이 수행되는 방식 때문에 방금 수행 한 것처럼 연산자를 고려할 수 없습니다. 규칙과 토큰간에 우선 순위를 지정하므로 동일한 방식으로 처리하면 안되는 규칙 간의 차이점을 설명해야합니다. 그리고 exp: exp "+" expexp: exp "*" exp과 동일하게 취급하고 싶지 않습니다.

각 연산자에 대해 하나씩 네 가지 규칙을 유지하십시오.

실제로에 우선 순위 수준마다 하나의 규칙을 정의 할 수 있지만 무언가를 고려하고 싶다면 실제 부가 가치가없는 IMHO에는 더 복잡 할 것입니다.

우선 순위 지시문 (%right 등)이 여기에 쓸모가 없다는 것을 알려주는 도구가 필요합니다. 그것은 분쟁 해결에서 사용할 수없는 힌트입니다 (문법을 쓰는 방식 때문에). 벤슨은 경고했다.

또한이 모습이 있어야합니다의