[antlr-interest] treewalker as interpreter

Sinan sinan.karasu at boeing.com
Wed Feb 27 12:53:27 PST 2002


Terence Parr wrote:
> 
> On Sunday, February 24, 2002, at 10:01  PM, marc_schellens wrote:
> 
> > I would like to use a tree-walker as
> > an interpreter.
> > One necessary thing then is, to be able to
> > 'jump' meaning to explicitely set the next
> > AST node to be parsed and jump out of a rule
> > immediately.
> > What would be the best way to do so?
> 
> I would have a instruction "cracker" routine that basically figured out
> what tree fits with the instruction.  Then I'd have just a list of these
> suckers in the tree grammar (i.e., lots and lots of start rules).  Then
> I'd call them individually to parse/handle that kind of element.
> 
> Ter

Go To statement would be something like ( in the treewalker)

gotoStatement
	:	#( "goto"		i:INT_CONST)
		{
			_t=(AST)theContext.getTarget(i.getText());
			_retTree=_t;
			throw new DTGoToException(i.getText());
		}
	;


Then you would have:

codeblock:
		CODEBLOCK (statement)*
;
exception
	catch [DTGoToException ex]{
		_t=_retTree;
	}


Couple of things to watch out for.

   if(x==x)then{
100:  x=x-x+x;
   }

   goto 100;

 is obviously bad.....

 So it is a good idea to create a new scope for each block, so that if
target is not found in the
current scope, then rethrow the goto exception...

 Sinan

 PS: I tried this and it works.....

 

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



More information about the antlr-interest mailing list