3 ms·
You can, yes. However, doing that requires making the newline between the `else` and `if` significant: if c1 then s1 else if c2 then
by munificent 5y ago
You can, yes. However, doing that requires making the newline between the `else` and `if` significant:
if c1 then
s1
else
if c2 then
s2
else
s3
end
end // <-- Two "end"s
Versus:
if c1 then
s1
else if c2 then
s2
end // <-- Only one "end"
If you don't make the newline there significant, then every if statement at the beginning of an else clause would be interpreted as chained:
if c1 then
s1
else
if c2 then
s2
end
s3
end
Here, the parser will incorrectly treat the first "end" as closing the outer "if" when it's intended to close the inner one. It will then think "s3" is outside of the "else" clause and then consider the second "end" to be a parse error.
In principle, this isn't actually ambiguous and if the parser had unbounded lookahead it could probably find the second "end" and rollback and figure out what's going on. In practice, languages with unbounded lookahead are hard for humans to parse too.
Having an explicit "elif" token avoids all of this weirdness.