Not sure how much this helps, but I implemented a preprocessing step in my 
grammar that could support directives like this:

Define CONST value

Which would subsequently be referred to like this:

$CONST

Sort of like #define in c/c++. Anyway, I did so by using the parser, 
TokenRewriteStream, and then reparsing:

public static String PreProcess (CharStream stream) 
{
 TPLLexer lexer = new TPLLexer(stream);
TokenRewriteStream rawTokens = new TokenRewriteStream(lexer);
List initTokens = rawTokens.getTokens();
int index = 0;
for (Object curObj : initTokens)
{
CommonToken curToken = (CommonToken) curObj;
String text = curToken.getText();
if (text.startsWith("$"))
{
text = text.substring(1); // remove '$'
if (lexer.getPreProcDefines().containsKey(text))
rawTokens.replace(index, lexer.getPreProcDefines().get(text));
}
index++;
}

return rawTokens.toString(); // produces String with our modifications
}

Poor performance, I'm sure. But it's one method. You could rewrite the tokens 
after the 1st parse in this way. Then parse the resulting string a 2nd time. 
Hope this helps.
-- 
Joey

On Tuesday, March 1, 2011 at 2:04 PM, Tenaja wrote: 
> How do you implement compiler directives? Can anyone offer an example, or the 
> page# in the book? 
> 
> 
> I'd like to implement a "switch" (actually, an array of switches) that alters 
> Antlrs output based on the switch value. For instance, switching TAB from 
> whitespace into a token that can be used for termination.
> 
> Thanks.
> 
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: 
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "il-antlr-interest" group.
> To post to this group, send email to il-antlr-interest@googlegroups.com.
> To unsubscribe from this group, send email to 
> il-antlr-interest+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/il-antlr-interest?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-interest@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to