[antlr-interest] Conditional lexing

Justin Murray jmurray at aerotech.com
Thu Sep 13 10:23:28 PDT 2012


Mike,

You used a disambiguating/validating semantic predicate ({boolExpr}?),
which will cause an exception if the rule matches but the predicate is
false. You want a gated semantic predicate ({boolExpr}?=>), which
effectively turns off the following alternative if the predicate is
false. As Jim said, you may need both a true and false alternative,
depending on how this rule is used.

-Justin

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Mike Lischke
Sent: Thursday, September 13, 2012 12:39 PM
To: ANTLR Mailing List
Subject: [antlr-interest] Conditional lexing

Hi list,

I'd like to consume input in the lexer based on a variable I pass into
my lexer rule. How can I do  this?

fragment VERSION_COMMENT_TAIL[bool matches_version]:
	{!matches_version}? ( options { greedy = false; }: . )* '*' '/'
{ $type = VERSION_COMMENT; $channel = 98; } ;

This shall consume everything like a normal multiline comment if the
parameter is true. Otherwise it should do simply nothing. Unfortunately,
the generated code creates an exception if the param is false and in
addition runs the loop. Any other ideas?

If all fails I could simply make this instead:

fragment VERSION_COMMENT_TAIL[bool matches_version]:
	{ if (matches_version) return } ( options { greedy = false; }: .
)* '*' '/' { $type = VERSION_COMMENT; $channel = 98; } ;

but that seems so hack-ish...

Mike
--
www.soft-gems.net


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list