[antlr-interest] Lexer rule with alternatives (Newbie question)
Петров Александр
gmdidro at gmail.com
Sat Nov 22 09:44:17 PST 2008
> but there can be problems as '+' used in to different rules will create
> two distinct token types which will cause a MismatchedTokenTypeException
> later.
This is exactly what happens with my grammar.
Thank you very much.
2008/11/22 Johannes Luber <jaluber at gmx.de>:
> Петров Александр schrieb:
>> Hello,
>> why such grammar doesn't work properly:
>> --------------------------------------------------------------------------------------
>> expr :
>> operand ((oper) operand)*
>> ;
>> //Correct operands
>> operand :
>> INT
>> ;
>> OPER: ('+'|'-'|'*'|'/')
>> ;
>> INT :
>> ('0'..'9')+
>> ;
>> --------------------------------------------------------------------------------------
>>> t+1
>> line 0:3 mismatched input '+' expecting EOF
>>
>> But if I replace the Lexer rule by Parser rule:
>>
>> oper: ('+'|'-'|'*'|'/');
>>
>> All work as it should. Why I should't use Lexer rule with alternatives ?
>
> The problem with your example is that in the expr rule you use oper, but
> define OPER as rule. Did you overlook this while posting or does even
>
> expr :
> operand (OPER operand)*
> ;
>
> not work? Nonetheless, the usual way to define operands is to create a
> single rule for each operand and reference those lexer rules in a single
> parser rule for convenience. As "oper: ('+'|'-'|'*'|'/');" creates
> implicit lexer rules doing it this way simulates the described approach,
> but there can be problems as '+' used in to different rules will create
> two distinct token types which will cause a MismatchedTokenTypeException
> later.
>
> Johannes
>>
>> Thank you
>>
>> 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