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 6923523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:32:46+00:00 2026-05-27T10:32:46+00:00

I have the following ANTLR grammar which is given as an example by Terrence

  • 0

I have the following ANTLR grammar which is given as an example by Terrence Parr.

grammar b;
backtrack
   :    (cast ';')=> cast ';'
   |   (e ';')=>    e ';'
   |                e '.'
   ;

cast:   '(' ID ')' ;

e   :   '(' e ')'
    |   ID
;

ID  :   'a'..'z'+ ;

However, when I try to interpret (a) with the backtrack rule, I get a MisMatchedToken exception. I ask this question because I have a much bigger grammar which I use for a compiler. I have the exact problem there. If I remove one of the rules everything works fine (except for the input parse by the rule removed, of course) but when I add the syntactic predicates I get an error when I parse, even though the grammar compiles(exactly like with the b grammar).

Any suggestions or ideas why this might be? Thank you.

  • 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-27T10:32:47+00:00Added an answer on May 27, 2026 at 10:32 am

    The interpreter in ANTLRWorks is notoriously buggy, and doesn’t handle any kind of predicate at all. So don’t use it.

    Note that you said you parsed "a()", but that will not be parsed properly. The parser will complain it is missing a ".". I presume you meant you’s parsing "a();"

    If you run the following demo:

    grammar b;
    
    @parser::members {
      public static void main(String[] args) throws Exception {
        bLexer lexer = new bLexer(new ANTLRStringStream("(a);"));
        bParser parser = new bParser(new CommonTokenStream(lexer));
        parser.backtrack();
      }
    }
    
    backtrack
     : (cast ';')=> cast ';' {System.out.println("cast");}
     | (e ';')=>    e ';'    {System.out.println("e");}
     |              e '.'
     ;
    
    cast
     : '(' ID ')' 
     ;
    
    e
     : '(' e ')'
     | ID
     ;
    
    ID : 'a'..'z'+ ;
    

    by executing:

    java -cp antlr-3.3.jar org.antlr.Tool b.g 
    javac -cp antlr-3.3.jar *.java
    java -cp .:antlr-3.3.jar bParser

    (on Windows, the last command should look like: java -cp .;antlr-3.3.jar bParser)

    you’ll see that cast is being printed to the console without any kind of error or warning from ANTLR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Antlr grammar: grammar MyGrammar; doc : intro planet; intro :
I have the following production in my ANTLR grammar: rich_newick_string : str=(.*';') { stack.pushRichNewickString($str.text);
I have the following ANTLR grammar: grammar Tasks; options { language = Java; }
I have following Code Block Which I tried to optimize in the Optimized section
i have following html page with UL and LI, i try to assign width
I don't see why the following antlr grammar does not generate the tree for
I want to parse ISO 8601 dates in my ANTLR grammar. 2001-05-03 I have
I have a parser rule in ANTLR that takes a parameter. tincture [Tinctures tinctures]
I'm attemping to learn language parsing for fun... I've created a ANTLR grammar which
Whilst creating an inline ANTLR Tree Grammar interpreter I have come across an issue

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.