Pretty much the same as JS Bangs' answer, only here's a complete SSCCE you can compile and run and I showed how you can "label" your tokens and access them to put them in the List the mainRule is returning. Also note that the init
needs an @
sign in front of it (at least ANTLR v3 expects it).
grammar Test;
@parser::members {
public static void main(String[] args) throws Exception {
String text = "(a, bb , ccc )";
ANTLRStringStream in = new ANTLRStringStream(text);
TestLexer lexer = new TestLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
System.out.println(new TestParser(tokens).mainRule());
}
}
mainRule returns [List<String> words]
@init{$words = new ArrayList<String>();}
: '(' w=WORD {$words.add($w.text);} (',' w=WORD {$words.add($w.text);} )* ')'
;
WORD
: ('a'..'z'|'A'..'Z'|'0'..'9')+
;
WS
: ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; }
;
And then:
bart@hades:~/Temp$ java -cp antlr-3.2.jar org.antlr.Tool Test.g
bart@hades:~/Temp$ javac -cp antlr-3.2.jar *.java
bart@hades:~/Temp$ java -cp .:antlr-3.2.jar TestParser
[a, bb, ccc]
bart@hades:~/Temp$
On Windows the commands above are pretty much the same, only run your TestParser
like this:
java -cp .;antlr-3.2.jar TestParser
(there's a semi-colon instead of a regular colon)