0

I have this grammar

S-> Stat
Stat -> Exprs WRITE
Stat -> Vars READ
Vars -> ID COMMA Vars
Vars -> ID
Exprs -> Expr COMMA Exprs
Exprs -> Expr
Expr -> ID
Expr -> ...
...

(You can test it here http://jsmachines.sourceforge.net/machines/lalr1.html)

The grammar is ambiguous and it produces S/R conflicts, but the real problem comes out with input ID COMMA ID WRITE meanwhile with ID COMMA ID READ the parser works.

Is there a way to prevent this without change the grammar in CUP?

Alexander
  • 1
  • 1
  • 1
    There's no way to fix it without changing the grammar. But there is a fix without changing the language. It's a bit annoying, though, and it needs more details about `Expr` – rici Sep 30 '20 at 18:59
  • There's a sketch of how to do this sort of thing with an LALR(1) parser at the end of [this answer](https://stackoverflow.com/questions/35248575/eliminating-grammar-ambiguity-when-a-rule-covers-a-subset-of-another/35249521#35249521). – rici Sep 30 '20 at 19:06

0 Answers0