[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Interesting ambiguity in "The Complete Syntax of Lua"
- From: Paul Baker <paulbaker8@...>
- Date: Mon, 18 Jul 2016 00:42:39 +1200
I entered "The Complete Syntax of Lua" from the Reference Manual [1]
into a context-free grammar tool, in order to investigate the
well-known "function call x new statement" ambiguity.
To my surprise, the tool found that the grammar was ambiguous even
with the "stat ::= functioncall" production (and hence the above
ambiguity) removed. With some help, I eventually found that the
ambiguity occurs when a "varlist `=´ explist" statement follows an
"exp". Specifically, "exp var" is ambiguous.
An example of an ambiguous "exp var" is "f(g)(h)[i]". Using two
consecutive "varlist `=´ explist" statements, we can have, say:
> function f(g) return g end
> function g(h) return h end
> h = {}
> i = 1
> a = f(g)(h)[i] = 1
According to the grammar in the reference manual, this last line could
be parsed successfully as either "a = f(g); (h)[i] = 1" or "a = f;
(g)(h)[i] = 1". I was interested to know which of these the
semicolon-free line would be equivalent to, but it did not
successfully parse at all. It seems that without a semicolon the
entirety of "f(g)(h)[i]" is parsed as a single expression, leading to
an error:
stdin:1: unexpected symbol near '='
[1] https://www.lua.org/manual/5.1/manual.html#8