0
需要定義一個switch statement,其中分支的定義由大括號包圍,但對於單語句分支可以省略大括號,如單語句if
塊。如何在Bison中明確定義單語句開關分支?
(白色間隔爲清楚起見)所需的語法的一個例子:
switch
condition1
blah
| condition2 {
blah
blah
}
| condition3
| condition4
switch
nested_condition
blah
我有以下語法,但它具有一個移位/減少衝突:
%token <union_string> T_ID "identifier"
%% // begin rules
program:
statement_list
;
statement_list:
statement_list statement
| empty
;
statement:
T_ID //added for completeness
| "switch" switch_branch_list //the subject of the switch is elided for brevity
;
switch_branch_list:
switch_branch_list "|" switch_branch
| switch_branch
;
switch_branch:
T_ID statement
| T_ID "{" statement_list "}"
;
empty:
;
衝突:
...
State 9
5 statement: "switch" switch_branch_list .
6 switch_branch_list: switch_branch_list . "|" switch_branch
"|" shift, and go to state 13
"|" [reduce using rule 5 (statement)]
$default reduce using rule 5 (statement)
...
如何解決給定語法中的shift/reduce衝突?