[antlr-interest] ANTLR-based Java grammar for javac compiler

Jim Idle jimi at temporal-wave.com
Thu Nov 13 15:57:56 PST 2008


On Thu, 2008-11-13 at 22:38 +0100, Simon wrote:
> You could have written a custom TreeAdaptor to build the javac tree.  
> Or was that simply not possible / too tedious?


At least one of the problems is (and this is a common one), that the
javac tree building code expects all the nodes that are children of the
node you are building to be supplied at once. There is no "add this
child" operation.

Further, when you want to build an AST that is useful for netbeans or
similar, then at error recovery time you want to build whatever you have
managed to gather as an AST; the ANTLR tree building currently makes
this quite difficult. Hence, in this case at least, it is easier to use
the existing AST building mechanism, as Yang has done.

If this grammar is to become the definitive Java grammar, it will have
to be a non-backtracking grammar and the proposed operator precedence
changes would be needed. Otherwise it is not possible to compete with
the speed of the current Java lexer/parser. 

Specific error recovery rules, (already discussed/formulated as a
syntax) will be really good (for everyone in fact - 'if you fail here,
see if any of these rules would match') too. The problem with trying to
explicitly code them is that they become part of the valid grammar and
of course introduce ambiguties. The idea here is that you say:

instr: ADD REG COMMA REG

And then adorn this rule with a set of error recognition rules such as:

ID REG COMMA REG { generate special error message}

And so on. Ter already has a tentative syntax for doing this.

The good news is that there is lots of great work in this grammar and I
see no reason it can't be modifed to drop backtracking and incorporate
the proposed operator parsing for expressions (that basically avoids
descending 22 methods to find '666'.)

Jim

> 
> On Nov 13, 2008, at 19:23 , Yang Jiang wrote:
> 
> > That is because we are talking about two kinds of tree here.
> > The one build by Antlr's tree construction mechanisms is different  
> > than the one needed by javac.
> >
> > Of course you could build an Antlr tree, but that's how far a  
> > grammar can go. But if you want to
> > take advantages of javac's code validation, code generation etc.,  
> > you have to build a javac tree
> > to hook into the javac's system.
> >
> > Yang
> >
> > Simon wrote:
> >> Why do they not use the tree construction mechanisms available in   
> >> Antlr? As far as I can tell, they build the tree by using custom   
> >> actions. The link to the full grammar is shown below:
> >>
> >> http://hg.openjdk.java.net/compiler-grammar/compiler-grammar/langtools/file/e37d7d5df672/src/share/classes/com/sun/tools/javac/antlr/Java.g
> >>
> >> When would you choose not to use the tree construction mechanisms?
> >>
> >> Simon
> >>
> >> On Nov 10, 2008, at 21:45 , Terence Parr wrote:
> >>
> >>
> >>> http://openjdk.java.net/projects/compiler-grammar/
> >>>
> >>> Direct link to the grammar:
> >>>
> >>> http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g
> >>>
> >>> USF MSCS student Yang Jiang built this as an intern for Sun.
> >>>
> >>> Terence
> >>>
> >>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> >>>
> >>
> >>
> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> >>
> >>
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list