[antlr-interest] Help on gated semantic predicate

Kieran Simpson kierans777 at gmail.com
Sat Aug 11 06:16:41 PDT 2012


Yep, I've had the problem too.  The way I got around it was much in the 
vein of Andrews post, moving the "decision" into a parser rule (and thus 
the Parser class).  By having the parser "cast" the token it overcomes 
this shortcoming of the lexer.  For example:

id: MASK | ID;

Since MASK is really a subset of the characters than an ID can be, an id 
parser rule can consume either token, and the caller of the id rule will 
know that the input was matched correctly (semantically you got an ID 
with bit of syntactic trickery).

HTH.

Cheers,

On 11/08/12 4:23 AM, Andrew Mains wrote:
> Francis,
>
> The problem here is that both MASK and ID are lexer rules, not parser
> rules. As you probably know, even though you are using a combined
> grammar, the lexer and parser still compile down to separate Java
> classes. Since the lexer provides tokens for the parser, it necessarily
> executes its rules before the parser
> sees a single token. Furthermore, rules defined first in the lexer have
> precedence over those defined later, so the lexer will match MASK
> instead of ID.
>
> Therefore, the lexer matches MASK before start is even called, giving
> the behavior you observed.
>
> If mask were a parser rule instead of a lexer rule, it would act as you
> expected.
>
> Hope this helps!
>
> Andrew
>
> On 08/09/2012 12:00 PM, antlr-interest-request at antlr.org wrote:
>> start : ID;
>> MASK : {false}?=>( 'A' | 'B');
>> VARIABLE : 'A'..'Z';
>> ID : ('a'..'z'|'A'..'Z'|'_')
>> ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
>> WS : ( ' ' | '\t' | '\r' | '\n' )+ {
>> $channel=HIDDEN; } ;
>>
>
>


More information about the antlr-interest mailing list