[antlr-interest] ColonAssign vs Label Colon vs Colon
Gavin Lambert
antlr at mirality.co.nz
Thu Jun 26 12:56:49 PDT 2008
At 06:32 27/06/2008, Michael Fogeborg wrote:
>COLON : ':' ;
>IDENT : 'A'..'Z' ;
>ASSIGN : ID ':=' ID ;
>LABEL : IDENT COLON ;
>
>The Lexer gets lost on the COLON and I can't get this working...
This is because you're mixing leaf rules (COLON and IDENT) with
non-leaf rules (LABEL and ASSIGN). (And ASSIGN is using an
unlisted rule, but I'm assuming that's a typo.)
ASSIGN definitely looks like it should be in the parser, not the
lexer, as whitespace ought to be permitted around the ':=' sign,
and it's much easier to deal with multiple IDs there. The same
could also be true for LABEL -- most languages that do labels this
way do permit whitespace to be between the identifier and the
colon.
If you really want to match it via a lexer rule, though, then
you'll need to merge the rules and manually disambiguate:
/* this exists only to create the token name; it'll never be
called so the content is unimportant */
fragment LABEL: IDENT COLON;
IDENT
: ('A'..'Z') /* should that be ('A'..'Z')+ or did you really
want single-character identifiers? */
((COLON) => COLON { $type = LABEL; })?
;
(In this case you probably don't actually need the syntactic
predicate, but I usually prefer to put it in anyway, for symmetry
with the cases where you do need it.)
More information about the antlr-interest
mailing list