ullage icon indicating copy to clipboard operation
ullage copied to clipboard

Grammar Ambiguity in `while`

Open iwillspeak opened this issue 5 years ago • 1 comments

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 opening do 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.

iwillspeak avatar Apr 24 '19 08:04 iwillspeak

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" or T.

which means you can write loops like while foo do (100) end and

while foo
   (100)
end

iwillspeak avatar Apr 24 '19 11:04 iwillspeak