ullage
ullage copied to clipboard
Grammar Ambiguity in `while`
Given the following:
var foo: Bool = true
while foo
(print 100)
end
The parse tree will be a function call to foo, rather than a grouping expression in the body of the while loop. To solve this we need some kind of expression separator.
- For
while
we could mandate()
around the condition - For
while
we could have an openingdo
or similar - For all expressions we could use end of line as a separator. Expressions would only parse infix operators over newlines if they're in a grouping expression?
- Some kind of explicit statement separator to fix this in the general case, e.g.
;
in C
In general I'm not a fan of the whitespace solution. I'd rather the language wasn't whitespace sensitive. If we move to a traditional if
/elif
/else
block this will become more pressing as the same ambiguity exists there.
The Ruby grammar appears to solve this problem with a pair grammar non-terminals:
-
T
, which can be either;
, or\n
-
DO
, which is either the literal"do"
orT
.
which means you can write loops like while foo do (100) end
and
while foo
(100)
end