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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:46:30+00:00 2026-05-25T23:46:30+00:00

The java code generated from ANTLR is one rule, one method in most times.

  • 0

The java code generated from ANTLR is one rule, one method in most times. But for the following rule:

switchBlockLabels[ITdcsEntity _entity,TdcsMethod _method,List<IStmt> _preStmts]
    :   ^(SWITCH_BLOCK_LABEL_LIST switchCaseLabel[_entity, _method, _preStmts]* switchDefaultLabel? switchCaseLabel*)
    ;

it generates a submethod named synpred125_TreeParserStage3_fragment(), in which mehod switchCaseLabel(_entity, _method, _preStmts) is called:

synpred125_TreeParserStage3_fragment(){
    ......
    switchCaseLabel(_entity, _method, _preStmts);//variable not found error        
    ......
}

switchBlockLabels(ITdcsEntity _entity,TdcsMethod _method,List<IStmt> _preStmts){
    ......
    synpred125_TreeParserStage3_fragment();
    ......
}

The problem is switchCaseLabel has parameters and the parameters come from the parameters of switchBlockLabels() method, so “variable not found error” occurs.

How can I solve this problem?

  • 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-25T23:46:30+00:00Added an answer on May 25, 2026 at 11:46 pm

    My guess is that you’ve enabled global backtracking in your grammar like this:

    options {
      backtrack=true;
    }
    

    in which case you can’t pass parameters to ambiguous rules. In order to communicate between ambiguous rules when you have enabled global backtracking, you must use rule scopes. The “predicate-methods” do have access to rule scopes variables.


    A demo

    Let’s say we have this ambiguous grammar:

    grammar Scope;
    
    options { 
      backtrack=true;
    }
    
    parse
      :  atom+ EOF
      ;
    
    atom
      :  numberOrName+
      ;
    
    numberOrName
      :  Number
      |  Name
      ;
    
    Number : '0'..'9'+;
    Name   : ('a'..'z' | 'A'..'Z')+;
    Space  : ' ' {skip();};
    

    (for the record, the atom+ and numberOrName+ make it ambiguous)

    If you now want to pass information between the parse and numberOrName rule, say an integer n, something like this will fail (which is the way you tried it):

    grammar Scope;
    
    options { 
      backtrack=true;
    }
    
    parse
    @init{int n = 0;}
      :  (atom[++n])+ EOF
      ;
    
    atom[int n]
      :  (numberOrName[n])+
      ;
    
    numberOrName[int n]
      :  Number {System.out.println(n + " = " + $Number.text);}
      |  Name   {System.out.println(n + " = " + $Name.text);}
      ;
    
    Number : '0'..'9'+;
    Name   : ('a'..'z' | 'A'..'Z')+;
    Space  : ' ' {skip();};
    

    In order to do this using rule scopes, you could do it like this:

    grammar Scope;
    
    options { 
      backtrack=true;
    }
    
    parse
    scope{int n;         /* define the scoped variable          */ }
    @init{$parse::n = 0; /* important: initialize the variable! */ }
      :  atom+ EOF
      ;
    
    atom
      :  numberOrName+
      ;
    
    numberOrName /* increment and print the scoped variable from the parse rule */
      :  Number {System.out.println(++$parse::n + " = " + $Number.text);} 
      |  Name   {System.out.println(++$parse::n + " = " + $Name.text);}
      ;
    
    Number : '0'..'9'+;
    Name   : ('a'..'z' | 'A'..'Z')+;
    Space  : ' ' {skip();};
    

    Test

    If you now run the following class:

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String src = "foo 42 Bar 666";
        ScopeLexer lexer = new ScopeLexer(new ANTLRStringStream(src));
        ScopeParser parser = new ScopeParser(new CommonTokenStream(lexer));
        parser.parse();
      }
    }
    

    you will see the following being printed to the console:

    1 = foo
    2 = 42
    3 = Bar
    4 = 666
    

    P.S.

    I don’t know what language you’re parsing, but enabling global backtracking is usually overkill and can have quite an impact on the performance of your parser. Computer languages often are ambiguous in just a few cases. Instead of enabling global backtracking, you really should look into adding syntactic predicates, or enabling backtracking on those rules that are ambiguous. See The Definitive ANTLR Reference for more info.

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

Sidebar

Related Questions

I am working in a model-driven environment, where Java code is generated from a
I have the following code generated by Eclipse (.java file). import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Display;
ANTLR generates java source from the grammar file. Generated source has dependency to ANTLR
I have a java code for copy file from one folder to another folder.
I have generated a java code from matlab and while executing the java code
I have a problem while pasting my contents (or text) generated by Java code
Our Java code (not the test code) reads files from the current directory, which
I have an antlr generated Java parser that uses the C target and it
Does anyone know what approach one can take to automatically generate Java source code,
I have an AST derived from the ANTLR Parser Generator for Java. What I

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.