[antlr-interest] Tree grammar suggestion needed

Ted Villalba ted.villalba at gmail.com
Sun Jul 15 23:29:20 PDT 2007


Hi,

I am trying to write a tree grammar for a language that has a rule "value"
which contains any number of nested "values" . So a value may contain a
value, may contain a value, etc..
A sample AST output tree is attached from ANTLRWorks. I have the parser
recognizing this language fine, but I can't capture this in the tree
grammar.

Any help defining this tree grammar, specifically for this value rule, would
be greatly appreciated.

Probably better to not look at this, but here is one of my many attempts:

value    : ^(VALUE value_){System.out.println("This works well...ALT1
value_: "+ $value_.st);} -> passThru(val={$value_.st})
         | ^(VALUE value ){System.out.println("This template returns null
but the ALT fulfills the tokens required" + $value.st);} ->
passThru(val={"FUBAR"})
         ;

value_   : ^(BOOL_OP terms value) ->
valueTemplate(op={$BOOL_OP.text},terms={$terms.st}, values={$value.st})
            | ^(BOOL_OP a=value b=value)  ->
valueTemplate(op={$BOOL_OP.text},terms={$a.st}, values={$b.st})
          | terms  {System.out.println("The third ALT(terms) of value_ rule:
" + $terms.st);} -> passThru(val={$terms.st})
         ;

Thanks again,
Ted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070715/d09a8a35/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ASTTree.png
Type: image/png
Size: 8276 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20070715/d09a8a35/attachment-0001.png 


More information about the antlr-interest mailing list