[antlr-interest] Reference neseted rewriting result?

John B. Brodie jbb at acm.org
Mon Jul 30 07:43:21 PDT 2012


Greetings!

On 07/30/2012 10:20 AM, Chan David wrote:
> Hi, I created a grammar, there’s a rule looks like below:
>
>
>
> Big_rule: p1=(e=expr -> ^(VR $e1))  p2(s=statement->^(VT $s))  -> ^(ROOT
> $p1 $p2)
>
>
>
> As you see, there are three AST rewritings. ‘p1’ and ‘p2’ are nested result
> of AST rewriting, and they will be referenced in the final rewriting. But
> after code emitted, I found I was wrong, it doesn’t work as my mind. How to
> reference nested rewriting result?
>

I do not think you can refer to nested rewrites individually.

Each (possibly partial) rewrite sets the tree for the entire rule.

So I can think of 3 possibilities:

1) skip the nested rewrites entirely, e.g.:

r : expr statement -> ^(ROOT ^(VR expr) ^(VT statement)) ;

2) use sub-rules for the nesting, e.g:

r : r1 r2 -> ^(ROOT r1 r2) ;
r1 : expr -> ^(VR expr) ;
r2 : statement -> ^(VT statement) ;

3) reference the outer rule when nesting (not sure this really will 
work), e.g.:

r: (expr -> ^(VR expr) (statement -> r ^(VT statement)) -> ^(ROOT r) ;

The above suggestions are just off the top of my head and untested.

Hope this helps!
    -jbb



More information about the antlr-interest mailing list