[antlr-interest] scoped variable not being passed

Thomas Brandon tbrandonau at gmail.com
Mon Sep 3 11:18:37 PDT 2007


You should only declare a scope when you want a rule to create a new
scope object on the stack. So, for dynamic scopes you generally
shouldn't have any scope clauses apart from the initial definitions.
Code like:
selectStatement
scope prog;
	:	'Select' '(' ')' criteria=criteriaBlock? ->
select_statement(crit={criteria != null ? $criteria.st : null},
model={$prog::className})
	;
will cause a new prog scope object to be created on the stack and
removed when the rule exits, so your changes below that method will
all be lost.
If you remove all such scope use declarations I think your code should work.

Tom.

On 9/4/07, Warner Onstine <warnero at gmail.com> wrote:
> Never did hear from anyone on this issue, is this doable? I've tried a
> couple of different things I thought might work but yielded the same
> result.
>
> -warner
>
> On 8/29/07, Warner Onstine <warnero at gmail.com> wrote:
> > Ok, next to last question (I have one nagging grammar generation
> > question left, but want to get this one fixed before I pose it). I
> > have a prog scope variable (prog is one of the first matches which
> > I'll show below), I want to reference this variable in a later match
> > but it isn't being passed along and I'm not sure what I need to do to
> > get it to pass correctly.
> >
> > Here's where it is declared:
> > prog
> > scope {
> >   List methods;
> >   List imports;
> >   String className;
> > }
> > @init {
> >   $prog::methods = new ArrayList();
> >   $prog::imports = new ArrayList();
> >   $prog::className = new String();
> > }
> >         :       packageDecl importsDecl? model ->
> > prog(packageName={$packageDecl.st}, imports={$prog::imports},
> > className={$prog::className}, methods={$prog::methods})
> >         ;
> >
> > The variable I want to reference is $prog::className and I want to
> > reference it in my later statements (which I think are under the
> > method scope), here is where the method scope is declared later.
> >
> > method
> > scope  {
> >   List localVars;
> > }
> > @init {
> >   $method::localVars = new ArrayList();
> > }
> >         :       returnType=type? name=ID '(' p=parameter? ')' '{' methodBody=body? '}'
> >                 -> method(returnType={returnType != null ? $returnType.st : new
> > String("void")},
> >                           name={$name.text},
> >                           param={p != null ? $p.st : new String("")},
> >                           body={methodBody != null ? $method::localVars : null})
> >
> >         ;
> >
> > And I want to use the className variable here:
> >
> > criteriaBlock
> >         :       '{' ID_EQUALS '(' ID ')' '}' -> id_eq(id={$ID.text},
> > model={$prog::className})
> >         ;
> >
> > Again, I've attached the grammar for reference.
> >
> > -warner
> > --
> > Warner Onstine - Programmer/Author
> > New book on Tapestry 4!
> > Tapestry 101 available at
> > http://sourcebeat.com/books/tapestrylive.html
> > warner at warneronstine.com
> > http://warneronstine.com/blog
> >
> >
>
>
> --
> Warner Onstine - Programmer/Author
> New book on Tapestry 4!
> Tapestry 101 available at
> http://sourcebeat.com/books/tapestrylive.html
> warner at warneronstine.com
> http://warneronstine.com/blog
>


More information about the antlr-interest mailing list