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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:52:13+00:00 2026-06-08T02:52:13+00:00

I have a lexical rule (Integer) which uses some fragments. In a parser rule

  • 0

I have a lexical rule (Integer) which uses some fragments. In a parser rule (parse) I want to rewrite my tree differently depending on which fragment generated the token in question. I have made a small grammar to demonstrate what I’m attempting:

grammar subrange;

options {
    output=AST;
}

tokens {
    NumberNode;
    DecimalNode;
    BinaryNode;
    HexNode;
    OctalNode;
}

parse
    : Integer+ -> ^(NumberNode Integer)+
    ;

Integer
    : DECIMAL_LITERAL
    | BINARY_LITERAL
    | HEX_LITERAL
    | OCTAL_LITERAL
    ;

fragment BINARY_LITERAL
    : '2#' ('0' | '1')+
    ;

fragment HEX_LITERAL 
    : ('16#' | '0' ('x'|'X')) HEX_DIGIT+
    ;

fragment HEX_DIGIT
    : (DIGIT|'a'..'f'|'A'..'F')
    ;

fragment DECIMAL_LITERAL 
    : ('0' | '1'..'9' DIGIT*)
    ;

fragment OCTAL_LITERAL 
    : '8#' ('0'..'7')+
    ;

fragment DIGIT
    : '0'..'9'
    ;

SPACE : (' ' | '\t' | '\r' | '\n')+ {skip();};

I want the parse rule to rewrite a DECIMAL_LITERAL under an imaginary DecimalNode but a BINARY_LITERAL under a BinaryNode (rather than everything under a NumberNode).

I’m attempting to do this by changing the token type inside the lexical rule so that I can then rewrite accordingly inside the parse rule.

I think I should be able to do this with an action but I have been unable to figure out how to find the returned token in order to change its type. http://www.antlr.org/wiki/display/ANTLR3/Special+symbols+in+actions seems to indicate that $tokenref should work but it doesn’t get translated at all.

Or is there another way to accomplish this?

Thanks in advance.

  • 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-08T02:52:14+00:00Added an answer on June 8, 2026 at 2:52 am

    It seems a bit odd to me: grouping all such literals under a single Integer token, and then, in a parser rule you want to separate them again.

    Why not just remove Integer and do:

    integer
        : BINARY_LITERAL // when output=AST, this creates a CommonTree with type 'BINARY_LITERAL'
        | HEX_LITERAL    // ...
        | DECIMAL_LITERAL
        | OCTAL_LITERAL 
        ;
    
    BINARY_LITERAL
        : '2#' ('0' | '1')+
        ;
    
    HEX_LITERAL 
        : ('16#' | '0' ('x'|'X')) HEX_DIGIT+
        ;
    
    DECIMAL_LITERAL 
        : ('0' | '1'..'9' DIGIT*)
        ;
    
    OCTAL_LITERAL 
        : '8#' ('0'..'7')+
        ;
    

    ?

    Or you could keep the Int(eger) rule but set the numerical value of the various int-literals by doing:

    Int
    @init{int skip = 0, base = 10;}
        : ( DECIMAL_LITERAL
          | BINARY_LITERAL  {base = 2;  skip = 2;} 
          | OCTAL_LITERAL   {base = 8;  skip = 2;} 
          | HEX_LITERAL     {base = 16; skip = $text.contains("#") ? 3 : 2;} 
          )
          {
            setText(String.valueOf(Integer.parseInt($text.substring(skip), base)));
          }
        ;
    
    fragment BINARY_LITERAL
        : '2#' ('0' | '1')+
        ;
    
    fragment HEX_LITERAL 
        : ('16#' | '0' ('x'|'X')) HEX_DIGIT+
        ;
    
    fragment DECIMAL_LITERAL 
        : ('0' | '1'..'9' DIGIT*)
        ;
    
    fragment OCTAL_LITERAL 
        : '8#' ('0'..'7')+
        ;
    

    Be careful giving rules a name as some object/class/reserved-word of the target language can have (Integer in case of Java).


    EDIT

    Okay. I’ll leave my other answer there in case passers-by are wondering why on earth I’m proposing this… 🙂

    Here’s what (I think) you’re after:

    grammar T;
    
    options {
      output=AST;
    }
    
    tokens {
      BinaryNode;
      OctalNode;
      HexNode;
      DecimalNode;
    }
    
    parse
     : integer+
     ;
    
    integer
     : i=Integer -> {$i.text.startsWith("2#")}?         ^(BinaryNode Integer)
                 -> {$i.text.startsWith("8#")}?         ^(OctalNode Integer)
                 -> {$i.text.matches("(16#|0[xX]).*")}? ^(HexNode Integer)
                 ->                                     ^(DecimalNode Integer)
     ;
    
    Integer
     : DECIMAL_LITERAL
     | BINARY_LITERAL
     | HEX_LITERAL
     | OCTAL_LITERAL
     ;
    
    fragment BINARY_LITERAL
     : '2#' ('0' | '1')+
     ;
    
    fragment HEX_LITERAL 
     : ('16#' | '0' ('x'|'X')) HEX_DIGIT+
     ;
    
    fragment HEX_DIGIT
     : (DIGIT|'a'..'f'|'A'..'F')
     ;
    
    fragment DECIMAL_LITERAL 
     : ('0' | '1'..'9' DIGIT*)
     ;
    
    fragment OCTAL_LITERAL 
     : '8#' ('0'..'7')+
     ;
    
    fragment DIGIT
     : '0'..'9'
     ;
    
    SPACE 
     : (' ' | '\t' | '\r' | '\n')+ {skip();}
     ;
    

    Parsing the input "2#1111 8#77 0xff 16#ff 123" will result in the following AST:

    enter image description here

    Since you’ve lost the information about what type of Integer each literal is, you will have to do this check in the integer-rule (the -> {boolean-expression}? ... things after the rewrite rules).

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

Sidebar

Related Questions

I have a school assignment which consists of programming a scanner/lexical analyzer for a
I have written a script which calculates lexical diversity and a few other meaningful
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
have 2 questions : A computer with 32-bit address uses 2-level page table (9
I have a function that takes a list and replaces some elements. I have
I have a schema which I use XmlBeans to umarshall to Java objects. I
I have a let statement in which I would like to dynamically destructure a
I have a simple language that I'm using Flex(Lexical Analyzer), it's like this: /*

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.