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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:58:50+00:00 2026-05-24T04:58:50+00:00

The grammar below parses ( left part = right part # comment ) ,

  • 0

The grammar below parses ( left part = right part # comment ), # comment is optional.

Two questions:

  1. Sometimes warning (ANTLRWorks 1.4.2):
    Decision can match input such as “{Int, Word}” using multiple alternatives: 1, 2 (referencing id2)
    But only sometimes!
  2. The next extension should be that the comment (id2) can contain chars ‘(‘ and ‘)’.

The grammar:

grammar NestedBrackets1a1;

//==========================================================
// Lexer Rules
//==========================================================

Int
  :  Digit+
  ;

fragment Digit
  :  '0'..'9'
  ;

Special
  : ( TCUnderscore | TCQuote )
  ;
TCListStart   : '(' ; 
TCListEnd     : ')' ;   
fragment TCUnderscore  : '_' ;
fragment TCQuote       : '"' ;

// A word must start with a letter
Word
  :  ( 'a'..'z' | 'A'..'Z' | Special ) ('a'..'z' | 'A'..'Z' | Special | Digit )*
  ;

Space
  :  ( ' ' | '\t' | '\r' | '\n' ) { $channel = HIDDEN; }
  ;

//==========================================================
// Parser Rules
//==========================================================

assignment
  :  TCListStart id1 '=' id1 ( comment )? TCListEnd
  ;

id1
  :  Word+
  ;

comment
  : '#' ( id2 )*
  ;

id2
  :  ( Word | Int )+
  ;
  • 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-24T04:58:50+00:00Added an answer on May 24, 2026 at 4:58 am

    ANTLRStarter wrote:

    Sometimes warning (ANTLRWorks 1.4.2): Decision can match input such as “{Int, Word}” using multiple alternatives: 1, 2 (referencing id2)
    But only sometimes!

    No, the grammar you posted will always produce this warning. Perhaps you don’t always notice it (your IDE-plugin or ANTLRWorks might show it in a tab you don’t have opened), but the warning is there. Convince yourself by creating a lexer/parser from the command line:

    java -cp antlr-3.4-complete.jar org.antlr.Tool NestedBrackets1a1.g
    

    will produce:

    warning(200): NestedBrackets1a1.g:49:19:
    Decision can match input such as "{Int, Word}" using multiple alternatives: 1, 2
    
    As a result, alternative(s) 2 were disabled for that input
    

    This is because you have a * after ( id2 ) inside your comment rule, and id2 also is a repetition of tokens: ( Word | Int )+. Let’s say your input is "# foo bar" (a # followed by two Word tokens). ANTLR can now parse the input in more than 1 way: the 2 tokens "foo" and "bar" could be matched by ( id2 )*, where id2 matches a single Word token at a time, but "foo" and "bar" could also be matches in one go of the id2 rule.

    Look at the merged rules:

    comment
      :  '#' ( ( Word | Int )+ )*
      ;
    

    See how you’re repeating a repetition: ( ( ... )+ )*? This is usually a problem, as it is in your case.

    Resolve this problem by either replacing the * with a ?:

    comment
      :  '#' ( id2 )?
      ;
    
    id2
      :  ( Word | Int )+
      ;
    

    or by removing the +:

    comment
      :  '#' ( id2 )*
      ;
    
    id2
      :  ( Word | Int )
      ;
    

    ANTLRStarter wrote:

    The next extension should be that the comment (id2) can contain chars ‘(‘ and ‘)’.

    That is asking for trouble since a comment is followed by a TCListEnd, which is a ). I don’t recommend letting a comment match ).

    EDIT

    Note that comments are usually stripped from the source file while tokenizing the input source. That way you don’t need to account for them in your parser rules. You can do that by “skipping” these tokens in a lexer rule:

    Comment
      :  '#' ~('\r' | '\n')* {skip();}
      ; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my grammar with antlrworks, I can get noviablealtexception for rules like if, while
the 'expr' rule in the ANTLR grammar below obviously mutually left-recursive. As a ANTLR
AntlrWorks says that input {'AND','OR'..'XOR'} can be matched by two alternatives. Even with the
I have to define the grammar of a file like the one shown below.
Throughout a Bison grammar I am using right recursion, and I have read that
I have an ANTLR grammar file as part of a C# project file and
Lets say the same grammar is not LR(1), can we safely say that the
I have this simple grammar for a C# like syntax. I can't figure out
When I execute the below grammar with the input (3), I obtain this parse
I've written the below grammar for ANTLR parser and lexer for building trees for

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.