[antlr-interest] Conditional lexing

Jim Idle jimi at temporal-wave.com
Fri Sep 14 09:53:27 PDT 2012


Mike,

I am aware of that. It is just that your comment rule is of course
generating a comment. Just separate '*/' in to characters and it should be
fine. There was some reason that I could not change that, but I no longer
remember. It's possible that I answered this before though.

I do sometimes wish that all your questions about the C target were not
prefixed with "There is a bug...", or "generates invalid code because it
is wrong" and so on :) You probably don't mean it to sound the way it
does.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Mike Lischke
> Sent: Friday, September 14, 2012 1:54 AM
> To: Justin Murray
> Cc: ANTLR Mailing List
> Subject: Re: [antlr-interest] Conditional lexing
>
>
> 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
>
>
>
> 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