如何在这个yacc文件中找到shift/reduce冲突?

neu*_*cer 19 yacc

当我尝试在以下文件上使用yacc时,我得到错误冲突:1 shift/reduce如何找到并修复冲突?

/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%%

program : declaration_list ;

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt ;

params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;
Run Code Online (Sandbox Code Playgroud)

ard*_*srk 20

正如mientefuego指出你的语法有经典的"悬空"问题.您可以通过为导致冲突的规则分配优先级来解决问题.

导致冲突的规则是:

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;
Run Code Online (Sandbox Code Playgroud)

首先使ELSE和LOWER_THAN_ELSE(伪令牌)非关联:

%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
Run Code Online (Sandbox Code Playgroud)

这使ELSE优先于LOWER_THAN_ELSE,因为LOWER_THAN_ELSE是先声明的.

然后在冲突规则中,您必须为shift或reduce操作指定优先级:

selection_stmt : IF '(' expression ')' statement    %prec LOWER_THAN_ELSE ;
               | IF '(' expression ')' statement ELSE statement ;
Run Code Online (Sandbox Code Playgroud)

在这里,优先考虑转移.我已经纳入了上面提到的更正并列出了以下完整的语法:

/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL

%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
%%

program : declaration_list ;

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID ';'
                | type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt ;

params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement    %prec LOWER_THAN_ELSE ;
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;
Run Code Online (Sandbox Code Playgroud)

  • 我为什么要首先使'ELSE和LOWER_THAN_ELSE(一个伪令牌)非关联`? (2认同)

小智 6

也许你应该尝试一下yacc -v <filename>,它会产生细节的输出.

我在这里测试过,你的语法描述在经典的"悬空"问题中失败了.

看看这篇维基百科的文章.


Dig*_*oss 5

咳咳,这个问题的正确答案通常是:什么都不做。

移位/归约冲突预计会出现不明确的语法。它们不是错误,而是冲突。

通过优先选择shift而不是reduce来解决冲突,这恰好解决了规范的悬空else问题。

bison 甚至有一个 %expect n语句,这样当恰好有n 个冲突时,您就不会收到 S/R 冲突警告。