gpt4 book ai didi

yacc - 如何在此yacc文件中查找移位/减少冲突?

转载 作者:行者123 更新时间:2023-12-03 12:39:33 25 4
gpt4 key购买 nike

当我尝试在以下文件上使用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 ;

最佳答案

正如mientefuego指出的那样,语法存在经典的“悬而未决”问题。
您可以通过为导致冲突的规则分配优先级来解决问题。

导致冲突的规则是:

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

首先,通过使ELSE和LOWER_THAN_ELSE(伪 token )不相关:
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE

仅因为首先声明了LOWER_THAN_ELSE,这才赋予ELSE比LOWER_THAN_ELSE更高的优先级。

然后,在冲突规则中,您必须为shift或reduce Action 分配优先级:
selection_stmt : IF '(' expression ')' statement    %prec LOWER_THAN_ELSE ;
| IF '(' expression ')' statement ELSE statement ;

在此,换档具有更高的优先级。我已经合并了上述更正,并在下面列出了完整的语法:
/* 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 ;

关于yacc - 如何在此yacc文件中查找移位/减少冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1737460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com