[antlr-interest] Basic AST tree generation

Eric Scharff eds_2 at yahoo.com
Fri May 5 09:32:31 PDT 2006


I have a grammar with rules like

expr_list : expr ( COMMA! expr )* ;
expr : term ( operator term ) * ;
operator : OP1 | OP2 | OP3 ;
term : identifier ( postfix_operator )? ;
identifier : IDENT ;
postfix_operator :
	( (DOT^ identifier) | LPAREN^ expr_list RPAREN ) (
postfix_operator )? ;

and I would like to use Antlr's automatic tree generation.

If I re-factor the grammar as

	expr : term ( (OP1^ | OP2^ | OP3^) term ) * ;

I get what I would expect, a left-associative parse tree, eg.

	a * b / c ==> (/ (* a b) c)

but with the current grammar, I would just get a linear stream
of tokens:

	a * b / c ==> a * b / c

Furthermore, I want the term rule to be left associative

	a.b.c(x).d  ==> (FIELDREF (FUNCAPPLY (FIELDREF (DOT a b) c)
(ARGS x)) d)
	
Again, by default, antlr generates a linear list.

I'd rather not factor operator into expr (because I use operator
in other places, not just expr), and I'm not sure how to get any
meaningful tree construction from the term rule.

I'm sure this is a basic question, but I just can't get my head
around antlr's tree construction system.

-Eric

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the antlr-interest mailing list