[antlr-interest] TokenStreamRewriteEngine: possible to use non-terminals instead of tokens?

Terence Parr parrt at cs.usfca.edu
Wed Feb 4 13:41:06 PST 2004


On Feb 4, 2004, at 1:31 PM, SainTiss wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I must be misunderstanding this, because as far as I can see, this 
> won't work
> in general...
> Having a stack will work if there's a rule which calls itself directly 
> or
> indirectly.

The stack will handle the recursion properly.

> But rules called by this rule might also be calling themselves, which 
> would
> reintroduce the problem that the top of stack is being overwritten.
> Additionally (supposing I'm trying to delete an identifier), there may 
> be
> subsequent identifiers, which are at the same nesting level, and so 
> the top
> of stack will again be overwritten...
> Trivial case:
> myRule: identifier identifier otherRule;
> Suppose I'm trying to get the index of the first identifier, that 
> won't work,
> because the second will already have overwritten it... I know I could 
> check
> the top of stack before the second identifier is parsed, but of course 
> this
> is a trivial case, and in more complex cases it won't be so clear 
> which parts
> of the rule will potentially overwrite the top of stack.

identifier will set the attribute value and you are free to use it at 
the appropriate time.  In your myRule example, you need to make a copy 
of the index value before it calls identifier again.  This is 
simulating what a return value bubbling up would do.  It's set below, 
but each reference has a copy.  The instance variable approach avoids 
the problem of having to define the return value in all the 
intermediate rules and the stack avoids the "what about recursive rule 
ref" problem.

Hope this helps...

Ter
--
Professor Comp. Sci., University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Co-founder, http://www.jguru.com
Co-founder, http://www.knowspam.net enjoy email again!
Co-founder, http://www.peerscope.com pure link sharing




 

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