Two things are wrong:
- Precedence of
'%'
is missing, add it to'/' '*'
- The
'(' expression
error handler is ambiguous (in an expression(4*(2+3)+5*7
there are many ways to insert a missing parenthesis) and is in fact in conflict with the normal'(' expression ')'
rule. It is non-trivial to make such a handler work. I would recommend to remove it and rely on built-in yacc error handler.
Simple error handling can be implemented like this:
stmt:
expression { printf("\n valid exprn");}
| error { printf(" Invalid arithmetic Expression\n"); }
;
expression:
'(' expression ')'
| '(' error ')' { printf(" Invalid arithmetic Expression\n"); }
| ... /* all the rest */
You won't need all other error handlers too.