[antlr-interest] [Newbie] Parsing exactly N characters

Alexandre Hamez alexandre.hamez at lip6.fr
Wed Aug 22 03:29:32 PDT 2007


Hi everybody,

I need to parse instructions like that:
	TR( CAMI_STRING )
or	OC( CAMI_STRING , CAMI_STRING , CAMI_STRING)

Where CAMI_STRING has the form
	INTEGER:STRING.
INTEGER is the size of the following STRING which could contains any  
ASCII character, except '\n'.

My problem is that I can have something like that:
	TR(5:ab)d)
or  	OC(2:ab,3:a;c,2:ac)
	
	So strings can have an embedded ',' or ')', but with no particular  
meaning.

	I was hopping that something dummy as the following  would be  
sufficient, but indeed it doesn't work:

> /*------------------------------------------------------------------
> * PARSER RULES
> *------------------------------------------------------------------*/
>
>
> cami	:	
> 	command*
> 	;
>
> command 	:
> 	open_connection |
> 	trace_message
> 	;
>
> open_connection		:
> 	'OC(' cami_string ',' cami_string ',' cami_string ')'	
> 	;
>
> trace_message	:
> 	'TR(' cami_string ')'
> 	;
>
> cami_string
> 	:	
> 	NUMBER ':' STRING
> 	;
>
> /*------------------------------------------------------------------
> * LEXER RULES
> *------------------------------------------------------------------*/
>
> NUMBER	
> 	: 	
> 	('0'..'9')+
> 	;
>
> STRING	
> 	:
> 	(
> 	'a'..'z' |
> 	' ' 	 |
> 	')' 	 |
> 	','
> 	)+
> 	;
>
> NEWLINE
> 	: 	
> 	( '\r' | '\n' )+ 	
> 	{ $channel = HIDDEN; }
> 	;
>
>

With such a parser, I end up with the following error:
> TR(5:abcde)
> BR.recoverFromMismatchedToken
> line 0:-1 mismatched input '<EOF>' expecting ')'


So, is it possible to use the value of the NUMBER to parser exactly  
NUMBER characters? Or is there a smarter parser rule I can write? I  
tried to use the greedy option, but it doesn't change anything.

Any help would be gladly appreciated :-)

------------------------------------------------------------------------ 
---
Alexandre Hamez   LIP6 - MoVe / EPITA - LRDE
LIP6: tel: +33 1 44 27 31 92  / Bureau 818
104 Avenue du Président Kennedy 75016 Paris
http://www-src.lip6.fr/~Alexandre.Hamez


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2429 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20070822/060746b0/attachment.bin 


More information about the antlr-interest mailing list