I have strings like this:
`(val1, val2, val3)`
And I have ANTLR grammar to parse this code:
grammar TEST;
tokens {
ORB = '(';
CRB = ')';
COMA = ',';
}
@members{
}
/*Parser rule*/
mainRule
: ORB WORD (COMA WORD)* CRB;
/*Lexer rule*/
WORD : ('a'..'z'|'A'..'Z'|'0'..'9')+;
WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; };
Now I need to map all WORDs into Java. How can I bind value when target token embraced into brackets?
Many thanks!
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
initneeds an@sign in front of it (at least ANTLR v3 expects it).And then:
On Windows the commands above are pretty much the same, only run your
TestParserlike this:(there’s a semi-colon instead of a regular colon)