[antlr-interest] C Grammar - Symbol Suffix Related..

Ron Burk ronburk at gmail.com
Mon Dec 6 09:25:01 PST 2010


> I have a doubt on the following productions of the C grammar.
What version of C are you shooting for?

> Any ideas why the grammar is so?

Maybe it's just reproducing a specification grammar? For example,
in the C99 spec, section A.2.2, the 6.75 construct is:

direct-declarator:
    identifier
    ( declarator )
    direct-declarator [ type-qualifier-list.opt assignment-expression.opt ]
          ...
    direct-declarator ( parameter-type-list )
    direct-declarator ( identifier-list.opt )

Transforming that to ANTLR would look pretty much like what you
posted. (Ironically, if ANTLR simply automatically performed the
transformations for direct left recursion and left factoring, a shocking
percentage of the C99 language spec would slide through unchanged
as LL(1).)

Not all constraints can (or should) be implemented via grammar
syntax, as the people who worked on the C language knew.
In fact, I speculate that tools like ANTLR, by offering ever-expanding
feature sets, result in less and less readable grammars, as the
everything-looks-like-a-nail syndrome causes people to try to cram
every constraint into the grammar, foregoing more modular,
understandable, and functional divisions of labor.

So, for example, given a construct like:

int foo(int a)(int b);

Visual C++ says:

error C2091: function returns function

A highly readable and to the point error message. I suspect that if
they instead had tried to contort their grammar to reflect every
constraint under the sun, the message would have come out more
like "Unexpected left parenthesis after parameter declaration list."
I know which I prefer (and I also prefer the ability to directly match
a syntactically plausible but semantically erroneous construct so
that there's no need to rely on "error recovery" mechanisms to
guess where to continue the parse).

 IMHO :-).


More information about the antlr-interest mailing list