[antlr-interest] newbie request for help

micha micha-1 at fantasymail.de
Fri Dec 5 00:28:54 PST 2008


On Friday 05 December 2008 07:46:10 Kenny Leung wrote:
> Hi All.
>
> One of the interesting things I found was that this is legal:
>
>      NUMBER : '0'..'9';

that's a lexer rule

> but this is not:
>
>      number : '0'..'9';

that's a parser rule, the ".." syntax is only allowed in lexer rules.

> Is there a way I can say, "use tokenizer rule A after the "{", but use
> tokenizer rule B after the "=".

for context switching the lexer,  I have two ideas (which both may be wrong 
:-) ) :

let the lexer allways return charaters as token and  combine them in parser 
rules like (examples):

id: LETTER (LETTER | DIGIT )*
type_id:  I | D | S | CARET | V
type_encoding = LEFT_BRACE id EQUAl type_id+ RIGHT_BRACE


or try a syntactic predicate in the lexer like:

fragment ID: LETTER (LETTER | DIGIT)*

NAME: ;
TYPE: ;
EQUAL:;
V:;

NAME_or_TYPE: 
	  (ID '=') => ID { type = NAME; }
	| '=' { type = EQUAL; }
	| 'v'  { type = V;}
	...

but I don't know if this works  in nested cases.


 Michael



More information about the antlr-interest mailing list