Note the first sentence Terence starts with: "just had some cool ideas about a semantic rule specification language...". That's what the first example is: an idea. It's not valid syntax.
There are (at least) two options for you:
1. rewrite the text in the token immediately
grammar T;
options{
output=AST;
}
@parser::members {
public static void main(String[] args) throws Exception {
TLexer lexer = new TLexer(new ANTLRStringStream("NVARCHAR"));
TParser parser = new TParser(new CommonTokenStream(lexer));
parser.type();
}
}
type
: NVARCHAR {System.out.println("token=" + $NVARCHAR.text);}
;
NVARCHAR
: 'NVARCHAR' {setText("VARCHAR");}
;
But this only adjusts the text
, not the type
of the token, which remains a NVARCHAR
type.
2. use an imaginary token:
grammar T;
options{
output=AST;
}
tokens {
VARCHAR='VARCHAR';
}
@parser::members {
public static void main(String[] args) throws Exception {
TLexer lexer = new TLexer(new ANTLRStringStream("NVARCHAR"));
TParser parser = new TParser(new CommonTokenStream(lexer));
parser.type();
}
}
type
: NVARCHAR -> VARCHAR
;
NVARCHAR
: 'NVARCHAR'
;
which changes the text
and type
of the token.
As you can see, with both demos, token=VARCHAR
is being printed to the console:
bart@hades:~/Programming/ANTLR/Demos/T$ java -cp antlr-3.3.jar org.antlr.Tool T.g
bart@hades:~/Programming/ANTLR/Demos/T$ javac -cp antlr-3.3.jar *.java
bart@hades:~/Programming/ANTLR/Demos/T$ java -cp .:antlr-3.3.jar TParser
token=VARCHAR