<p>Consider the Lua programming language, which has the following constructs involving the keywords <code>do</code> and <code>end</code>:</p>
<ul>
<li><code>do</code>-<code>end</code></li>
<li><code>while &lt;cond&gt; do</code>-<code>end</code></li>
<li><code>for &lt;...&gt; do</code>-<code>end</code></li>
</ul>
<p>If I were to add <code>do</code>-<code>while &lt;cond&gt;;</code> 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 <code>&lt;cond&gt;</code> being considered a <code>&lt;prefixexp&gt;</code> of the following statement).</p>
<p>I seemingly can't construe any ambiguity, but it looks like this might significantly complicate the parsing: When a <code>do</code> is encountered, it may end with either an <code>end</code> or a <code>while</code>; however a <code>while</code> may also be a nested <code>while</code>-<code>do</code>-<code>end</code> inside the <code>do</code> block.</p>
<p>Which issues would you see with adding <code>do</code>-<code>while</code> given Lua's current use of the <code>while</code>, <code>do</code> and <code>end</code> keywords?</p>
<p>(I'm aware that <code>do</code>-<code>while</code> would be largely redundant with <code>repeat</code>-<code>until</code>; I'm also wondering why the Lua authors preferred <code>repeat</code>-<code>until</code>, and I feel that parsing considerations may have been a factor)</p>

<parsing><loops><while-loop><do-while><lua>
Parsing `do`-`while`, `while`-`do`-`end` and `do`-`end`
<p>The consecutive two-block sequence</p>
<pre class="lang-lua prettyprint-override"><code>do print(1) while math.random() &lt; 0.5
do print(2) end
</code></pre>
<p>and the nested-block sequence</p>
<pre class="lang-lua prettyprint-override"><code>do
   print(1)
   while math.random() &lt; 0.5 do print(2) end
</code></pre>
<p>are ambiguous so far, having <em>exactly</em> the same tokens — if there's another <code>end</code> 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.</p>
<p>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 <code>do print(1) while math.random() &lt; 0.5 do print(2) end</code>. 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.</p>
<p>It's true that this isn't necessarily an ambiguity <em>problem</em> 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 <code>end</code>s.</p>
<p>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.</p>
<p>This isn't really a workable extension for Lua, even though in many cases it <em>could</em> be correctly parsed. It's at best hard for the computer to parse, and potentially unachievable for the programmer to parse.</p>

CC BY-SA 4.0
2023-07-06T10:02:32.757Z
2023-07-06T10:02:32.757Z
Michael Homer
CC BY-SA 4.0
2023-07-24T19:15:27.993Z
If you have two copies of the ambiguous partial sequence followed by one `end`, the program is unambiguous without question.
Criticize SE actions means ban
# Parsing `do`-`while`, `while`-`do`-`end` and `do`-`end`
Posted by: username_0 on 2023-07-06
Consider the Lua programming language, which has the following constructs involving the keywords `do` and `end`:

* `do`-`end`
* `while  do`-`end`
* `for <...> do`-`end`

If I were to add `do`-`while ;` 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  being considered a  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)
**Tags:** parsing loops while-loop do-while lua
---
## 1 answers
### Answer by username_1 on 2023-07-06. Score: 11 | Accepted answer
The consecutive two-block sequence
do print(1) while math.random() < 0.5
do print(2) end

and the nested-block sequence
do
   print(1)
   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.
> **Comments:**
> **username_2** (2023-07-24): If you have two copies of the ambiguous partial sequence followed by one `end`, the program is unambiguous without question.  
---
