[antlr-interest] Creating fake tokens

Mark Lentczner markl at glyphic.com
Wed May 5 08:19:40 PDT 2004


On May 4, 2004, at 11:38 PM, Brad Schick wrote:

>  and a nice
> solution would be to replace the ELSEIF token with tokens ELSE and
> IF.

I'm not sure what the goal is, but I bet it could be handled in an 
easier way in the parser:

If it is just that your grammar needs to accept both "elseif" and "else 
if":

else-if-part:
       ELSE IF expression statement
     | ELSEIF expression statement
     ;

If you just want your AST to be clean, consider:

if-statement:
     IF LPAREN! expression RPAREN! statement else-part ;

else-part:
       ELSE! statement
     | ELSIF! LPAREN! expression RPAREN! statement else-part
       { ## = #([IF], ##); }
     | empty
       { ## = #[NOP]; }
     ;

empty: ;

This builds a regular tree of the form (IF cond if-true-statment 
if-false-statment) where the statement parts could be nested IF forms, 
other statments or NOP (I don't like my trees to have optional parts).  
Other regular forms for conditional statements could be built if you 
prefer.

Unless the token stream is being used in some other way, I suspect 
either of these approaches will be much easier than fiddling with the 
lexer to spit out two tokens.

	- Mark

Mark Lentczner
markl at wheatfarm.org
http://www.wheatfarm.org/



 
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