在编写表达式语法后,我遇到了 3 个移位/归约冲突。数学运算表达式很好,但是当我编写“TOKEN_MINUS 表达式 %prec UMINUS”语句时,存在 3 个 shift.reduce 冲突。我不知道如何解决这个问题。请帮助我。
%{
#define YYDEBUG 1
#include <stdio.h>
int yylex();
extern int yylineno;
extern int yyerror (const char *);
%}
%debug
%locations
%define parse.error detailed
%token TOKEN_NUMBER
%token TOKEN_THEN
%token TOKEN_END
%token TOKEN_STOP
%token TOKEN_DO
%token TOKEN_IDENT
%token TOKEN_READ
%token TOKEN_world
%token TOKEN_NEWLINE
%token TOKEN_IF
%token TOKEN_ELSEIF
%token TOKEN_WHILE
%token TOKEN_POWER TOKEN_MULTIPLY
%token TOKEN_PROGRAM
%token TOKEN_ELSE
%token TOKEN_MINUS
%token TOKEN_WRITE
%token TOKEN_ENDIF
%token TOKEN_ENDWHILE
%token TOKEN_FULL_STOP
%token TOKEN_EXCLAMATION_POINT
%token TOKEN_COMMA
%token TOKEN_LEFT_PARENTHESIS
%token TOKEN_RIGHT_PARENTHESIS
%token TOKEN_COLON
%token TOKEN_ASSIGNMENT TOKEN_DOUBLE_ASSIGNMENT
%token TOKEN_DIV
%token TOKEN_GREATER_THAN
%token TOKEN_LESS_THAN
%token TOKEN_STRING
%token TOKEN_STRINGS
%token TOKEN_ADD
%token TOKEN_REAL
%token TOKEN_GT
%left TOKEN_ADD
%left TOKEN_MINUS
%left TOKEN_MULTIPLY
%left TOKEN_DIV
%right UMINUS
%right TOKEN_POWER
%%
program:
statements
;
statements:
statements statement
| statement
;
statement:
simple_stmts
;
simple_stmts:
simple_stmt TOKEN_NEWLINE
| simple_stmt
;
simple_stmt:
expression
| write_stmt
| read_stmt
| assignment
;
write_stmt:
TOKEN_WRITE atom
;
read_stmt:
TOKEN_READ expression
;
assignment:
TOKEN_IDENT TOKEN_ASSIGNMENT expression
;
expression:
expression TOKEN_ADD expression
| expression TOKEN_MINUS expression
| expression TOKEN_MULTIPLY expression
| expression TOKEN_DIV expression
| expression TOKEN_POWER expression
| TOKEN_MINUS expression %prec UMINUS
| atom TOKEN_COMMA
| atom
;
atom:
TOKEN_IDENT
| TOKEN_STRINGS
| TOKEN_PROGRAM
| TOKEN_STOP
| TOKEN_REAL
| TOKEN_END
| TOKEN_NUMBER
| TOKEN_LEFT_PARENTHESIS expression TOKEN_RIGHT_PARENTHESIS
;
%%
int yyerror( const char *s ) {
fprintf(stderr, "Error at line %d: %s\n", yylineno, s);
return 1;
}
当我从代码中删除“TOKEN_MINUS 表达式 %prec UMINUS”时,冲突就会消失,但我相信我需要这个语句。
很抱歉遇到您的问题,但我不知道如何解决。 如果您想了解更多有关问题的信息,请使用
-Wcounterexamples
运行您的野牛,您也许可以自行修复它