2010-07-16 17 views
3

我試圖解析這句法:這個Bison代碼中的轉換/減少衝突來自哪裏?

34 + 1 − 8, 32 * 87 + 6/4, 34/8 

我期待地這樣說:

(, (- (+ 34 1) 8) (/ (+ (* 32 87) 6) 4) (/ 34 8)) 

這是BISON代碼:

%token NUMBER 
%token COMMA 
%token OPERATOR 
%left OPERATOR 
%left COMMA 
%% 

term: NUMBER | term op term ; 
op: OPERATOR | COMMA; 
%% 

有是一個問題:

test.y: conflicts: 2 shift/reduce 

我該如何解決它?

+0

您例如輸出輸入不匹配。你錯過了'/ 4' – Novelocrat 2010-07-16 12:57:22

+0

謝謝,只是改正了 – yegor256 2010-07-16 12:59:14

+0

如果我正確記住編譯器課程,術語運算符不是LL1語法。您必須將其轉換爲LL1語法才能使其工作 – Henri 2010-07-16 19:24:35

回答

2

問題是與您的term定義:

term: NUMBER | term op term ; 

當解析此,在每個數字,問題是:我應該讀另一個標記知道如果我有第一或第二種形式。

一個解決辦法是定義:

term: NUMBER reminder; 
reminder: /* empty */ | op term; 

語法,一旦適應,如下所示:

%token NUMBER 
%token COMMA 
%token OPERATOR 
%left OPERATOR 
%left COMMA 
%% 

term: NUMBER reminder; 
reminder: /* empty */ | op term; 
op: OPERATOR | COMMA; 
%% 

編譯沒有與bison (GNU Bison) 2.4.1警告。

+2

此答案有錯誤的診斷。問題實際上是優先聲明沒有效果,因爲操作符已經被簡化爲「op」。由於優先級是靜態的,因此'term op term'生產沒有優先級,並且生產是不明確的。 – rici 2015-12-08 17:54:03

10

要找到衝突的位置,請使用--verbose選項並查看文件example.output,其中輸入文件爲example.y。這是我從你輸入了文件:

State 7 conflicts: 2 shift/reduce 

(略)

state 7 

    2 term: term . op term 
    2  | term op term . 

    COMMA  shift, and go to state 4 
    OPERATOR shift, and go to state 5 

    COMMA  [reduce using rule 2 (term)] 
    OPERATOR [reduce using rule 2 (term)] 
    $default reduce using rule 2 (term) 

    op go to state 6