[antlr-interest] Selectively disabling lexer rules
Terence Parr
parrt at cs.usfca.edu
Mon Jan 2 10:28:12 PST 2006
On Jan 2, 2006, at 1:41 AM, Martin Traverso wrote:
> Ter,
>
> Is there a way to selectively disable lexer rules in ANTLR v3? Let
> me explain the problem I'm trying to solve. Maybe you have other
> ideas.
>
> Ruby has a conditional operator like Java:
>
> a ? b : c
>
> But the question mark is also used to represent character literals
> like so:
>
> ?x -> the character 'x'
>
> The character after the ? has to be a printable character, so new
> lines, spaces, etc are not allowed (for those, the literal is ?\n, ?
> \s, etc)
>
> The problem is that in the conditional expression a space is not
> required after the ?.
Ugh...ruby is full of these landmines...why do languages by people
who don't know how to parse become popular!?? ugh.
> Thus, "a ?b : c" is a valid expression. So you see the conflict.
> When parsing that expression the lexer cannot decide whether to
> match a character literal or the ? operator.
You could use a simple sem pred after the '?' to look at the next
chars, looking for ':' I suppose.
If you want to gate out a rule, use {...}?=> on the front of it. :)
Ter
More information about the antlr-interest
mailing list