[antlr-interest] Elusive rewrite rule

Dan th30dan at gmail.com
Sat Apr 12 13:00:54 PDT 2008


Hello everyone.

I'm trying to figure out how to write the rewrite rule for a parser rule
named "functionExpr".  I already have rewrite rules for my other parser
rules.  Here's a simplified version of my grammar:

=======================================================
tokens {
 FUNCTION;
 TAG;
}

functionExpr
 : NAME '(' pathExpr (',' ARG)* ')' -> ??????????
 ;

pathExpr
 : NAME ('/' pathExpr)? -> ^(TAG NAME pathExpr?)
 ;

NAME : (a-z)+ ;
ARG : '\'' (~'\'')* '\'' ;
=======================================================

My requirements dictate that for this input:
 foo(a/b/c, '1', '2')

I need to generate this AST:
 (TAG a (TAG b (TAG c (FUNCTION foo '1' '2'))))

Any ideas what should be the rewrite rule for "functionExpr" to satisfy this
requirement?



The basic problem I see is that I need to attach the FUNCTION subtree to the
'c' TAG subtree, but I haven't finished processing the entire functionExpr
input (i.e. the ARGs) at the time I parse the 'c' TAG subtree.

So is there some way to save a "reference" to the 'c' TAG subtree, so that
when I finish processing the functionExpr ARGs, I can create the FUNCTION
subtree and attach it to the 'c' TAG subtree via the reference, and have the
entire structure (starting with the 'a' TAG subtree at the top) become the
returned subtree of the entire functionExpr rule?

Many thanks,
-Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080412/ecfbe1fb/attachment.html 


More information about the antlr-interest mailing list