[antlr-interest] Fwd: Selectively disabling lexer rules

Bryan Ewbank ewbank at gmail.com
Mon Jan 2 06:17:30 PST 2006


Okay, now to the group :-(

On 1/2/06, Martin Traverso <mtraverso at gmail.com> wrote:
> 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 ?. 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.

I don't think the /lexer/ is where you'd want to do this, because it's
not really a lexical issue; it's a parsing issue.

Perhaps it would help to consider "?" as an ambiguous operator (binary
or unary, based on context) in the same way that "+" and "-" are
commonly treated?

T'would mean that you with need to distinguish "?" followed by
whitespace (always a binary operator) from "?" followed by a printable
character (binary or unary, by context).

Hope this helps,
- Bryan


More information about the antlr-interest mailing list