[antlr-interest] wildcard in tree grammar
Terence Parr
parrt at cs.usfca.edu
Mon Dec 1 11:37:27 PST 2008
On Dec 1, 2008, at 9:57 AM, Sam Harwell wrote:
> Here's a fictional example of why you might want to distinguish "any
> node or tree" from "any node" in a tree parser.
>
> Input: (3)+(2)
> Resulting Tree: ^(+ ^(expr 3) ^(expr 2))
> Goal: pattern rewrite in a filter tree parser to remove some nodes
> resulting from unnecessary parentheses.
>
> Pattern:
>
> remove_parens_atomicblock
> : ^(binary_op ^(expr left=.) ^(expr right=.))
> -> ^(binary_op $left $right)
> ;
>
> Result: ^(+ 3 2)
>
> This works as long as '.' means "any node", but may fail due to
> order of
> operations if the parenthesized expression is not a primary
> expression.
Yes, you must predicate the -> to ensure operator prec isn't an issue.
But you don't have a '.' in root position here.
>
>
> On a sidenote, the reason I said ^(.) is not currently allowed in a
> tree
> parser earlier is this:
>
> ANTLR.g3 line 534:
>
> tree_
> : TREE_BEGIN^
> treeRoot ( element )+
> RPAREN!
> ;
>
> One possible resolution add a 'WILDCARD_TREE' rule in ANTLR.g3 as
> follows, and add it as an alt to 'terminal'. This clears up both the
> wildcard-as-root issue and the "any node"/"any node or tree"
> ambiguity.
> It would be allowed anywhere in a tree parser except as a tree root.
>
> WILDCARD_TREE : '^.';
Yeah, i'll be looking into it.
Ter
More information about the antlr-interest
mailing list