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

shmuel siegel antlr at shmuelhome.mine.nu
Tue Feb 15 07:24:28 PST 2011


On 2/15/2011 1:05 PM, Richard Druce wrote:

> 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
Do you still get the problem if the '$' is not optional?



More information about the antlr-interest mailing list