我正试图摆脱我语法中的其他悬而未决的东西。终端是大写字母。我有:
statement:
expression_statement (not important now )
| compound_statement (not important now )
| selection_statement
| iteration_statement (not important now but maybe yes ? it contains statement non-terminal )
| jump_statement (not important now )
;
selection_statement:
IF LPAR expression RPAR statement
| IF LPAR expression RPAR statement ELSE statement
;
iteration_statement:
WHILE LPAR expression RPAR statement
| DO statement WHILE LPAR expression RPAR SEMIC
| FOR LPAR expression_opt SEMIC expression_opt SEMIC expression_opt RPAR statement
;
我试过这种方法
statement:
expression_statement
| compound_statement
| selection_statement_o
| selection_statement_c
| iteration_statement
| jump_statement
;
selection_statement_o:
IF LPAR expression RPAR statement
| IF LPAR expression RPAR selection_statement_c ELSE selection_statement_o
;
selection_statement_c:
IF LPAR expression RPAR selection_statement_c ELSE selection_statement_c
;
我现在被卡住了,不知道该怎么办。我应该如何在这里正确地移除悬空的 else ?谢谢你的帮助。
编辑1:我尝试了这种方法,我得到了更少的移位/减少冲突和更多的减少/减少冲突我想这是不对的:D但它是
statement:
matched
| unmatched
| other
;
other:
expression_statement
| compound_statement
| jump_statement
;
matched:
selection_statement_c
| iteration_statement_c
;
unmatched:
selection_statement_o
| iteration_statement_o
;
selection_statement_o:
IF LPAR expression RPAR statement
| IF LPAR expression RPAR matched ELSE unmatched
;
selection_statement_c:
IF LPAR expression RPAR matched ELSE matched
;
iteration_statement_o:
WHILE LPAR expression RPAR unmatched
| DO statement WHILE LPAR expression RPAR SEMIC
| FOR LPAR expression_opt SEMIC expression_opt SEMIC expression_opt RPAR unmatched
;
iteration_statement_c:
WHILE LPAR expression RPAR matched
| DO statement WHILE LPAR expression RPAR SEMIC
| FOR LPAR expression_opt SEMIC expression_opt SEMIC expression_opt RPAR matched
;