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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:02:03+00:00 2026-06-07T01:02:03+00:00

Background I have been using ANTLRWorks (V 1.4.3) for a few days now and

  • 0

Background

I have been using ANTLRWorks (V 1.4.3) for a few days now and trying to write a simple Boolean parser. The combined lexer/parser grammar below works well for most of the requirements including support for quoted white-spaced text as operands for a Boolean expression.


Problem

I would like the grammar to work for white-spaced operands without the need of quotes.


Example

For example, expression-

“left right” AND center

should have the same parse tree even after dropping the quotes-

left right AND center.

I have been learning about backtracking, predicates etc but can’t seem to find a solution.


Code

Below is the grammar I have got so far. Any feedback on the foolish mistakes is appreciated :).

Lexer/Parser Grammar

grammar boolean_expr;

options {
    TokenLabelType=CommonToken;
    output=AST;
    ASTLabelType=CommonTree;
}

@modifier{public}
@ctorModifier{public}

@lexer::namespace{Org.CSharp.Parsers}
@parser::namespace{Org.CSharp.Parsers}

public
evaluator 
    : expr EOF 
    ;

public
expr 
    : orexpr 
    ;

public  
orexpr 
    : andexpr (OR^ andexpr)* 
    ;

public
andexpr 
    : notexpr (AND^ notexpr)* 
    ;

public
notexpr 
    : (NOT^)? atom 
    ;

public
atom 
    : word | LPAREN! expr RPAREN! 
    ;

public
word  
    :  QUOTED_TEXT | TEXT 
    ;

/*
 * Lexer Rules
 */

LPAREN 
    : '(' 
    ;

RPAREN 
    : ')' 
    ;

AND 
    : 'AND'
    ;

OR 
    : 'OR'
    ;

NOT
    : 'NOT'
    ;

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

QUOTED_TEXT  
    : '"' (LETTER | DIGIT | ' ' | ',' | '-')+ '"'
    ;

TEXT
    : (LETTER | DIGIT)+ 
    ;

/*
Fragment lexer rules can be used by other lexer rules, but do not return tokens by themselves
*/
fragment DIGIT  
    :   ('0'..'9') 
    ; 

fragment LOWER  
    :   ('a'..'z') 
    ; 

fragment UPPER  
    :   ('A'..'Z') 
    ; 

fragment LETTER 
    :   LOWER | UPPER 
    ; 
  • 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-06-07T01:02:04+00:00Added an answer on June 7, 2026 at 1:02 am

    Simply let TEXT in your atom rule match once or more: TEXT+. When it matches a TEXT token more than once, you’ll also want to create a custom root node for these TEXT tokens (I added an imaginary token called WORD in the grammar below).

    grammar boolean_expr;
    
    options {
        output=AST;
    }
    
    tokens {
      WORD;
    }
    
    evaluator 
        : expr EOF 
        ;
    
    ...
    
    word  
        : QUOTED_TEXT 
        | TEXT+       -> ^(WORD TEXT+)
        ;
    
    ...
    

    Your input "left right AND center" would now be parsed as follows:

    enter image description here

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

Sidebar

Related Questions

Some Background I have been using Git for a while now. The projects that
Background I have been now using Entity Framework + Linq for about year and
I come from a Java background and have been using C# for the last
Background : I have been trying to display a sentence with Bold and Italic
Background : I have a few different threads which each need to write to
I have been using WSP builder up until now with VS 2008. I recently
Have been using a simple CSS only tooltip. Working Example css: .tip { position:relative;
I have been using supersized jQuery for the slideshow background of my website. I
Background I have been using the Authorize.net SDK in an Eclipse project of it's
This is some background to my problem... I have been using 3rd Party as

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.