[antlr-interest] Re: Antlr grammar to parse Java classfile?

Andreas Rueckert a_rueckert at gmx.net
Wed Dec 5 12:12:21 PST 2001


On Mit, 05 Dez 2001 Terence Parr wrote:
> Hi Gang,
> Nice discussion of what's best to use and thanks for supporting ANTLR!  
> One thing that you might also consider is simply using Java reflection 
> to pull apart the class files...if I'm not mistaken we built (at jGuru) 
> a search system (no longer with us) for classes/methods and so on.  
> There was some really easy built in Java thing that let it happen.  
> Janne Leppanen is probably still wandering Europe as a gypsy so I can't 
> ask him; oh, maybe I'll dig up that branch of the repository. :)

Yeah, we had this discussion on reflection, too. But I still think, that the
grammar is more readable. You have to consider, that we already have a Java
grammar with a lot of additional code, that do the modelling. A nice Java tree
parser grammar (which wouldn't contain any of the counter stuff, just all the
attributes/methods as nodes) would allow us to reuse most the code. My only
concern is performance at the moment...

> Anyway, concerning the "match n times" thing.  You're right...it would 
> be pretty useful.  What syntax is appropriate and how to do you say 
> 0...n vs 1..n?  Perhaps, for uniformity, we use my "element modifier 
> syntax (e.g., "INT<AST=INTNode>"):
> ids4 : ( ID )+<n=4> ;  // weird looking thing
> ids4 : ( ID )+<4> ; // a little better ("n" would be the default 
> variable)
> ids2opt : ( ID )*<2> ; // 0..2 not 1..2

But in all these cases, you used constants. Many fileformats come a counter,
that defines, how long the following data block is. So you have to parse the
counter 1st, and then match the next rule <counter> times.
> The implementation would be pretty simple I guess.  Just defines a 
> counter like the (...)+ does and generates an error if you don't get n; 
> counter<=n would need to go in the while loop, but should be easy.

Yup! The implementation should be trivial. But since we don't have that feature
yet, I'm trying to come up with something, that works with the current Antlr.
I thought about the semantic predicate again and that might work, too. Like
{int counter=0}
	: counter=blockCount 
	  ( {counter>0}? blockEntry {counter--;} )*
Do you think it would work? So that block entry is matched <counter> times?



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

More information about the antlr-interest mailing list