0
假設我的語法中有以下規則。當操作員被分組爲非終端時,野牛移位 - 減少衝突
expr: expr op expr
| NUMBER
op: '+' | '-' | '*' | '/'
和我有以下聲明做
%token NUMBER
%left '+' '-'
%right '*' '/'
這將導致在
State 12
4 expr: expr . op expr
4 | expr op expr .
'+' shift, and go to state 6
'-' shift, and go to state 7
'*' shift, and go to state 8
'/' shift, and go to state 9
'+' [reduce using rule 4 (expr)]
'-' [reduce using rule 4 (expr)]
'*' [reduce using rule 4 (expr)]
'/' [reduce using rule 4 (expr)]
$default reduce using rule 4 (expr)
op go to state 11
4移位,減少衝突。然而,如果我重寫我的語法爲
expr: expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| NUMBER
優先規則k進入並且衝突得到解決。我相信衝突的產生是因爲當野牛遇到op
時,它不會跟蹤被移入堆棧的前op
。有沒有辦法讓所有運營商分組,並仍然解決衝突?
[Yacc/Bison,通過分組數學操作最小化數量]的可能重複(http://stackoverflow.com/questions/1410875/yacc-bison-minimize-amount-by-grouping-math-ops) –