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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:30:54+00:00 2026-05-27T19:30:54+00:00

I’m working on a parser for the language D and I ran in to

  • 0

I’m working on a parser for the language D and I ran in to trouble when I tried to add the “slice” operator rule. You can find the ANTLR grammar for it here.
Basically the problem is that if the lexer encounters a string like this: “1..2” it gets completely lost, and it ends up being as a single float value and therefore the postfixExpression rule for a string like “a[10..11]” ends up being a ExpArrIndex object with a ExpLiteralReal argument. Can somebody explain what is exactly wrong with the numeric literals? (as far as I understand it fails somewhere around these tokens)

  • 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-27T19:30:55+00:00Added an answer on May 27, 2026 at 7:30 pm

    You can do that by emitting two tokens (an Int and Range token) when you encounter a ".." inside a float rule. You need to override two methods in your lexer to accomplish this.

    A demo with a small part of your Dee grammar:

    grammar Dee;
    
    @lexer::members {
    
      java.util.Queue<Token> tokens = new java.util.LinkedList<Token>();
    
      public void offer(int ttype, String ttext) {
        this.emit(new CommonToken(ttype, ttext));
      }
    
      @Override
      public void emit(Token t) {
        state.token = t;
        tokens.offer(t);
      }
    
      @Override
      public Token nextToken() {
        super.nextToken();
        return tokens.isEmpty() ? Token.EOF_TOKEN : tokens.poll();
      }
    }
    parse
     : (t=. {System.out.printf("\%-15s '\%s'\n", tokenNames[$t.type], $t.text);})* EOF
     ;
    
    Range
     : '..'
     ;
    
    IntegerLiteral 
     : Integer IntSuffix?
     ;
    
    FloatLiteral 
     : Float ImaginarySuffix? 
     ;
    
    // skipping
    Space
     : ' ' {skip();}
     ;
    
    // fragments
    fragment Float
     : d=DecimalDigits ( options {greedy = true; } : FloatTypeSuffix 
                       | '..' {offer(IntegerLiteral, $d.text); offer(Range, "..");}
                       | '.' DecimalDigits DecimalExponent?
                       )
     | '.' DecimalDigits DecimalExponent?
     ;
    
    fragment DecimalExponent : 'e' | 'E' | 'e+' | 'E+' | 'e-' | 'E-' DecimalDigits;
    fragment DecimalDigits   : ('0'..'9'|'_')+ ;
    fragment FloatTypeSuffix : 'f' | 'F' | 'L';
    fragment ImaginarySuffix : 'i';
    fragment IntSuffix       : 'L'|'u'|'U'|'Lu'|'LU'|'uL'|'UL' ;
    fragment Integer         : Decimal| Binary| Octal| Hexadecimal ;
    fragment Decimal         : '0' | '1'..'9' (DecimalDigit | '_')* ;
    fragment Binary          : ('0b' | '0B') ('0' | '1' | '_')+ ;
    fragment Octal           : '0' (OctalDigit | '_')+ ;
    fragment Hexadecimal     : ('0x' | '0X') (HexDigit | '_')+;  
    fragment DecimalDigit    : '0'..'9' ;
    fragment OctalDigit      : '0'..'7' ;
    fragment HexDigit        : ('0'..'9'|'a'..'f'|'A'..'F') ;
    

    Test the grammar with the class:

    import org.antlr.runtime.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        DeeLexer lexer = new DeeLexer(new ANTLRStringStream("1..2 .. 33.33 ..21.0"));
        DeeParser parser = new DeeParser(new CommonTokenStream(lexer));
        parser.parse();
      }
    }
    

    And when you run Main, the following output is produced:

    IntegerLiteral  '1'
    Range           '..'
    IntegerLiteral  '2'
    Range           '..'
    FloatLiteral    '33.33'
    Range           '..'
    FloatLiteral    '21.0'
    

    EDIT

    Yeah, as you indicated in the comments, a lexer rule can only emit 1 single token. But, as you yourself already tried, semantic predicates can indeed be used to force the lexer to look ahead in the char-stream to ensure there is actually a ".." after an IntegerLiteral token before trying to match a FloatLiteral.

    The following grammar would produce the same tokens as the first demo.

    grammar Dee;
    
    parse
     : (t=. {System.out.printf("\%-15s '\%s'\n", tokenNames[$t.type], $t.text);})* EOF
     ;
    
    Range
     : '..'
     ;
    
    Number
     : (IntegerLiteral Range)=> IntegerLiteral {$type=IntegerLiteral;}
     | (FloatLiteral)=>         FloatLiteral   {$type=FloatLiteral;}
     |                          IntegerLiteral {$type=IntegerLiteral;}
     ;
    
    // skipping
    Space
     : ' ' {skip();}
     ;
    
    // fragments
    fragment DecimalExponent : 'e' | 'E' | 'e+' | 'E+' | 'e-' | 'E-' DecimalDigits;
    fragment DecimalDigits   : ('0'..'9'|'_')+ ;
    fragment FloatLiteral    : Float ImaginarySuffix?;
    fragment IntegerLiteral  : Integer IntSuffix?;
    fragment FloatTypeSuffix : 'f' | 'F' | 'L';
    fragment ImaginarySuffix : 'i';
    fragment IntSuffix       : 'L'|'u'|'U'|'Lu'|'LU'|'uL'|'UL' ;
    fragment Integer         : Decimal| Binary| Octal| Hexadecimal ;
    fragment Decimal         : '0' | '1'..'9' (DecimalDigit | '_')* ;
    fragment Binary          : ('0b' | '0B') ('0' | '1' | '_')+ ;
    fragment Octal           : '0' (OctalDigit | '_')+ ;
    fragment Hexadecimal     : ('0x' | '0X') (HexDigit | '_')+;  
    fragment DecimalDigit    : '0'..'9' ;
    fragment OctalDigit      : '0'..'7' ;
    fragment HexDigit        : ('0'..'9'|'a'..'f'|'A'..'F') ;
    fragment Float
     : d=DecimalDigits ( options {greedy = true; } : FloatTypeSuffix 
                       | '.' DecimalDigits DecimalExponent?
                       )
     | '.' DecimalDigits DecimalExponent?
     ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.