I have the following grammar, which is a small subset of "The Complete Syntax of Lua":
chunk -> | chunk stat.
stat -> var `=´ exp.
var -> Name | exp `[´ exp `]´.
exp -> var | exp `(´ exp `)´ | `(´ exp `)´.
According to a context-free grammar tool, this grammar is not LR(1)
and (hence?) not LALR(1)
, SLR(1)
, LR(0)
or LL(1)
.
Is this grammar ambiguous? If so, could you please give an example of an ambiguous sentence? If not, is there a good reason why it is not LR(1)
?
Edit: A slightly simplified grammar which exhibits the same issue:
stat -> exp | exp var.
var -> Name | exp `[´ exp `]´.
exp -> var | exp `(´ exp `)´ | `(´ exp `)´.
Edit 2: Thanks to Michal Bohuslávek for an example of a sentence which is ambiguous with respect to the second grammar. Unfortunately the sentence does not directly lead to an ambiguity with the first grammar.
However, if the first rule of the second grammar were changed to stat -> exp var.
, an ambiguous sentence would lead to an ambiguity with the first grammar. Michal's idea can be extended to generate such a sentence: exp '(' exp ')' '(' exp ')' '[' exp ']'
.
For example this sequence is IMO ambiguous:
exp '(' exp ')' '[' exp ']'
It's either:
exp '[' exp ']'
/ \
exp '(' exp ')'
Or:
exp var
/ \
exp '[' exp ']'
|
'(' exp ')'