[antlr-interest] Fairly simple grammar question (Antlr 3.1)

Richard Druce contactdick at gmail.com
Tue Feb 15 03:05:18 PST 2011


Hi,

I'm sure this question has been answered before but I'm not sure how
to phrase it well enough to find it in the archives.

I want to match to inputs an INT such as '   170  \n'
and an AMOUNT such as '  $ 170.00 ' (unfortunately there is
occasionally a space before the amount)

The following grammar works for ' $ 170.00 ' but not for  ' 170 ' it
sees the space and tries to match the AMOUNT and the output
'mismatched character ' ' expecting '.' ' is provided.  Reading
through the antlr reference book, it looks like there are a number of
ways to possibly handle this but I'm not sure how best to approach it.

grammar testing;

start :
	(AMOUNT
	| INT
	| NEW_LINE)+;

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

AMOUNT 	: '$'? SPACE? DIGIT+ (','DIGIT+)? '.' DIGIT DIGIT;

INT :  DIGIT+ (','DIGIT+)?;

NEW_LINE
	: ('\r'?'\n');

WS  : (SPACE |'\t'|'\u000C')
	{$channel=HIDDEN;};
	
fragment SPACE
	: ' ';

Thanks,

Richard
--
m: +44 753 489 2926


More information about the antlr-interest mailing list