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

  • Home
  • SEARCH
  • 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 4604814
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:20:51+00:00 2026-05-22T00:20:51+00:00

I’m using ANTLRWorks 1.4.2 to create a simple grammar for the purpose of evaluating

  • 0

I’m using ANTLRWorks 1.4.2 to create a simple grammar for the purpose of evaluating an user-provided expression as boolean result. This ultimately will be part of a larger grammar, but I have some questions about this current fragment. I want users to be able to use expressions such as:

  1. 2 > 1
  2. 2 > 1 and 3 < 1
  3. (2 > 1 or 1 < 3) and 4 > 1
  4. (2 > 1 or 1 < 3) and (4 > 1 or (2 < 1 and 3 > 1))

The first two expressions are legal in my grammar, but the last two are not, and I am not sure why. Also, ANTLRworks seems to suggest that input such as ((((1 > 2) with mismatched parentheses is legal, and I am not sure why. So, I seem to be missing out on some insight into the right way to handle parenthetical grouping in a grammar.

How can I change my grammar to properly handle parentheses?

My grammar is below:

grammar conditional_test;

boolean
    :   boolean_value_expression
        EOF
    ;

boolean_value_expression
    :   boolean_term (OR boolean_term)*
        EOF
    ;

boolean_term
    :   boolean_factor (AND boolean_factor)*
    ;

boolean_factor
    :   (NOT)?  boolean_test
    ;

boolean_test
    :   predicate
    ;

predicate
    :   expression relational_operator expression
    |   LPAREN boolean_value_expression RPAREN
    ;

relational_operator
    :   EQ
    |   LT
    |   GT
    ;   

expression
    :   NUMBER
    ;


LPAREN      :   '(';
RPAREN      :   ')';
NUMBER      :   '0'..'9'+;

EQ          :   '=';
GT          :   '>';
LT          :   '<';

AND         :   'and';
OR          :   'or' ;
NOT         :   'not';
  • 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-22T00:20:51+00:00Added an answer on May 22, 2026 at 12:20 am

    Chris Farmer wrote:

    The first two expressions are legal in my grammar, but the last two are not, and I am not sure why. …

    You should remove the EOF token from:

    boolean_value_expression
        :   boolean_term (OR boolean_term)*
            EOF
        ;
    

    You normally only use the EOF after the entry point of your grammar (boolean in your case). Be careful boolean is a reserved word in Java and can therefor not be used as a parser rule!

    So the first two rules should look like:

    bool
        :   boolean_value_expression
            EOF
        ;
    
    boolean_value_expression
        :   boolean_term (OR boolean_term)*
        ;
    

    And you may also want to ignore literal spaces by adding the following lexer rule:

    SPACE : ' ' {$channel=HIDDEN;};
    

    (you can include tabs an line breaks, of course)

    Now all of your example input matches properly (tested with ANTLRWorks 1.4.2 as well).

    Chris Farmer wrote:

    Also, ANTLRworks seems to suggest that input such as ((((1 > 2) with mismatched parentheses is legal, …

    No, ANTLRWorks does produce errors, perhaps not very noticeable ones. The parse tree ANTLRWorks produces has a NoViableAltException as a leaf, and there are some errors on the “Console” tab.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.