Consider the Lua programming language, which has the following constructs involving the keywords do and end:

  • do-end
  • while <cond> do-end
  • for <...> do-end

If I were to add do-while <cond>; statements (the semicolon would be optional, but for the sake of simplicity we can assume that it's mandatory to exclude possibly of the part of <cond> being considered a <prefixexp> of the following statement).

I seemingly can't construe any ambiguity, but it looks like this might significantly complicate the parsing: When a do is encountered, it may end with either an end or a while; however a while may also be a nested while-do-end inside the do block.

Which issues would you see with adding do-while given Lua's current use of the while, do and end keywords?

(I'm aware that do-while would be largely redundant with repeat-until; I'm also wondering why the Lua authors preferred repeat-until, and I feel that parsing considerations may have been a factor)


1 Answer 1


The consecutive two-block sequence

do print(1) while math.random() < 0.5
do print(2) end

and the nested-block sequence

   while math.random() < 0.5 do print(2) end

are ambiguous so far, having exactly the same tokens — if there's another end somewhere later on, it can only be the nested blocks, and if there isn't it can only be the consecutive blocks, but that's no way to run a parser.

The line endings are not meaningful for Lua: you can write all of that on one line, or every token on a separate line, and it's no different; I've put them in like this to make it comprehensible here. It is the same as do print(1) while math.random() < 0.5 do print(2) end. If we did assume that the semicolon was mandatory, then when it was present only the consecutive-block case would be possible, but mandatory statement terminators aren't part of Lua's general syntactic package.

It's true that this isn't necessarily an ambiguity problem in this case: you can speculatively take both parses at this point and one will always be eliminated later on, and some parsing approaches inherently do so (including PEGs!). It is likely to be a problem for the reader, however, since they can't tell without reading the rest of the file and counting ends.

With more complex nesting, even that may not be enough, and regardless there is a problem of getting a drastically incorrect but non-erroneous parse because of an accidentally mismatched token. In other languages, these cases might be disambiguated by line endings, but Lua precludes that.

This isn't really a workable extension for Lua, even though in many cases it could be correctly parsed. It's at best hard for the computer to parse, and potentially unachievable for the programmer to parse.

  • $\begingroup$ If you have two copies of the ambiguous partial sequence followed by one end, the program is unambiguous without question. $\endgroup$ Jul 24, 2023 at 19:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .