[antlr-interest] Problems with AST construction

Andreas Bartho andreas.bartho at inf.tu-dresden.de
Thu Aug 23 05:47:39 PDT 2007


Hi Loring,

thank you for your answer.
I have tried something similar (see middle part of my original mail), 
unfortunately this will fail when the grammar gets bigger (I have not 
yet identified the real reason for this).

After a good night's sleep I finally managed to find a solution which 
I'd like to share in case anyone runs into similar problems.

The main thought is to inline all recursive rules (invocation, 
increment, decrement in my case). This is ugly, but it works. Even stuff 
like i()++(j--)(i()++(u--))--++++ is parsed correctly.

Cheers
	Andreas


grammar recstatement;
options {output=AST; k=*; backtrack=true;}
	
tokens {
	Invocation;
	SimpleName;
	Increment;
	Decrement;
}
	

primary : invocation | increment | decrement | nonrec ;

nonrec  : simplename ;
	
invocation
	:	(nonrec -> nonrec)
		(
			(
				increment_rest -> ^(Increment $invocation increment_rest)
			|	decrement_rest -> ^(Decrement $invocation decrement_rest)
			)*
			
			invocation_rest -> ^(Invocation $invocation invocation_rest))+
	;	
	
increment
	:	(nonrec -> nonrec)
		(
			(
				invocation_rest -> ^(Invocation $increment invocation_rest)
			|	decrement_rest -> ^(Decrement $increment decrement_rest)	
			)*
			
			increment_rest -> ^(Increment $increment increment_rest))+
	;	

decrement
	:	(nonrec -> nonrec)
		(
			(
				invocation_rest -> ^(Invocation $decrement invocation_rest)
			|	increment_rest -> ^(Increment $decrement increment_rest)	
			)*
			
			decrement_rest -> ^(Decrement $decrement decrement_rest))+
	;	
	

invocation_rest : '(' primary* ')' ;
	
increment_rest : '++' ;
	
decrement_rest : '--' ;		

simplename : Identifier  -> ^(SimpleName Identifier)
	;

Identifier
     :   'a'..'z'*
     ;



More information about the antlr-interest mailing list