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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:11:03+00:00 2026-06-09T15:11:03+00:00

i’m working on a sql parser to allow our programmers to create a mysql

  • 0

i’m working on a sql parser to allow our programmers to create a mysql like query which is then automatically translated to mssql or mysql queries, depending on what you want. This to allow flexible transitions between mysql and mssql databases.

I currently have what I set out to create except that there is one problem.

If i run a query like this:

SELECT * FROM `x`.`y` WHERE `y`.`b` = 'hello'

the character string is seen as a identifier llo

This is my grammer

grammar Query;

query
    : (select_stmt|update_stmt|delete_stmt|insert_stmt|upsert_stmt) ';'? EOF
    ;

select_stmt
    : select 'FROM' select_table_exp where? groupby? orderby? limit?
    ;

update_stmt
    : 'UPDATE' table_exp set where?
    ;

delete_stmt
    : 'DELETE FROM ' table_exp where    
    ;

insert_stmt
    : 'INSERT INTO' table_exp fields values
    ;

upsert_stmt
    : 'UPSERT' table_exp set ?  
    ;

values
    : 'VALUES (' valuelist ')'
    ;

valuelist
    : value (',' valuelist)?where
    ;

fields
    : '(' groupbylist ')'
    ;

set
    : 'SET' (setlist)
    ;

setlist
    : setters (',' setlist)?
    ;

select
    : 'SELECT' fieldlist
    ;

where
    : 'WHERE' where_stmt 
    ;

limit
    : 'LIMIT' (INT|param) (',' (INT|param))?
    ;

groupby
    : 'GROUP BY' groupbylist    
    ;

orderby
    : 'ORDER BY' orderbylist
    ;

select_table_exp
    : table_exp (join_stmt)?    
    | '(' select_stmt ')' as
    ;

table_exp
    : identifier as?
    ;

fieldlist
    : 
      (
          function as
        | (identifier as?)
        | identifier_prefix? '*' // solve the problem of the `ID`.* problem
        | '(' select_stmt ')' as
      ) (',' fieldlist)?
    ;

function
    : 'COUNT(' (identifier|'*') ')' 
    | 'MAX(' identifier ')'
    | 'MIN(' identifier ')'
    | 'AVERAGE(' identifier ')'
    | 'CONCAT(' groupbylist ')'
    | 'CONCAT_WS(' CHAR ',' groupbylist ')' 
    ;

orderbylist
    : identifier ('ASC'|'DESC')? (',' orderbylist)? 
    ;

groupbylist
    : identifier (',' groupbylist)? 
    ;

as
    : 'AS' (ID | '`' ID '`')    
    ;

join_stmt
    : leftjoin (join_stmt)?
    | rightjoin (join_stmt)?
    | join (join_stmt)?
    ;

leftjoin
    : 'LEFT JOIN' (table_exp|('(' select_stmt ')' as)) on_stmt
    ;

rightjoin
    : 'RIGHT JOIN' (table_exp|('(' select_stmt ')' as)) on_stmt
    ;

join
    : 'JOIN' (table_exp|('(' select_stmt ')' as)) on_stmt
    ;

on_stmt
    : 'ON' where_stmt
    ;

where_stmt
    : where_exp ((BOOLEANAND|BOOLEANOR) where_stmt)?
    ;

where_exp
    : where_exp_identifier
    | value (WHEREOPERATORS|EQUALITYOPERATOR) identifier
    | '(' select_stmt ')' (WHEREOPERATORS|EQUALITYOPERATOR) identifier
    ;

where_exp_identifier
    : identifier (WHEREOPERATORS|EQUALITYOPERATOR) where_exp_identifier_operator_right
    | identifier 'BETWEEN' (numeric_value BOOLEANAND numeric_value)
    | identifier 'LIKE' string_value
    ;

where_exp_identifier_operator_right
    : value
    | identifier
    | '(' select_stmt ')'
    ;

setters
    : identifier EQUALITYOPERATOR value
    | value EQUALITYOPERATOR identifier
    | identifier EQUALITYOPERATOR '(' select_stmt ')'
    | '(' select_stmt ')' EQUALITYOPERATOR identifier
    ;

identifier_value
    : value
    | identifier
    ;

value
    : string_value
    | numeric_value
    | param
    ;

string_value
    : STRING
    | CHAR
    ;

numeric_value
    : INT
    | FLOAT
    ;

identifier_prefix
    : ID '.'
    | '`' ID '`.'   
    ;

identifier
    : identifier_prefix? ID 
    | identifier_prefix? '`' ID '`'
    ;

param
    : '@' ID
    ;

BOOLEANAND
    : 'AND'
    ;

BOOLEANOR
    : 'OR'
    ;

EQUALITYOPERATOR
    : '='
    ;

WHEREOPERATORS
    : ('!='|'<>'|'<'|'>'|'<='|'>=')
    ;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

INT :   '0'..'9'+
    ;

FLOAT
    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   '.' ('0'..'9')+ EXPONENT?
    |   ('0'..'9')+ EXPONENT
    ;

COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

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

STRING
    :  '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
    ;

CHAR:  '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
    ;

fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;

I’m far from an ANTLR expert so after 3 hours of fiddling with my grammar to try and get the grammar to accept a character string as correct at that position I decided to ask you guys for some help. Maybe some antlr guru here can help me sort this out.

Many thanks

  • 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-09T15:11:05+00:00Added an answer on June 9, 2026 at 3:11 pm

    Your input 'hello' is not being tokenized as a string because you defined a string to be surrounded by double quotes. A char is surrounded by single quotes in your grammar:

    STRING
        :  '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
        ;
    
    CHAR:  '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
        ;
    

    That is the cause of the error.

    • 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 would like to run a str_replace or preg_replace which looks for certain words
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.