[antlr-interest] Using token values in other java classes (newbie)

Nigel Sheridan-Smith nbsherid at secsme.org.au
Tue Jan 18 14:38:04 PST 2005


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Peter Robinson
> Sent: Wednesday, 19 January 2005 9:11 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Using token values in other java classes
> (newbie)
> 
> Hi,
> 
> I recently stumbled upon antlr -- it seems so much easier to use
> productively than lex/yacc that I have been trying to learn it, even
> though Java is not my "first" language. I hope these questions are not
> to obvious, but I have been frutilessly googling for a few hours, so
> please forgive...
> 
> 1) The file type I am trying to parse has thousands of individual
> records, each of which contain multiple specifications such as
> status xx
> where xx can be one of 2-30 keywords. I have a rule that contains the
> following.
> 
> 	STATUS_KW
> 		s:status_type {
> 		  myJavaObject.setStatus(Integer.parseInt(s.getText())); }
> 
> ;
> 

s=status_type is needed for return values... also, you won't need
s.getText() or Integer.parseInt() as the return value is an integer, not a
token. So just "myJavaObject.setStatus (s);" will work just fine :D


> status_type returns [ int x ]:  ( LIVE_KW { x = LIVE_KW; } |
> 	                         SECONDARY_KW { x = SECONDARY_KW; } |
> 				DISCONTINUED_KW { x = DISCONTINUED_KW; }|
> 				NEWENTRY_KW {  x = NEWENTRY_KW; } )
> ;
> 
> ==>> However, I keep getting error messages when transforming the .g
> file that "s" cannot be identified. What am I doing wrong?
> 
> 2) The grammar file generates a java interface with int constants for
> tokens. Assuming I have some java class files in subdirectories and want
> them to know about these constants, how does one do that (just class foo
> implements bar doesnt work).
> ==>> WHat is the best way to get other Java classes to use these tokens?
> 

Ummm... I think referencing them as ClassName.Identifier should work just
fine... so just refer to them as "MyTokenTypes.IDENT" from other classes.

Nigel


--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435 




More information about the antlr-interest mailing list