You problem stems from the fact that the '(' could be the start of either the first alternative for c2
or the last alternative for atom
. Just for example, given input like ((x+y) > (a+b))
, the first open paren is the beginning of a c2
, but the second is the beginning of an atom
. [edit: And the parser has no indication of which way to go until some arbitrary point later -- for example, it can't know that the first open paren is the beginning of a c2
until it encounters the >
. For example, if that were a *
instead, then both the opening parens would be beginnings of atom
s.]
One possible way to handle it would be to unify the rules for arithmetic and Boolean expressions, so you only have one rule with '(' expression ')
, and the expression
might be arithmetic or Boolean. This often, however, has the side-effect of producing rather loose typing, with relatively free conversion between arithmetic and Boolean expressions (at least at the parser level -- you can then enforce the types as rigidly as you like in the semantics).
Edit: In Pascal, for example, the rules run something like this (simplifying a tiny bit):
expression: simple_expression ( rel_op simple_expression )*
simple_expression: ( '+' | '-')? term ( ('+' | '-' | 'or' ) term )*
term: factor ( ( '/' | '*' | 'div' | 'mod' | 'and') factor )*
factor: constant | variable | function_call | '(' expression ')' | 'not' factor