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

  • Home
  • SEARCH
  • 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 3982438
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:31:40+00:00 2026-05-20T05:31:40+00:00

I’m pretty familiar with Antlr 2.X and the Antlr 3.1.X CSharp and python targets.

  • 0

I’m pretty familiar with Antlr 2.X and the Antlr 3.1.X CSharp and python targets.

However I’m now forced to use the Antlr 3 C target for a project.

My issue is how do I report errors in my grammar, or tree grammar.

Consider a rule that matches a token and we put it into a map. We want to ensure the token is unique. Normally I’d throw an exception if the token was already in the map and catch the exception outside of hte parser to report the error.

What is Antlr C runtime equivalent of the following rule?

token_match: ID 
{
    if(mp.find($ID.Text))
        throw std::exception("Non unique token found");
}
  • 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-20T05:31:41+00:00Added an answer on May 20, 2026 at 5:31 am

    I’d recommend using your own methods to display semantic errors. But if you need to stay in ANTLR for this:

    First you have to create your own error handler if you want to create custom exception types. Look at antlr3baserecognizer.c:1000 for the original.

    static void         
    displayRecognitionErrorNew  (pANTLR3_BASE_RECOGNIZER recognizer,
                                 pANTLR3_UINT8 * tokenNames)
    {
    ...
        switch  (ex->type)
        {
        case    ANTLR3_UNWANTED_TOKEN_EXCEPTION:
    ...
        case    NUTF_EXCEPTION:
          printf("Non unique token found");
          break;
    

    Then code some function or rule code to process an exception structure. Look at antlr3baserecognizer.c:325 to setup your exception.

    ex = antlr3ExceptionNew(ANTLR3_RECOGNITION_EXCEPTION,
                           (void *)ANTLR3_RECOGNITION_EX_NAME,
                           NULL,
                           ANTLR3_FALSE);
    
    ex->type                = NUTF_EXCEPTION
    ex->line                = ins->getLine (ins);
    ex->charPositionInLine  = ins->getCharPositionInLine    (ins);  
    ex->index               = is->index (is);
    ex->streamName          = ins->fileName;
    ex->message             = "That was totally unexpected";
    

    Next you need to actually throw your exception when you detect the error. I think the only way to do this is to override mismatch() adding in your code and calling it from your rule.

    static  void
    mismatchNew(pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST follow)
    {
      ...
    }
    

    Finally tell ANTLR to use your new functions.

    @parser::apifuncs {
      RECOGNIZER->displayRecognitionError       = displayRecognitionErrorNew;
      RECOGNIZER->antlr3RecognitionExceptionNew = antlr3RecognitionExceptionNewNew;
      RECOGNIZER->mismatch                      = mismatchNew;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.