[antlr-interest] a range as single token?

Bart Kiers bkiers at gmail.com
Sat Jan 15 02:50:16 PST 2011


On Sat, Jan 15, 2011 at 11:43 AM, fikin <nikolai.fiikov at gmail.com> wrote:

> most likely this is obviously stupid question, pardon me in advance please.
>
> what exactly is wrong with this grammar?
>
> lines   :       line (NEWLINE line)*;
> line    :       '\u0020'..'\u007F'*;
> NEWLINE :       '\r'? '\n';
>

The range operator works differently inside a parser-rule.
For example, the rule *foo*:

foo
  :  A..C
  ;

A : 'a';
Z : 'z';
C : 'c';


will match one of the tokens A, Z or C (not one of the characters 'a', 'b'
or 'c'!).

Just as the `.` (DOT meta character) that has a different meaning depending
in which rule it is used: it means 'any character' inside lexer rules, while
it means 'any token' inside parser rules.

Regards,

Bart.


More information about the antlr-interest mailing list