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

Andreas Rueckert a_rueckert at gmx.net
Fri Dec 7 08:32:45 PST 2001


> Actually, I would argue that semantic predicates are the way to go; as
> Monty pointed out
>     ( { n > 0 }? foo { n--; } )*
> will parse at most n iterations and you can add a sem pred as a
> termination check, as Ter suggested.  Recursive definitions also work:       

Ok, I use the following construct now:
// The constant pool.
{ int poolSize=0; }
	: poolSize=u2			             		// Parse the size of the constant pool + 1
	  ( {--poolSize > 0}? cp_info )* {poolSize==0}?  	// Parse <poolSize-1> cp_info structures.
and get the following warning:
     [java] ANTLR Parser Generator   Version 2.7.1   1989-2000 jGuru.com
     [java] ../../modules/classfile/src/org/argouml/uml/reveng/classfile/classfile.g:66: warning: nondeterminism upon
     [java] ../../modules/classfile/src/org/argouml/uml/reveng/classfile/classfile.g:66:        k==1:BYTE
     [java] ../../modules/classfile/src/org/argouml/uml/reveng/classfile/classfile.g:66:        between alt 1 and exit branch of block  
No idea for a workaround so far on my side (but I haven't really tried a lot,
since I was busy hacking the classfile specs in a grammar file).

I just learned, that the 'match n times' might have a few advantages, that have
not yet discussed IIRC.

- It seems, that I cannot assign the result of the n-th match to the n-th
element of an array. At least, I get an error, when I try to do something, like
	: length=u2 { bytes = new byte[length]; }
	  ( {length-- > 0}? bytes[bytepos++]=u1 )* {length==0}? { name = new String(bytes); } 	;  
Workaround is simply to use a buffer for the result.

- The trailing sematic predicate throws a semantic exception, while a
recognition exception makes more sense in my eyes.

- The predicate loop is very readable, if you don't have the generated code in
mind. Maybe some explanation in the Antlr manual would help.



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

More information about the antlr-interest mailing list