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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:04:16+00:00 2026-05-30T20:04:16+00:00

I am using ANTLR to create an and/or parser+evaluator. Expressions will have the format

  • 0

I am using ANTLR to create an and/or parser+evaluator. Expressions will have the format like:

  • x eq 1 && y eq 10
  • (x lt 10 && x gt 1) OR x eq -1

I was reading this post on logic expressions in ANTLR Looking for advice on project. Parsing logical expression and I found the grammar posted there a good start:

grammar Logic;

parse
  :  expression EOF
  ;

expression
  :  implication
  ;

implication
  :  or ('->' or)*
  ;

or
  :  and ('&&' and)*
  ;

and
  :  not ('||' not)*
  ;

not
  :  '~' atom
  |  atom
  ;

atom
  :  ID
  |  '(' expression ')'
  ;

ID    : ('a'..'z' | 'A'..'Z')+;
Space : (' ' | '\t' | '\r' | '\n')+ {$channel=HIDDEN;};

However, while getting a tree from the parser works for expressions where the variables are just one character (ie, "(A || B) AND C", I am having a hard time adapting this to my case (in the example "x eq 1 && y eq 10" I’d expect one "AND" parent and two children, "x eq 1" and "y eq 10", see the test case below).

@Test
public void simpleAndEvaluation() throws RecognitionException{
    String src = "1 eq 1 && B";

    LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));
    LogicParser parser = new LogicParser(new CommonTokenStream(lexer));


    CommonTree tree = (CommonTree)parser.parse().getTree();

    assertEquals("&&",tree.getText());
    assertEquals("1 eq 1",tree.getChild(0).getText());
    assertEquals("a neq a",tree.getChild(1).getText());
}

I believe this is related with the "ID". What would the correct syntax be?

  • 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-30T20:04:17+00:00Added an answer on May 30, 2026 at 8:04 pm

    For those interested, I made some improvements in my grammar file (see bellow)

    Current limitations:

    • only works with &&/||, not AND/OR (not very problematic)

    • you can’t have spaces between the parenthesis and the &&/|| (I solve that by replacing ” (” with “)” and “) ” with “)” in the source String before feeding the lexer)

      grammar Logic;

      options {
        output = AST;
      }
      
      tokens {
        AND = '&&';
        OR  = '||';
        NOT = '~';
      }
      
      // parser/production rules start with a lower case letter
      parse
        :  expression EOF!    // omit the EOF token
        ;
      
      expression
        :  or
        ;
      
      or
        :  and (OR^ and)*    // make `||` the root
        ;
      
      and
        :  not (AND^ not)*      // make `&&` the root
        ;
      
      not
        :  NOT^ atom    // make `~` the root
        |  atom
        ;
      
      atom
        :  ID
        |  '('! expression ')'!    // omit both `(` and `)`
        ;
      
      // lexer/terminal rules start with an upper case letter
      ID
        :
          (
          'a'..'z'
          | 'A'..'Z'
          | '0'..'9' | ' '
          | SYMBOL
        )+ 
        ;
      
      SYMBOL
        :
          ('+'|'-'|'*'|'/'|'_')
       ;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

By default, a parser generated using ANTLR-3 extends from org.antlr.runtime.Parser. How do I have
I want to parser a simple matlab-like for-loop, using ANTLR. The loop is like
I'm using ANTLR 3 to create an AST. I want to have two AST
I'm trying to use ANTLR to create a parser for simple language using C#
I'm using the antlr-3.4-complete-no-antlrv2.jar version of ANTLR on Eclipse Indigo. I have installed the
I currently have a working, simple language implemented in Java using ANTLR. What I
I've managed to create a small project in Eclipse using the Spring & BlazeDS
What is the correct way to solve this problem in ANTLR: I have a
I have created a console app referencing http://www.antlr.org/download/CSharp using System; using Antlr.StringTemplate; using Antlr.StringTemplate.Language;
Hi I have a small problem in my ANTLR tree grammar. I am using

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.