[antlr-interest] treewalker $expression.text?

Martin Kohl martin.kohl at gmail.com
Thu Sep 6 12:22:17 PDT 2007


I don't understand what you mean with a single subtree and multiple
subtrees. As it stands now, my Tree Walker Grammar matches the Parser
Grammar for most of the rules.
A few Parser Rules have been rewritten though. Here is an example containing
a rewritten rule and one that is left alone:

Parser Rule:

tokens {
PLUS;
}
expr
: aRule PlusTok bRule -> ^(PLUS aRule bRule)
| aRule MinusTok bRule
;

Tree Walker:
expr
: ^(PLUS aRule bRule)
| aRule MinusTok bRule
;

Assuming aRule and bRule are equally complex (multiple?) rules, how would
you suggest getting the text individually / how should the grammar be
reformed so as to have a single subtree? Do you mean with single subtree a
rule that only contains one possiblity?
How would you attempt to get the text from the expr in the following rule if
it was added to this grammar?:
stmt
: expr SemiColonToken
;

Thanks for the support,
kind Regards,
Martin


On 9/6/07, Terence Parr <parrt at cs.usfca.edu> wrote:
>
> $text works in tree grammars only if it's got a single root.  A
> limitation (for now).
>
> If eitherRule is a single subtree, should work.
> get text for multiple elements individually.
>
> Ter
> On Sep 6, 2007, at 10:06 AM, Martin Kohl wrote:
>
> > Hello,
> >
> > Why can't I use the form $ruleName.text in my actions in the tree
> > walker grammar? For example:
> >
> > rule :
> >    eitherRule {System.out.println($rule.text);}
> > |  orRule
> > ;
> >
> > works just fine if it's in the normal Parser Grammar, but not if I
> > try to do it in the TreeWalker grammar. Is there any way around
> > this? Also, I notice I can do this with Tokens in the TreeGrammar
> > without problems. So I can get it to print an IDENT, but if there's
> > an expression specifying that IDENT to follow, I can't print that
> > in the TreeWalker.
> >
> > Please help, I like the Tree Walker concept and would hate to have
> > to revert to moving all the actions to the normal Parser Grammar.
> >
> > Thanks!
> > Martin
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070906/07d0c65c/attachment.html 


More information about the antlr-interest mailing list