[antlr-interest] Newbie: Token/Rule Difference [again..]

Andreas Bartho andreas.bartho at inf.tu-dresden.de
Fri May 23 08:18:03 PDT 2008


Hi,

in your first grammar a single letter is matched by LETTER, i.e. the 
parser receives the LETTER token, but expects the WORD token, which 
causes the error. As you probably do not want LETTER to be a token but 
only a helper rule, try adding the fragment keyword. The same holds for 
digit:

grammar G1;
q	:	WORD;

fragment
LETTER 	:	('a'..'z' | 'A'..'Z');

fragment
DIGIT 	:	'0'..'9';

WORD	:	(DIGIT | LETTER)+;

This should work
	Andreas


Trey Cool Shonuff wrote:
> Hi all,
> I'm totally new to ANTLR and the last time I've really fiddled with language processing was back in college.
> Please help me tell why the following two, similar grammars behave differently:
> 
> grammar G1;
> q	:	WORD;
> LETTER 	:	('a'..'z' | 'A'..'Z');
> DIGIT 	:	'0'..'9';
> WORD	:	(DIGIT | LETTER)+;
> --------
> 
> grammar G2;
> q	:	(DIGIT | LETTER)+;
> LETTER 	:	('a'..'z' | 'A'..'Z');
> DIGIT 	:	'0'..'9';
> --------
> 
> I thought that these two were equivalent, however, G1 does not recognize the single-character sentence (throws MismatchedTokenException), while G2 does.
> 
> Can you tell me what the actual difference is?
> Than You,
> -Trevor.



More information about the antlr-interest mailing list