I am getting 3 shift/reduce conflicts for this code. Here is my code:
%{
#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
%token TOKEN_SQUARE_ROOT
%token TOKEN_ARRAY
%token TOKEN_READ_STMT
%left TOKEN_ADD TOKEN_MINUS
%left TOKEN_MULTIPLY TOKEN_DIV
%right UMINUS TOKEN_POWER
%start program
%%
program:
statements
;
statements:
statements statement
| statement
;
statement:
simple_stmts
;
simple_stmts:
simple_stmt TOKEN_NEWLINE
| simple_stmt
;
simple_stmt:
expression
| write_stmt
| assignment
| if_stmt
| elseif
| else
| do_loop
;
if_stmt:
TOKEN_IF atom TOKEN_THEN TOKEN_NEWLINE statements TOKEN_ENDIF
;
elseif:
TOKEN_ELSEIF atom TOKEN_THEN TOKEN_NEWLINE
;
else:
TOKEN_ELSE TOKEN_NEWLINE
;
do_loop:
TOKEN_DO atom assignment
;
write_stmt:
TOKEN_WRITE atom
| write_stmt TOKEN_COMMA atom
| TOKEN_WRITE conditions
;
assignment:
expression 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
| TOKEN_SQUARE_ROOT atom
| atom TOKEN_COMMA
| atom
| conditions
;
conditions:
atom TOKEN_LESS_THAN atom
| atom TOKEN_DOUBLE_ASSIGNMENT atom
| TOKEN_ARRAY atom
| conditions TOKEN_GT conditions
;
atom:
TOKEN_IDENT
| TOKEN_STRINGS
| TOKEN_PROGRAM
| TOKEN_STOP
| TOKEN_REAL
| TOKEN_END
| TOKEN_NUMBER
| TOKEN_READ_STMT
| TOKEN_READ
| TOKEN_LEFT_PARENTHESIS expression TOKEN_RIGHT_PARENTHESIS
;
%%
int yyerror( const char *s ) {
fprintf(stderr, "Error at line %d: %s\n", yylineno, s);
return 1;
}
The conflicts appeared when I did the expressions section. Particularly these lines
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
| TOKEN_SQUARE_ROOT atom
I have tried reviewing the left and right part of the math operations in the top but it still shows the conflicts.