[antlr-interest] How to intervene automatic keywords generation

Prasad K K prasad at aztec.soft.net
Fri Oct 18 11:05:26 PDT 2002


Hi,

You can use the following approach

on_error_stmt:
	"on" "error" "goto" label
;

keywords_that_can_be_identifiers:
	"error"
	"on"
;

identifier:
	IDENTIFIER | keywords_that_can_be_identifiers
;

Where IDENTIFIER is the Lexer rule that matches identifiers

Hope it helps.

- K

-----Original Message-----
From: Fan Yang [mailto:yhhf_dy at yahoo.com]
Sent: Wednesday, October 16, 2002 2:17 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] How to intervene automatic keywords generation


Hi,

According to the manual of ANTLR about Keywords and literals: any 
double-quoted string used in a parser is automatically entered into 
the literals table of the associated lexer. For instance,

on_error_stmt 
   : "on" "error" "goto" label)
   ;

So "on", "error" and "goto" will be automatically putted into the 
literals table. But in my target language "error" is not a keyword. 
so "error" can be an "identifier" in a statement as following pseudo 
code line shows. 

  printf("%s", e.error)

In fact, this line will cause a problem. because the parser will look 
for the identifier -"error" in the literals table first, then 
treat "error" as keyword not an general identifier. 

IDENT
  options {testLiterals=true;}
  :	('a'..'z'|'_') ('a'..'z'|'_'|'0'..'9')*	
  ;

My question is that may I determine which one ,double-quoted strings, 
can be entered into literals table instead of automatically 
generatation. Or there is an alternative way to deal with this kind 
of problem

Thanks,

Fan



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list