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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:05:48+00:00 2026-06-04T03:05:48+00:00

Which one would you prefer to declare a variable in which case, global scope

  • 0

Which one would you prefer to declare a variable in which case, global scope or @members declaration? It seems to me that they can serve for same purpose?

UPDATE here is a grammar to explain what i mean.

grammar GlobalVsScope;

scope global{
  int i;
}
@lexer::header{package org.inanme.antlr;}
@parser::header{package org.inanme.antlr;}

@parser::members {
  int j;
}

start
scope global;
@init{
  System.out.println($global::i);
  System.out.println(j);
}:R EOF;

R:'which one';
  • 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-06-04T03:05:50+00:00Added an answer on June 4, 2026 at 3:05 am

    Note that besides global (ANTLR) scopes, you can also have local rule-scopes, like this:

    grammar T;
    
    options { backtrack=true; }
    
    parse
    scope { String x; }
    parse
     : 'foo'? ID {$parse::x = "xyz";} rule*
     | 'foo' ID
     ;
    
    rule
     : ID {System.out.println("x=" + $parse::x);}
     ;  
    

    The only time I’d consider using local rule-scopes is when there are a lot of predicates, or global backtracking is enabled (resulting in all rules to have predicates in front of them). In that case, you could create a member variable String x (or define it in a global scope) and set it in the parse rule, but you might be changing this instance/scope variable after which the parser could backtrack, and this backtracking will not cause the global variable to be set to it’s original form/state! The local scoped variable will also not be “unset”, but that will likely be less of a risk: them being local to a single rule.

    To summarize: yes, you’re right, global scopes and member/instance variables are much alike. But I’d sooner opt for members-variables because of the friendlier syntax.

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

Sidebar

Related Questions

What are the various scenarios where one would prefer BizTalk Server ? Further which
Which one would you prefer to extract a sub-string from the given string and
What is difference between these two? Which one you would prefer when you need
I am curious which one would be better fitting as a currency field ?
From the following list of frameworks, which one would you use to develop a
How do you connect these to your computer? Which one would be easier to
What is the difference between HttpContext.Current.Request.IsAuthenticated and HttpContext.Current.User.Identity.IsAuthenticated? Which one would you use in
Do all git commands have a --dry-run option, or one which would indicate what
If I would want to operate on pointers which point to one byte should
I have one query on which I would like to get your valuable feedback.

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.