3 ms·
No, `elif`, `elsif`, and `elseif` exist to solve a real grammatical problem. They occur in languages that don't have an explicit block statement with its own de
by munificent 5y ago
No, `elif`, `elsif`, and `elseif` exist to solve a real grammatical problem. They occur in languages that don't have an explicit block statement with its own delimiters and instead allow a sequence of statements directly inside the then and else branches of an if statement, with some terminator like `end` at the end (or dedent in Python).
In those languages, without something like `elif`, you get an endless march to the right when you chain a series of ifs:
if c1 then
s1
else
if c2 then
s2
else
if c3 then
s3
else
if c4 then
s4
else
s5
end
end
end
end
C doesn't need this because it only allows a single statement for the then and else branches, and `{ ... }` is a statement. Therefore, you can chain its directly by having the else statement be an if statement.
- xigoi 5y agoYou could make `else if` a special sequence of tokens that works like `elif`.
- cxr 5y agoWhy are people upvoting this as if it wasn't just explained why that's not true? That's only possible when you have (mandatory) block delimiters. That was the entire basis of the comment—that `elif` is used in "languages that don't have an explicit block statement with its own delimiters".
- xigoi 5y agoHow is it not possible? Here's a simple grammar: if_stmt <- "if" expr INDENT stmts DEDENT ("else" "if" expr INDENT stmts DEDENT)* ("else" INDENT stmts DEDENT)? It does require one-token lookahead, but it's unambiguous.
- deleted 5y ago[deleted]
- cxr 5y agoAre INDENT and DEDENT here describing whitespace sensitivity in the vein of Python, which "uses whitespace to delimit control flow blocks"[1] (elsewhere[2]: "Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks")? We're specifically talking about the subset of languages where these blocks _don't_ have their own delimiters[3]. 1. https://en.wikipedia.org/w/index.php?title=Python_syntax_and https://en.wikipedia.org/w/index.php?title=Python_syntax_and... 2. https://en.wikipedia.org/w/index.php?title=Python_(programmi https://en.wikipedia.org/w/index.php?title=Python_(programmi... 3. There is a reading of Bob's original comment such that it's suggesting that Python and end-if languages are among languages without them (or something—I can't make total sense of that part of the comment; it gets kind of garden path-y), but they're not.
- xigoi 5y agoIt doesn't matter how indentation is parsed. What I wanted to say that you can simply replace "elif" with "else" "if" in the grammar.
- munificent 5y agoYou're right, you could do this in Python because it uses indentation for block delimiting. That means this is unambiguous: if c1 then: s1 else if c2: s2 s3 Without significant indentation, it's not possible to tell if "s3" is indented to be part of the the second if's then branch. In other words, "else if" and "else INDENT if" are unambiguously different in Python. That's not true in other languages that use "elif"/"elsif"/"elseif" where only newlines are significant and not indentation.
- munificent 5y agoYou 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.
- latchkey 5y agoCertainly true. I was more making a bit of a joke about naming things though since that was kind of the OPs original point (which was subtle hinted via the twitter link). Thanks for Dart, absolutely lovely language.