I am making a simple calculator using flex, bison.
I developed a grammar which includes two type of expressions - integer expressions and real expressions.
The grammar is similar to this:
exp -> intExp | realExp
intExp -> INT | intExp '+' intExp
realExp -> REAL | realExp '+' realExp | intExp '+' realExp | realExp '+' intExp
This is not LALR(1).
For example consider the string INT '+' REAL. At 'INT' the lookahead is '+' and based on just this it is impossible to tell whether the string is an intExp or a realExp.
I tried rewriting the grammar to resolve the ambiguity but nothing came of it.
I know I could defer making computations during parsing and instead build a parse tree. Then with type checking, the issue could be resolved. But that seems a bit too much for such a simple problem.
Is there any way bison itself can be made to handle this ambiguity? Or can the grammar be re-written in a better way?
No, if it's not LALR(1) then it's not. However in your language you cannot have a type mismatch error. Why then have separate productions for int and real expressions? Just make the node value contain an integer, a real and a type code.