[antlr-interest] tree parser rule referencing other parts of the tree

Monty Zukowski monty at codetransform.com
Thu May 20 09:57:30 PDT 2004


I'll ask Ter, but I think he's in NYC right now presenting his paper on 
web template formalisms.  It really was simple.

I found out later that we were basically adding lexical scoping 
(manually).  Usually considered as dangerous as goto programming, I 
think that within the context of a grammar lexical scoping has some 
validity, because the alternative is sending all the info you need up 
and down the stack as arguments.

On to an example.  You have three methods, one to push a variable 
definition on the stack, one to pop the definition off the stack, one 
to get the object so you can assign to it or read it.  Then some rules 
to use them, here's some off the cuff modifications of the example tree 
grammar 
http://www.wumpa.com/antlr/src/example/pre/SourceASTRecognizer.g.txt.  
Let's say we want the "from" rule to be able to know variable names.

select:#(SELECT {pushVariable("variableList",new List());}  //declare 
the variableList
	select_list from)
	{popVariable("variableList");}
;

select_list:#(SELECT_LIST (assignment)+)
;

assignment:#(EQUALS variable column)
;

from:#(FROM {getVariable("variableList");}  //then use it in some 
bizarre way
		(table)+)
;

variable: VARIABLE
;

table: identifier
;

column: identifier
;

identifier: IDENTIFIER
;

OK, that's a dumb example that would be easier with an instance 
variable.  But when you start dealing with nested sub-selects using the 
above approach will make sense.  For instance if we had

table: identifier | LPAREN select RPAREN ;

then you would have proper scoping.

Questions?


On May 20, 2004, at 1:30 AM, Adrian Tineo wrote:

> El Jueves, 20 de Mayo de 2004 00:02, Monty Zukowski escribió:
>> This was a big topic of discussion at the latest cabal -- how to 
>> manage
>> those out of rule references.  Basically we decided to create our own
>> scoped namespace and shove special labels in there pointing to the 
>> tree
>> node.  Easy enough to do manually, in fact Ter did that in half an 
>> hour
>> or so while we sat there.
>
> Is that available? I would like to have a look at that extension.
>
> Thanks
>
> Adrian Tineo
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
Monty Zukowski

ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit -- 
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list