[antlr-interest] Trouble using keyword tokens

Jim Idle jimi at temporal-wave.com
Mon Feb 22 07:54:33 PST 2010


Don't forget to use the search engine at antlr.markmail.org  

Do the following:

1) Do not use 'LITERALS', make tokens for the lexer, list all the keywords in order (or at least in one group);
2) Replace ID with a parser rule id;
3) Make the id rule:   id: ID | MEASUREMENT ;

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Anders Sollander
> Sent: Monday, February 22, 2010 7:31 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Trouble using keyword tokens
> 
> 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
> 
> 
> 
> 
> 
> 
> 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