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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:48:11+00:00 2026-05-23T14:48:11+00:00

I have a small custom scripting language, and I am trying to update it

  • 0

I have a small custom scripting language, and I am trying to update it to allow boolean expressions such as a > 2 and a > 2 and (b < 3 or c > 5). It’s the parenthetical expressions that I am having trouble with here.

Here is a (edited since the original post based on the answer from @Bart Kiers) full grammar that exhibits the problem. This is a pared-down version of my actual grammar, but the problem occurs here too.

grammar test;


options {
    language = 'JavaScript'; 
    output = AST;
} 


statement 
    :   value_assignment_statement  
        EOF
    ;


value_assignment_statement 
    :   IDENT
        '='
        expression                      
    ;

value_expression 
    :   value_list_expression           
    |   IDENT                           
    ;


value_list_expression 
    :   value_enumerated_list       
    ;


value_enumerated_list : '{' unary+ '}'
    ;



term 
    :   LPAREN expression RPAREN        
    |   INTEGER                         
    |   value_expression                
    ;

unary : ( '+' | '-' )* term
    ;

mult :  unary ( ('*' | '/') unary)*
    ;

expression : mult ( ('+' | '-') mult )*
    ;


boolean 
    :   boolean_expression
        EOF
    ;

boolean_expression
    :   boolean_or_expression
    ;

boolean_or_expression 
    :   boolean_and_expression (OR boolean_and_expression)*
    ;

boolean_and_expression 
    :   boolean_rel_expression (AND boolean_rel_expression)*
    ;

boolean_rel_expression
    :   boolean_neg_expression relational_operator boolean_neg_expression
    ;

boolean_neg_expression 
    :   (NOT)? atom
    ;

atom
    :   LPAREN boolean_expression RPAREN
    //| expression
    ;


relational_operator : '=' | '>' | '<';


LPAREN      :   '(';
RPAREN      :   ')';
AND         :   'and';
OR          :   'or';
NOT         :   'not';
IDENT       :   LETTER LETTER+;
INTEGER     :   DIGIT+;
WS          :   (' ' | '\n' | '\r' | '\t')+     { $channel = HIDDEN; };

fragment DIGIT      : '0'..'9';
fragment LETTER     : ('a'..'z' | 'A'..'Z');

My attempt to accommodate parenthetical boolean expressions such as a > 2 or (b < 3) is in the commented-out line in the atom rule. When I uncomment this line and include it in the grammar, ANTLR gives me this error:

[fatal] rule atom has non-LL(*) decision due to recursive rule invocations reachable from alts 1,2. Resolve by left-factoring or using syntactic predicates or using backtrack=true option.

I would like to address this by removing the recursion, but I can’t seem to make the transition from the Wikipedia description on how to remove left recursion to my own stuff.

In using this grammar, I want sometimes to use statement as a root with input such as abc = 2 + 3, which assigns a value to a variable named abc. Other times I want to use the grammar to evaluate an expression with boolean as the root with input such as abc > 3 and (xyz < 5 or xyz > 10). When I tried to use @Bart’s answer as a model, it worked fine until I tried to merge the parts of the grammar used by statement with the parts used by boolean. They should both be able to use an expression, but that’s where I’m stuck with this left recursion error.

So, how can I both handle parentheses and avoid the left recursion problem?

  • 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-23T14:48:11+00:00Added an answer on May 23, 2026 at 2:48 pm

    Boolean expressions are just the same as the additive- and multiplicative expression, and should therefore not be separated from them. Here’s how to account for all types of expressions:

    grammar test;
    
    parse
      :  expression EOF
      ;
    
    expression 
      :  or
      ;
    
    or
      :  and (OR and)*
      ;
    
    and
      :  rel (AND rel)*
      ;
    
    rel
      :  add (('=' | '>' | '<') add)*
      ;
    
    add
      :  mult (('+' | '-') mult)*
      ;
    
    mult
      :  unary (('*' | '/') unary)*
      ;
    
    unary 
      :  '-' term
      |  '+' term
      |  NOT term
      |  term
      ;
    
    term 
      :  INTEGER  
      |  IDENT       
      |  list
      |  '(' expression ')'
      ;
    
    list 
      :  '{' (expression (',' expression)*)? '}'
      ;
    
    AND     :  'and';
    OR      :  'or';
    NOT     :  'not';
    IDENT   :  LETTER LETTER*;
    INTEGER :  DIGIT+;
    WS      :  (' ' | '\n' | '\r' | '\t')+  { $channel = HIDDEN; };
    
    fragment DIGIT   : '0'..'9';
    fragment LETTER  : ('a'..'z' | 'A'..'Z');
    

    which will parse the example input:

    abc > 3 and (xyz < 5 or xyz > {1, 2, 3})
    

    into the following parse tree:

    enter image description here

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

Sidebar

Related Questions

I have created a small flash CS4 project that has a few custom components
I have small web app that generate PDF files as a report. I'm trying
In my app I have a small custom button that just shows an FB
I have a small code that allows me to add custom name to a
I have a small, custom crafted HTML report, not using any reporting engine, for
I have small utility that does some processing on a file and changes the
I am trying to create a small custom snippet for my C# code in
I have a small Java Swing application that I want to rewrite in WPF
So I have a small issue with converting a string to a boolean when
I have written a small custom web server application in C running on Linux.

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.