[antlr-interest] What is $Symbols

Terence Parr parrt at cs.usfca.edu
Thu Jun 21 17:15:26 PDT 2007


On Jun 22, 2007, at 6:10 AM, Paul Keir wrote:

> Hi,
>
> In the ANTLR C Grammar there is a global scope:
>
> scope Symbols { Set types; }
>
> which is updated when new typedefs are discovered during parsing. A  
> reference to $Symbols becomes a reference to a java.util.stack, and  
> I'm often using things like $Symbols.size() and $Symbols[i]::types.
>
> Well there's the background, but could someone be so kind as to  
> tell me what exactly it's a stack of?

It's a stack of objects whose fields are inside the scope def.  Here,  
it's only one.

> I'm passing it to my own java object, and I'd like to iterate over  
> its contents like
>
> public void my_method(Stack sym) {
>            for (Object depth : sym)
>                depth.types.add("hello");   // e.g.
> }

Upon entry to a rule that says "scope Symbols;" you get a new push  
and then pop at end.

> But that doesn't work. The 'for' bit is fine, but the next 'add'  
> line results in a 'cannot find symbol' error. I'd like to get more  
> specific than 'Object', but there's my problem. Can anyone help?

Note that depth is type object.  Java will need a cast.  Look at the  
output code and see what's up.

Ter


More information about the antlr-interest mailing list