[antlr-interest] Conditional lexing

Mike Lischke mike at lischke-online.de
Fri Sep 14 01:54:17 PDT 2012


Justin, Jim,

thanks for your suggestions.

> 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.


I had already tried a gated semantic predicate and found it to produce the same exception as other predicates I tried (including a validating semantic predicate), but I didn't know it's mandatory to have at least one alternative there (and be it empty). The generated code completely changes when I add an empty alternative and now works as intended. It's also interesting to note that gated semantic predicates only work at the start of a rule.

For reference here the now corrected and working rule:

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

The next step is now to keep a global flag to track when a version comment is active so I can properly handle the trailing '*/' part. What is usually used for such a flag, USERx?

Jim, as a side note: the C target code generator generates sometimes invalid code leading to a compilation error, due to the fact the grammar line is included in the source code. Most of the time a single line comment is used but for no apparent reason the multi line variant is used which then conflicts with the '*/' part. This is why I wrote '*' '/' in the rule above.

Mike
-- 
www.soft-gems.net




More information about the antlr-interest mailing list