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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:05:31+00:00 2026-06-04T08:05:31+00:00

I have a small interrogation concerning my grammar. I want to parse strings, like

  • 0

I have a small interrogation concerning my grammar.
I want to parse strings, like the following :

 "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')"

I ended up with the following grammar (a bit more complex than needed I know) :

// Aiming at parsing a complete BQS formed Query

grammar Logic;

options {
    output=AST;
}

tokens {
  NOT_LIKE;
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/
 // precedence order is (low to high): or, and, not, [comp_op, geo_op, rel_geo_op, like, not like, exists], ()
 parse  
    : expression EOF -> expression
    ; // ommit the EOF token

 expression
    : query
    ;       

 query  
    : term (OR^ term)*    // make `or` the root
    ;

 term   
    : factor (AND^ factor)*
    ;

 factor
  :  (notexp -> notexp) ( NOT LIKE e=notexp  -> ^(NOT_LIKE $factor $e))?
  ;

 notexp
  :  NOT^ like
  |  like
  ;

 like // this one has to be completed (a lot)
    : atom (LIKE^ atom)*
    ;


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

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/
// GENERAL OPERATORS: 
//NOTLIKE   :   'notlike' | 'NOTLIKE'; // whitespaces have been removed
LIKE    :   'like' | 'LIKE';

OR          :   'or' | 'OR';
AND         :   'and' | 'AND';
NOT         :   'not' | 'NOT';

//ELEMENTS 
CONSTANT_EXPRESSION : DATE | NUMBER | QUOTED_STRING;    
ID          :   (CHARACTER|DIGIT)+; 

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

fragment DATE       :   '\'' YEAR '/' MONTH '/' DAY (' ' HOUR ':' MINUTE ':' SECOND)? '\'';

fragment QUOTED_STRING :    '\'' (CHARACTER)+ '\'' ; 

//UNITS
fragment CHARACTER :    ('a'..'z' | 'A'..'Z'|'.'|'\''|'%'); // FIXME: Careful, should be all ASCII
fragment DIGIT  :   '0'..'9' ;
fragment DIGIT_SEQ  :(DIGIT)+;
fragment DEL    :   SPACE ',' SPACE ; //Delimiter + may be space behind
fragment NUMBER :   (SIGN)? DIGIT_SEQ ('.' (DIGIT_SEQ)?)?; // should be given in decimal degrees, North is 0 and direction is clockwise, range is 0 to 360
fragment SIGN   :   '+' | '-';
fragment YEAR   :   DIGIT DIGIT DIGIT DIGIT;
fragment MONTH  :   DIGIT DIGIT;
fragment DAY    :   DIGIT DIGIT;
fragment HOUR   :   DIGIT DIGIT;
fragment MINUTE :   DIGIT DIGIT;
fragment SECOND :   DIGIT (DIGIT)? ('.' (DIGIT)+)?;

fragment SPACE : (' ')?;// used to increase compatibility

Thing is, I have this message when creating the AST :

line 1:11 no viable alternative at input ''%bridge%''
line 1:35 no viable alternative at input ''%Munich%''

The generated tree is though correct (as far as I’m concerned at least):

antlr viable ast tree

So, could anyone give me a hint about what’s wrong in there ? I think character contains all extra characters needed to correclty parse this expression. . .

Thanks !

As usual, some Java code to quickly test the grammar :

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
  public static void main(String[] args) throws Exception {

    // the expression
    String src = "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')";

    // create a lexer & parser
    //LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));
    //LogicParser parser = new LogicParser(new CommonTokenStream(lexer));

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

    // invoke the entry point of the parser (the parse() method) and get the AST
    CommonTree tree = (CommonTree)parser.parse().getTree();

    // print the DOT representation of the AST 
    DOTTreeGenerator gen = new DOTTreeGenerator();
    StringTemplate st = gen.toDOT(tree);
    System.out.println(st);
  }
}
  • 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-04T08:05:33+00:00Added an answer on June 4, 2026 at 8:05 am

    I see 3 issues:

    1

    Your atom rule matches epsilon (nothing):

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

    (note the “nothingness” inside | |)

    causing your grammar to be ambiguous. I guess it should be:

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

    2

    Your fragment CHARACTER matches a single quote while this single quote also denotes the end of the fragment QUOTED_STRING.

    I guess CHARACTER should be this instead:

    fragment CHARACTER : ('a'..'z' | 'A'..'Z' | '.' | '%'); 
    

    3

    Nowhere in your parser rule you match the token CONSTANT_EXPRESSION, so the AST you posted could never have been created by a parser generated from the grammar you posted. I presume you’d want to match it in the atom rule like this:

    atom   
     : ID 
     | CONSTANT_EXPRESSION
     | '(' expression ')' -> expression
     ;
    

    With the changes outlined above, I get the following AST without any errors being printed to the console:

    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 small library i want to use for creating games. First, i tried
BACKGROUND: I would like to have small labels in columns of a table. I'm
I have small problem. What I want to achieve is adding sum of values
I have small question regarding the following error: this.context.sourceCache On a custom control I
I have small problem with my .net 2.0 winforms application. I want to embed
i have small class like public static class TSM { static string TokenID =
I have small doubt in CoVariance and ContraVariance.. See the Following Code.. interface IGetElement<out
I have small problem with mysql. I have data in table and I want
I have small CGI script running on a server[Linux OS]. following is a part
I want to have small plus or minus button before each row in a

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.