[antlr-interest] python grammar(s)

Johannes Luber jaluber at gmx.de
Thu Apr 3 13:50:35 PDT 2008


alexander krohn schrieb:
>>
> can you explain what you mean by "semantic limitations"?

There is syntax and there is semantics. Syntax means only correct use of 
the grammar, but not necessarily that the semantics are correct, that 
there is meaning being conferred. "The jam flew like carwinkle." is 
correct syntax, but still nonsense.

Other example. In C# you can use modifiers in front of class-, method-, 
etc.-definitions. But while there is a set of known modifiers, only a 
subset is valid for certain definitions and even there is additional 
context involved. Allowing all modifiers at every place would be 
syntactically correct.

Semantically correct, you can use "virtual" on methods, but not on 
classes. Classes can be "protected" or "private" only when being nested 
themselves. Methods can have "override" only, if there is in a base 
class a suitable "virtual" method.

Some of these limitations can be expressed in BNF where you just allow 
only sensible modifiers, removing only the ones which are never used. 
But this imposes semantic meaning on a pure syntactical construct. This 
should be done in a second pass, because then one can check for other 
context-dependent limitations.

> what would the second pass look like? what grammar should it use? a tree 
> grammar maybe?

A tree grammar is probably the right approach. At least I don't know any 
reason why it isn't. I would use the first pass also to create a symbol 
table. While I haven't gotten around to get my own C# compiler to 
include symbol tables yet, my dissertation thesis may be still useful 
for you, at least the bits about compiler design and ANTLR grammar 
tweaking. If you like I can send you one copy.

> i want to do a mostly complete transformation using a heterogenous ast 
> build by using rule arguments and return values. this puts more stuff to 
> the rules than using rewrite rules but the result is really nice.

That goes beyond my own expertise. Sorry. :(

Johannes


More information about the antlr-interest mailing list