[antlr-interest] Trouble using keyword tokens

Anders Sollander Anders.Sollander at mathworks.de
Mon Feb 22 07:30:42 PST 2010


Hi,

I have an issue with a parser that is using certain keywords within the parser rules, and these keywords can also appear as random identifiers in other rules. When this happens, the parser reports an error, and I see no way to avoid this. The grammar and the test input can be found below.
When the parser reaches the second HELLO rule, it stumbles on the token MEASUREMENT (evidently because it's present as a word in the meas rule). Is there a workaround for this?

Thanks
Anders

Grammar (testkeyword.g):
----------------------------------------------------

grammar testkeyword;

prog 	: body+	;	
body	:  meas | words 	;
	
meas	:	
	BEGIN 'MEASUREMENT'
		INT
	END 'MEASUREMENT' ;
	
words	:	
	BEGIN id1=ID
		INT ID+
	END id2=ID ;

BEGIN 	:	'/begin';
END	:	'/end';

ID  :	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
INT :	'0'..'9'+ ;
WS  :   ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;}  ;


Test input:
-----------------------------------------------------------
/begin HELLO
	13 I say hello
/end HELLO
/begin MEASUREMENT
	44
/end MEASUREMENT
/begin HELLO
	55 Use MEASUREMENT word
/end HELLO







More information about the antlr-interest mailing list