[antlr-interest] simple return value problem

Tobias Wunner tobias.wunner at gmail.com
Tue May 12 00:42:25 PDT 2009


Hello,

still new ANTLR I ran into a simple return value problem. I would like  
to parse a token sequence and return the matched string.

For example I would like to match the characters "hello" in  "11 hello  
22" and return it as a string. I used $var.getText() and matched with  
something like var=CHAR+.

Unfortunately I was only able to retrieve the last character e.g. "o"  
in "hello"

My full code is attached below.

For any help I would be very thankful.

Toby

sample input:		MYTYPES: name1, name2
sample output:		Type = S
					  param = 1
					  param = 2

grammar parse;

@members {
String type;
ArrayList<String> list = new ArrayList<String>();
}

in		:  typeExpression
		{
		  System.out.println("Type = "+type);
		  for(String p:list) {
		    System.out.println(" param = "+p);
		  }
		};
typeExpression  :  '@' var=(CHAR|DIGIT)+ ':' DELIM* param (DELIM* ','  
DELIM* param)*
		{
		   type=$var.getText();
		};
param		:  var=(CHAR|DIGIT)+
		{
		  list.add($var.getText());
		}
		;

CHAR	:	'a'..'z';
DIGIT	:	'0'..'9';
DELIM	:	' '|'\t'|'\n';


More information about the antlr-interest mailing list