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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:31:20+00:00 2026-05-15T23:31:20+00:00

I am trying to implement a nested comment in D. nestingBlockComment : ‘/+’ (options

  • 0

I am trying to implement a nested comment in D.

nestingBlockComment
 : '/+' (options {greedy=false;} :nestingBlockCommentCharacters)* '+/' {$channel=HIDDEN;}; // line 58

  nestingBlockCommentCharacters
   :  (nestingBlockComment| '/'~'+' | ~'/' ) ; //line 61

For me, it would be logical that this should work…

This is the error message I get:
[21:06:34] warning(200): d.g:58:64: Decision can match input such as "'+/'" using multiple alternatives: 1, 2
As a result, alternative(s) 1 were disabled for that input
[21:06:34] warning(200): d.g:61:7: Decision can match input such as "'/+'" using multiple alternatives: 1, 3
As a result, alternative(s) 3 were disabled for that input

Could someone explan those error messages to me and the fix?

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-15T23:31:21+00:00Added an answer on May 15, 2026 at 11:31 pm

    AFAIK, the error is because nestingBlockCommentCharacters can match +/ (the ~'/' twice).

    Personally, I’d keep the nestingBlockComment as a lexer rule instead of a parser rule. You can do that by adding a little helper method in the lexer class:

    public boolean openOrCloseCommentAhead() {
      // return true iff '/+' or '+/' is ahead in the character stream
    }
    

    and then in a lexer comment-rule, use a gated semantic predicates with that helper method as the boolean expression inside the predicate:

    // match nested comments
    Comment
      :  '/+' (Comment | {!openOrCloseCommentAhead()}?=> Any)* '+/'
      ;
    
    // match any character
    Any
      :  .
      ;
    

    A little demo-grammar:

    grammar DComments;
    
    @lexer::members {
      public boolean openOrCloseCommentAhead() {
        return (input.LA(1) == '+' && input.LA(2) == '/') ||
               (input.LA(1) == '/' && input.LA(2) == '+');
      }
    }
    
    parse
      :  token+ EOF
      ;
    
    token
      :  Comment {System.out.println("comment :: "+$Comment.text);}
      |  Any
      ;
    
    Comment
      :  '/+' (Comment | {!openOrCloseCommentAhead()}?=> Any)* '+/'
      ;
    
    Any
      :  .
      ;
    

    and a main class to test it:

    import org.antlr.runtime.*;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            ANTLRStringStream in = new ANTLRStringStream(
                "foo /+ comment /+ and +/ comment +/ bar /+ comment +/ baz");
            DCommentsLexer lexer = new DCommentsLexer(in);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            DCommentsParser parser = new DCommentsParser(tokens);
            parser.parse();
        }
    }
    

    Then the following commands:

    java -cp antlr-3.2.jar org.antlr.Tool DComments.g 
    javac -cp antlr-3.2.jar *.java
    java -cp .:antlr-3.2.jar Main
    

    (for Windows, the last command is: java -cp .;antlr-3.2.jar Main)

    produce the following output:

    comment :: /+ comment /+ and +/ comment +/
    comment :: /+ comment +/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement some nested loops that are spitting out good old nested html
I'm trying to implement a expression handling grammar (that deals with nested parenthesis and
Getting started with python. I am trying to implement positional index using nested dictionary.
I was trying to implement a nested tree structure in AngularJS. It consists of
I am trying to wrap my mind around the best way to implement nested
I'm trying to implement a Rails3 nested form with has-many through association. My model
I'm trying out Scala and I want to see how one would implement insertion
I'm trying to implement code for nested commenting which I found at http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/ .
I am trying to implement nested quotes in my BBcode class. But havent succeed
I am trying to implement the nested-set model for a sqlite database. So far

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.