[antlr-interest] What about grammar inheritance?

brian-l-smith at uiowa.edu brian-l-smith at uiowa.edu
Mon Dec 8 12:47:40 PST 2003


Quoting Terence Parr <parrt at cs.usfca.edu>:

> On Monday, December 8, 2003, at 12:58  AM, Ruslan Zasukhin wrote:
> 
> > on 12/8/03 10:03 AM, Silvain Piree at s.piree at enneya.com wrote:
> >
> >> On the antlr website, the paper "ANTLR 3 Whitepaper"
> >> mentions "dropping inheritance" as one of the features of
> >> ANTLR 3.
> >>
> >> Will there be an alternative grammar re-use mechanism
> >> in ANTLR 3?
> 
> Either inheritance or delegation or something...haven't turned my 
> thoughts to this yet. :)

I don't understand what the "Grammar reuse via RCS concepts" idea is.

I think I mentioned this before, but I'll mention it again. SDF/SDF2 has a
really good grammar module system that is much more flexible than ANTLR's
current grammar inheritence system. Whereas ANTLR's system is similar to subtype
polymorphism (subclassing), SDF's mechanism is more like parametrically
polymorphism (generics). You are encouraged to break up your grammar into
reusable pieces, which you can then assemble in different ways, overriding and
parameterizing rules as necessary. 

For a simple example, let's say you have a module for the lexical syntaxes of a
bunch of languages; e.g. Java, C#, Standard ML, Lisp, CORBA IDL, etc. It is
commonly the case that any new language you create will have a lexical syntax
that is basically a combination of different subsets of these languages. So, it
seems you would want to do something like this (the meta-language is made up):

lexical grammar MyLanguageLexicalGramar is 
begin
    StringLiteral imported from SQL;       -- We love single quotes
    Identifier    imported from StandardML;-- why not allow primes in ID's
    Comment       imported from PLSQL;     -- the ultimate comment marker
end;
    
This is one very useful idea that is basically impossible to implement using
ANTLR's current inheritence system (no multiple inheritence). It is easily done
in SDF2's module system. Also, the rule template mechanism I suggested a long
time ago can be implemented through this module system--each template rule is
just a parameterized (sub-)module.

- Brian



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list