I provided a solution to a problem very similar to this one here: Is C#'s lambda expression grammar LALR(1)?. The basic idea was to separate out the ( id )
case from the other two possibilities ( ( expr_not_id )
and ( list_at_least_2_ids )
). Then the decision about how to reduce ( id )
can be deferred until the lookahead token is available (in your case, {
, assuming that that is sufficient).
Unfortunately, while the transformation of expr
into expr_not_id
is pretty straightforward and almost mechanical, it definitely touches a lot of productions. Also, it's somewhat ugly. So it fails to solve the problem you present in the last sentence. I don't actually think that it is possible to transform primary
without touching expr
, but I've been surprised before.
(The other obvious solution, since the grammar is in fact unambiguous, is to use a GLR parser-generator, but I don't believe the parser-generator you are using has that feature.)