[antlr-interest] wildcard in tree grammar
Terence Parr
parrt at cs.usfca.edu
Mon Dec 1 08:43:34 PST 2008
On Nov 30, 2008, at 11:15 PM, Gavin Lambert wrote:
> At 09:29 1/12/2008, Terence Parr wrote:
> >not sure. I guess i left out as it's weird. When would you write:
> >
> >^(. ID). It's always the root that says what kind of thing it is,
> >
> >right?
>
> This is a completely fabricated example, but your tree structure
> might dictate that at that position you know it's either a
> ^(ORDER1FN ID) or a ^(ORDER2FN ID) or a ^(ORDER3FN ID). In one tree
> grammar you care about the distinction, while in another you don't.
'.' is great to avoid a subtree or any random operand node you don't
care about. As a root, it can only be a node so it's pretty easy to
give the set of valid roots if necessary. Operands can be subtrees
and those can be whole grammars; harder to ignore. Remember that AST
construction is all about identifying the ops and pseudo-ops in your
program, then making them roots. Why would you ever want to ingore
what operation your looking at?
Oh, I just found one. You want to find all ID refs...well, actually
that is just:
id : ID ;
If you don't care about the root, just don't give it, right?
x : ^(. ID) ;
is identical to ID except it won't match ID as a root but plain ID
would.
See my drift here? Random root with known children == children.
> Of course you could just write ^((ORDER1FN | ORDER2FN | ORDER3FN)
> ID), but if you know that there will never be any other
> possibilities then it's simpler to just write ^(. ID), especially as
> the number of variations increases.
>
>
> And then of course there's Oliver's example, where he just wanted to
> traverse the (sub)tree regardless of structure (presumably to pretty-
> print it or something).
If you don't care about structure, one could argue why you're using a
grammar to do that ;)
> They might be unusual cases, admittedly, but they seem reasonable.
I'm still unconvinced I'm afraid. ;)
Ter
More information about the antlr-interest
mailing list