Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5977299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:21:02+00:00 2026-05-22T21:21:02+00:00

This is my tree grammar: grammar t; options{ output = AST; } type :

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:21:03+00:00Added an answer on May 22, 2026 at 9:21 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple grammar: grammar sample; options { output = AST; } assignment
I added output=AST; to the grammar options at the top of cs.g to print
I have a simple grammar options { language = Java; output = AST; ASTLabelType=CommonTree;
I don't see why the following antlr grammar does not generate the tree for
Hi I have a small problem in my ANTLR tree grammar. I am using
Whilst creating an inline ANTLR Tree Grammar interpreter I have come across an issue
With ANTLR2, you could define something like this in grammar definition file: options {
I have an ANTLR grammar which creates an AST, and then I have written
Reading and watching this presentation: http://boost-spirit.com/home/2011/06/12/ast-construction-with-the-universal-tree/ I've discovered this statement -- basically we are
Why do I get this error while generating code in ANTLR? [16:06:38] error(10): internal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.