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

Justin Murray jmurray at aerotech.com
Tue Feb 15 07:26:10 PST 2011


Hi Richard,

The answer is to use 2 lexer rules instead of one, and change the parser 
rule. It is generally not a good idea to mark whitespace as hidden (as 
you have done), and also use whitespace in your lexer rules. Here is 
what I would try:

grammar testing;

start :
	(	DOLLAR? INT CENTS?
	| 	NEW_LINE
	)+
	;

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

DOLLAR	: '$';
CENTS	: '.' DIGIT DIGIT;

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

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

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


- Justin

On 2/15/2011 6:05 AM, 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
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list