This is my tree grammar:
grammar t;
options{
output = AST;
}
type
:
'NVARCHAR' -> "VARCHAR"
;
ANTLR3 3.1.3 says:
syntax error: antlr: t.g:12:5: unexpected token: 'NVARCHAR'
What’s wrong here? I took it from this article.
ps. I’m using this grammar later in order to get AST out of it. Once the AST is retrieved I’m walking through it and add every token’s text to some string buffer. The idea of the rewriting above is to replace certain tokens. I’m doing language-to-language mapping (SQL to SQL dialect, to be more specific).
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
But this only adjusts the
text, not thetypeof the token, which remains aNVARCHARtype.2. use an imaginary token:
which changes the
textandtypeof the token.As you can see, with both demos,
token=VARCHARis being printed to the console: