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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:20:11+00:00 2026-06-18T17:20:11+00:00

I am using ANTLR 3 to do the below. Assume I have an SQL

  • 0

I am using ANTLR 3 to do the below.

Assume I have an SQL query. I know that in general it’s WHERE, ORDER BY and GROUP BY clauses are optional. In terms of ANTLR’s grammar I would describe that like this:

query : select_clause from_clause where_clause? group_by_clause? order_by_clause?

The rule for each clause will obviously start with the respective keyword.

What I actually need is to extract each clause’s contents as a string without dealing with its internal structure.

To do this I started with the following grammar:

query :
    select_clause from_clause where_clause? group_by_clause? order_by_clause?
EOF;

select_clause :
    SELECT_CLAUSE
;

from_clause :
    FROM_CLAUSE
;

where_clause :
    WHERE_CLAUSE
;

group_by_clause :
    GROUP_BY_CLAUSE
;

order_by_clause :
    ORDER_BY_CLAUSE
;

SELECT_CLAUSE : 'select' ANY_CHAR*;

FROM_CLAUSE : 'from' ANY_CHAR*;

WHERE_CLAUSE : 'where' ANY_CHAR*;

GROUP_BY_CLAUSE : 'group by' ANY_CHAR*;

ORDER_BY_CLAUSE : 'order by' ANY_CHAR*;

ANY_CHAR : .;

WS : ' '+ {skip();};

This one didn’t work. I have had further attempts composing a correct grammar with no success. I suspect this task is doable with ANTLR3 but I am just missing smth.

More generally, I would like to be able to collect chars from the input stream into a single token until meeting a specific keyword that would indicate the beginning of a new token. This keyword should be the part of the new token.

Can you help me please?

  • 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-18T17:20:12+00:00Added an answer on June 18, 2026 at 5:20 pm

    Instead of adding them to your tokens, why not move the ANY_CHAR* into parser rules instead? You could even “glue” these single tokens together using a rewrite rule.

    A quick demo:

    grammar T;
    
    options { output=AST; }
    tokens  { QUERY; ANY; }
    
    query           : select_clause from_clause where_clause? group_by_clause? order_by_clause? EOF
                      -> ^(QUERY select_clause from_clause where_clause? group_by_clause? order_by_clause?)
                    ;
    select_clause   : SELECT_CLAUSE^ any;
    from_clause     : FROM_CLAUSE^ any;
    where_clause    : WHERE_CLAUSE^ any;
    group_by_clause : GROUP_BY_CLAUSE^ any;
    order_by_clause : ORDER_BY_CLAUSE^ any;
    any             : ANY_CHAR* -> ANY[$text];
    
    SELECT_CLAUSE   : 'select';
    FROM_CLAUSE     : 'from';
    WHERE_CLAUSE    : 'where';
    GROUP_BY_CLAUSE : 'group' S+ 'by';
    ORDER_BY_CLAUSE : 'order' S+ 'by';
    ANY_CHAR        : . ;
    WS              : S+ {skip();};
    
    fragment S      : ' ' | '\t' | '\r' | '\n';
    

    If you now parse the input:

    select JUST ABOUT ANYTHING from YOUR BASEMENT order by WHATEVER

    the following AST would be created:

    enter image description here

    Trying to do something similar in your lexer would be messy, and would mean some custom code (or predicates) to check for keywords up ahead in the char-stream (both not pretty!).

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

Sidebar

Related Questions

I'm using antlr to analyse and re-write sql query. I have: select : SELECT
I am using ANTLR 3 , I have a question is that How can
I am creating a parser using ANTLR 3.x that targets java. I have written
I have a simple grammar defined below using Antlr 3: grammar i; @header {
By default, a parser generated using ANTLR-3 extends from org.antlr.runtime.Parser. How do I have
I currently have a working, simple language implemented in Java using ANTLR. What I
I am using ANTLR to create an and/or parser+evaluator. Expressions will have the format
I'm using ANTLR 3 to create an AST. I want to have two AST
I would like to parse a string of French dates using Antlr. I have
I have created a console app referencing http://www.antlr.org/download/CSharp using System; using Antlr.StringTemplate; using Antlr.StringTemplate.Language;

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.