[antlr-interest] Subrule alternatives - MismatchedTokenException cannot be explained

John B. Brodie jbb at acm.org
Mon Aug 30 14:00:29 PDT 2010


Greetings!

On Mon, 2010-08-30 at 13:01 -0700, st3 wrote:
> Hi,
> 
> I have a simple grammar - which includes a '+' or '-' operation on a
> variable (ID) or constant (INT).
> The "add" rule is obviously: "mult" (+/- "mult"). 
> 
> However, the way I need the "add" rule defined throws the
> MismatchedTokenException:
> ----------------------------------------------------------------------------
> add	:	mult	
> 		( 
> 			('+' mult) 
> 			| 
> 			('-' mult) 
> 		)*
> 	;
> 
> while this way to define "add" rule works just fine:
> ----------------------------------------------------------------------------
> add	:	mult ( ( '+' | '-' ) mult)*	;
> 
> 
> Can you please give some insights white Antlr 3.0 does not like the first
> way to define "add" rule?

unable to reproduce.

both ways work equally well for me when i run the org.antlr.Tool from
the command line on the Dummy.g grammar you supplied. both get a warning
about no start rule that can end in EOF, but is easily fixed by adding a
rule:

start : expr EOF ;

I am currently using ANTLR 3.1.2 (because I happen to need a python
target) perhaps there is a difference in the versions? (i doubt it)


> The reason I need it this way is to use tree rewrites.
> 
> I tried using syntactic predicates but they error out. I have consulted
> Anltr reference book as well - however nothing obvious jumped at me.
> 
> Full grammar below.
> 
> Thanks a lot for your help!

hope this helps...
   -jbb

> 
> 
> 
> grammar Dummy;
> 
> expr : add ';' ;
> 
> add	:	mult	
> 		( 
> 			('+' mult) 
> 			| 
> 			('-' mult) 
> 		)*
> 	;
> 
> //add	:	mult ( ( '+' | '-' ) mult)*	;
> 
> mult	:	constant
> 		|	variable
> 		|	'(' expr ')'
> 		;
> 						
> variable	:	ID;	
> constant	:	INT;
> 
> ID  :	('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
> INT :	'0'..'9'+;
> WS  :   ( ' ' | '\t' | '\r' | '\n' ) { $channel=HIDDEN; } ;
> 





More information about the antlr-interest mailing list