[antlr-interest] Bug using scopes, rule parameters, java code gen

Curtis Clauson NOSPAM at TheSnakePitDev.com
Fri Nov 9 01:23:38 PST 2007


The body of scope and the argument to a rule are not actions. You can 
not include target language code like Java in them.

The scope body is a list of named attributes associated with type 
descriptions from the target language and delimited with a ';'.

The argument to a rule is a reference to local or scoped attribute, and 
not target code. So you can refer to the named attribute within the 
scope, but you cannot use target language syntax like a Java method call.

In your example, either your scoped attribute should be a String that 
can be directly referenced in the argument to the confabulate rule, or 
the confabulate rule should take an object as an argument and call the 
toString() method in a contained action.

For example (tested with AntLR v3.0.1)
----------
foo
scope {
     Object x;
}
     : {$foo::x = "Test Object";} bar
     ;

bar
     : confabulate[$foo::x]
     ;

confabulate[Object o]
     : 'y' {System.out.println($o.toString());}
     ;
----------

At least, that's as much as I've been able to figure out in the absence 
of appropriate documentation.

I hope that helps.
-- Curtis


Joseph Gentle wrote:
> If I have a simple parser:
> 
> 
> grammar test;
> 
> foo
> scope
> {
>     Object x;
> }
> : bar;
> 
> bar    :    confabulate[$foo::x.toString()];
> 
> confabulate[String y]:'y';
> 
> 
> ... when it actually calls confabulate there it generates this incorrect 
> code:
> 
>             confabulate(((foo_scope)foo_stack.peek()).x, .toString());
> 
> instead of:
>             confabulate(((foo_scope)foo_stack.peek()).x.toString());
> 
> The problem has something to do with the '.' in the parameter list being 
> parsed incorrectly.
> 
> There's a work-around using temporary variables, but its ugly.



More information about the antlr-interest mailing list