I am having the hardest time trying to remove two shift/reduce problems in my grammar. I cant find a solution anywhere. I see that Yacc apparently will prefer the shift, but if so then why won't mine compile? I know one of the issues is the dangling if-else, but the other, I have no idea. I would prefer that Yacc just handle it for me. The error message says 2 shift reduce conflicts
void yyerror (char *s);
#include <stdio.h>
#include <stdlib.h>
int yylex();
extern int yytext[];
extern FILE *yyin;
%}
%start program
%token LTE GTE BEQUALS NOTEQUALS BEGCOMMENT ENDCOMMENT COMMENT GREATER LESS COMMA PLUS SUB MULT DIV EQUAL LP RP LB RB LC RC SEMICOLON INT FLOAT VOID IF WHILE RETURN ID NUM
%nonassoc IFX
%nonassoc ELSE
%%
program : declaration_list { printf("\nACCEPT\n"); };
declaration_list : declaration_list declaration
| declaration
;
declaration : var_declaration
| fun_declaration
;
var_declaration : type_specifier ID SEMICOLON
| type_specifier ID LB NUM RB SEMICOLON
;
type_specifier : INT
| VOID
;
fun_declaration : type_specifier ID LP params RP compound_stmt ;
params : param_list | VOID ;
param_list : param_list COMMA param
| param
;
param : type_specifier ID
| type_specifier ID LB RB
;
compound_stmt : LC local_declarations statement_list RC
;
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 SEMICOLON
| SEMICOLON
;
selection_stmt : ifsubroutine statement
| ifsubroutine statement ELSE
;
ifsubroutine : IF LP expression RP
;
iteration_stmt : whilesubroutine LP expression RP statement
;
whilesubroutine : WHILE ;
return_stmt : RETURN SEMICOLON
| RETURN expression SEMICOLON
;
expression : var EQUAL expression
| simple_expression
;
var : ID
| ID LB expression RB
;
simple_expression : additive_expression relop additive_expression
| additive_expression
;
relop : LTE | LESS | GREATER | GTE | EQUAL| NOTEQUALS ;
additive_expression : additive_expression addop term
| term
;
addop : PLUS
| SUB
;
term : term mulop factor
| factor
;
mulop : MULT
| DIV
;
factor : LP expression RP
| var
| call
| NUM
;
call : ID LP args RP
;
args : arg_list | /* empty */ ;
arg_list : arg_list COMMA expression
| expression
;
%%
int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
if (!yyin)
{
printf("no file\n");
exit(0);
}
yyparse();
}
void yyerror(char *s)
{
printf("\nREJECT\n");
// printf("error from yyerror\n");
exit(0);
}
int yywrap()
{
// printf("in yywarp\n");
exit(0);
}```