[antlr-interest] The Classic else (Non-)Ambiguity

Scott Stanchfield scott at javadude.com
Tue May 4 06:18:53 PDT 2010


>> The construct
>> itself is ambiguous when if/else doesn't require some kind of block
>> specification (like C/C++/Java).
>
> No, it's really not. Do you understand why Sethi, Aho, and Ullman,
> when writing about this precise grammar situation, say that
> the alternative choice of the (merely technical) ambiguity is
> "surely wrong"?

Sure - the way we normally resolve the ambiguity is (probably) the
only reasonable way to do so. The issue is really who makes that
decision.

The grammar is a tool we use to describe our intention in the
language. The parser generator can only "do what we say" vs "do what
we mean". It's far too easy to make mistakes when writing rules;
grammars are not easy!

>> Some other language could define the semantics to intentionally match
>> the else to the _outermost_ if.
>
> Let's diverge to that non sequitur for a moment. Could you post
> a grammar for such a language?

It makes my head hurt thinking about that possibility ;)
It doesn't matter _how_ such a feature could be implemented; the point
is that it's up to the language designer to make that choice, even if
the choice doesn't feel right. It's their language.

Someone designed APL after all...

> Won't somebody please think of the children?  :-)

heheh... ;)

I prefer to correct children (and explain why they're incorrect) when
they use improper grammar in conversation. It not only helps them on
the sentence they're currently saying, but also helps them understand
the overall grammatical concepts a little better.

[Note that here the "grammar" equivalence is the language of ANTLR or
some other parser generator, not the language they're attempting to
implement using ANTLR or some other parser generator.]

Ciao,
-- Scott


More information about the antlr-interest mailing list