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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:58:28+00:00 2026-05-27T05:58:28+00:00

How can we distinguish a variable name, and an identifer, in an ANTLR grammar?

  • 0

How can we distinguish a variable name, and an identifer, in an ANTLR grammar?

VAR: ('A'..'Z')+ DIGIT*  ;
IDENT  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-')*;

The piece of grammar (in ANTLR) does not work because the compiler will complain that IDENT may never be reached for some input. This seems to be a classic head-hack for compiler writers, The lexer hack

For the ANTLR users, Could you tell me your neat way to work around it? Thanks

  • 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-27T05:58:28+00:00Added an answer on May 27, 2026 at 5:58 am

    zell wrote:

    The piece of grammar (in ANTLR) does not work because the compiler will complain that IDENT may never be reached for some input.

    No, that is not correct. The following grammar:

    grammar T;
    
    parse
      :  .* EOF
      ;
    
    VAR   : ('A'..'Z')+ DIGIT*  ;
    IDENT : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-')*;
    
    fragment DIGIT : '0'..'9';
    

    does not produce any error or warning. The lexer simply creates two type of tokens:

    1. if something starts with one or more upper case ascii letters followed by zero or more digits, a VAR is created;
    2. if something starts with a lowercase ascii letter or underscore, followed by ('a'..'z'|'A'..'Z'|'0'..'9'|'_'|'-')*, a IDENT is created.

    Note that therefor an IDENT can never start with an uppercase ascii letter: that will always become a VAR.

    So, if you have a parser rule that looks like:

    foo
      :  IDENT
      ;
    

    and the entire input is "BAR", then there will be a parser error because the lexer will not produce a INDENT token, but a VAR token, even though the parser “asks” for a IDENT.

    You must understand that no matter what the parser asks from the lexer, the lexer operates independently from the parser.

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

Sidebar

Related Questions

How can I distinguish a variable as a compiler-constructed string? For example, while the
Possible Duplicate: How to distinguish between a variable that is not declared and a
Can I distinguish signal, between delivered directly to a process and delivered via debugger.
How can I distinguish between published OSGI services implementing same interface by their properties?
Let's say I have a button.. how can I distinguish between single click and
There seems to be a bug in the Java varargs implementation. Java can't distinguish
I'm trying to find a CSS selector that can help me distinguish between the
I am generally not one to engage in subjective arguments over matters like variable
How can I distinguish in makefile, which targets and how( when ) they are
I'm looking at things that can distinguish a blog from a normal website. These

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.