[antlr-interest] Previous rule element reference in a scope

Jim Idle jimi at temporal-wave.com
Wed Apr 13 07:36:03 PDT 2011


You don't need to do that, just create a WITH node and then use a scope in
the tree walker - when you hit a DOT node with no LHS you know there will be
a WITH scope and just take the ID out of that. You will just make your tree
bigger otherwise and anyway what if the with is:

With (fred.y(45*j.callme()))
{
}

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Graham Mer
> Sent: Wednesday, April 13, 2011 2:29 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Previous rule element reference in a scope
>
> Hello list,
>
> I have a grammar that includes a "With" statement like the following:
>
> With anObject
>  .method1()
>  .method2()
>
> which is semantically equivalent to the following fragment:
>
> anObject.method1()
> anObject.method2()
>
>
> During AST construction, I want to distribute 'anObject' to its
> subordinate method calls, so that I get a tree like the following:
>
> ^(CALL anObject.method1)
> ^(CALL anObject.method2)
>
> This is a natural fit for a scope, but I'm not sure how to capture the
> 'anObject' reference in a scope.
>
> If I have a recognizer grammar with the following rules, how do I
> capture the withStatement  ID in a scope, and how do I insert that
> reference into the AST generated by memberCallStatement?
>
> withStatement
>  scope{ // what do I store here to save my ID? }
>  :  'With' ID
>  ;
>
> memberCallStatement
>  :  '.' ID '()'  // how do i insert $scope::ID in my AST?
>  ;
>
> Also, are there any special precautions I need to take to avoid
> inserting loops in the AST, for example, do I need to make a new
> instance of the with object every time I duplicate it in the AST, or
> can I just re-use it? Is it best to use a String or my own Java object,
> or can I capture the thing that the recognizer generates directly?
>
> I wish I could do essentially the following:
>
> withStatement
>  scope{ Object id; }
>  :  'With'! id=ID!
>  ;
>
> memberCallStatement
>  :  '.' ID '()'  -> ^(CALL $withStatement::id '.' $ID)
>  ;
>
> I know that will give ANTLR a sad, but I hope it illustrates what I
> want to produce.
>
>
> Thanks.
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list