[antlr-interest] Tree grammar for 'zero or more' rewrite

Terence Parr parrt at cs.usfca.edu
Wed Jul 11 18:46:48 PDT 2007


On Jul 11, 2007, at 4:46 PM, Ted Villalba wrote:

> Ah, so what I want to build is an AST. Something like:
>
> 0 terms:
>
>         =
>        /
>     TAG
>       |
>      AU
>
> 1 term:
>
>         =
>      /       \
>   TAG   VALUE
>     |           |
>    AU     TERMS
>                 |
>           all the kings horses
>
> 2 term
>
>
>            =
>      /          OR
>   TAG    /         \
>     |    VALUE  VALUE
>    AU     |           |
>          TERMS  TERMS
>              |            |
>              |     all the kings men
>   all the kings horses
>
> Is this approaching the information you are looking for in order to  
> help me here?
>

Yep, though i'm not sure your trees are really what you want. ;)

You should tell the parser to ignore whitespace I think to avoid WS  
calls everywhere.  er...i guess not given your WCHAR definition.  Do  
you really mean to match all those char?  Hmm...i'd find a way to  
match this differently lexically, but...for now.

Ok, first rule: is probably to make rule x return trees rooted in X.   
So, terms should return TERMS on top.

terms    : WCHAR+ -> ^(TERMS WCHAR+)
     | QUOTE WCHAR+ QUOTE -> ^(TERMS WCHAR+) // strip QUOTEs
     ;

Same with value.  Here, easiest thing is to make a helper rule:

value : value_ -> ^(VALUE value_) ;

and rename value to value_:

value   : terms ( operator^  terms )*
     | LPAREN! value RPAREN! ( operator^ value)* // i'll ignore as i  
don't know what you want
     ;

Also don't call value recursively in first alt.  That will create the  
wrong associativity for OR...it will do associativity you find with  
exponents.  Now you can use simple ^ operator.  And we have world  
peace....

Does this help?

Ter



More information about the antlr-interest mailing list