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

Ted Villalba ted.villalba at gmail.com
Thu Jul 12 00:49:43 PDT 2007


Awesome. Thanks for that.
I had overlooked the whitespace rule, not realizing the implications.
Ignoring whitespace cleaned up my grammar and made stepping through the
debugger easier. Also, the aptly named helper rule certainly makes sense to
me now.

Thanks again for your time here.

Ted

On 7/11/07, Terence Parr <parrt at cs.usfca.edu> wrote:
>
>
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070712/bd59d680/attachment-0001.html 


More information about the antlr-interest mailing list