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

The Archive Base Latest Questions

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

In ANTLR, for a given token, is there a way to tell which parser

  • 0

In ANTLR, for a given token, is there a way to tell which parser rule is matched?

For example, from the ANTLR grammar:

tokens
{
    ADD='Add';
    SUB='Sub';
}

fragment
ANYDIGIT    :   '0'..'9';

fragment
UCASECHAR   :   'A'..'Z';

fragment
LCASECHAR   :   'a'..'z';

fragment
DATEPART    :   ('0'..'1') (ANYDIGIT) '/' ('0'..'3') (ANYDIGIT) '/' (ANYDIGIT) (ANYDIGIT) (ANYDIGIT) (ANYDIGIT);

fragment
TIMEPART    :   ('0'..'2') (ANYDIGIT) ':' ('0'..'5') (ANYDIGIT) ':' ('0'..'5') (ANYDIGIT);

SPACE       :   ' ';

NEWLINE     :   '\r'? '\n';

TAB         :   '\t';

FORMFEED    :   '\f';

WS          :   (SPACE|NEWLINE|TAB|FORMFEED)+ {$channel=HIDDEN;};

IDENTIFIER  :   (LCASECHAR|UCASECHAR|'_') (LCASECHAR|UCASECHAR|ANYDIGIT|'_')*;

TIME        :   '\'' (TIMEPART) '\'';

DATE        :   '\'' (DATEPART) (' ' (TIMEPART))? '\'';

STRING      :   '\''! (.)* '\''!;

DOUBLE      :   (ANYDIGIT)+ '.' (ANYDIGIT)+;

INT         :   (ANYDIGIT)+;

literal     :   INT|DOUBLE|STRING|DATE|TIME;

var         :   IDENTIFIER;

param       :   literal|fcn_call|var;

fcn_name    :   ADD | 
                SUB | 
                DIVIDE | 
                MOD | 
                DTSECONDSBETWEEN | 
                DTGETCURRENTDATETIME |
                APPEND |
                STRINGTOFLOAT;

fcn_call    :   fcn_name WS? '('! WS? ( param WS? ( ','! WS? param)*)* ')'!;

expr        :   fcn_call WS? EOF;

And in Java:

  CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
  nodes.reset();
  Object obj;
  while((obj = nodes.nextElement()) != null)
  {
      if(nodes.isEOF(obj))
      {
          break;
      }
      System.out.println(obj);
  }

So, what I want to know, at System.out.println(obj), did the node match the fcn_name rule, or did it match the var rule.

The reason being, I am trying to handle vars differently than fcn_names.

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

    No, you cannot get the name of a parser rule (at least, not without an ugly hack ➊).

    But if tree is an instance of CommonTree, it means you’ve already invoked the expr rule of your parser, which means you already know expr matches first (which in its turn matches fcn_name).

    ➊ On a related note, see: Get active Antlr rule

    • 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 which is given as an example by Terrence
I have a simple ANTLR grammar, which I have stripped down to its bare
I have an AST derived from the ANTLR Parser Generator for Java. What I
In Antlr, if I have a rule for example: someRule : TOKENA TOKENB; it
Given an ANTLR Java Grammar - what java source code would I write to
Using ANTLR v3 and the CSharp2 language specifier, is there any way to indicate
I've got an antlr grammar that has a rule like so: rule: ID (COMMA
I am trying to write an ANTLR parser rule that matches a list of
I'm having a problem running the C runtime example at http://www.antlr.org/depot/examples-v3/C/treeparser from Antlr. I've
ANTLR generates java source from the grammar file. Generated source has dependency to ANTLR

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.