Missing K_SELECT

41 views
Skip to first unread message

Bing Yuan

unread,
Feb 9, 2016, 2:19:07 AM2/9/16
to antlr-discussion
Hi, 

I'm new to antlr4. I'm creating a parser to support SQL-like query for some POJOs.

Here is the grammar file:

grammar SimpleQuery;

simple_query : K_SELECT field_list (K_WHERE expr)?;

field_list
: '*'
| field_name (',' field_name)*;

field_name
: simple_field_name
| simple_field_name ('.' simple_field_name)+
;

simple_field_name : ID;

expr
: literal_value
| field_name
| unary_operator expr
| expr '||' expr
| expr ( '<' | '<=' | '>' | '>=' ) expr
| expr ( '=' | '==' | '!=' | '<>' | K_HAS | K_STARTWITH) expr
| expr K_AND expr
| expr K_OR expr
| '(' expr ')'
;

literal_value
: NUMERIC_LITERAL
| STRING_LITERAL
| K_NULL
;

unary_operator:
'-'
| '+'
| '~'
| '!'
| K_NOT
;

ID : [a-zA-Z_] [a-zA-Z_0-9]*;

NUMERIC_LITERAL : DIGIT+('.'DIGIT)?;
STRING_LITERAL : ('\'' | '\'\'')*(ID)('\'' | '\'\'')*;

SPACES : [ \u000B\t\r\n] -> skip;

K_SELECT : S E L E C T;
K_WHERE : W H E R E;
K_HAS : H A S;
K_STARTWITH : S T A R T W I T H;
K_AND : A N D;
K_OR : O R;
K_NOT : N O T;
K_NULL : N U L L;

fragment DIGIT : [0-9];

fragment A : [aA];
fragment B : [bB];
fragment C : [cC];
fragment D : [dD];
fragment E : [eE];
fragment F : [fF];
fragment G : [gG];
fragment H : [hH];
fragment I : [iI];
fragment J : [jJ];
fragment K : [kK];
fragment L : [lL];
fragment M : [mM];
fragment N : [nN];
fragment O : [oO];
fragment P : [pP];
fragment Q : [qQ];
fragment R : [rR];
fragment S : [sS];
fragment T : [tT];
fragment U : [uU];
fragment V : [vV];
fragment W : [wW];
fragment X : [xX];
fragment Y : [yY];
fragment Z : [zZ];

And when I tested the simple_query rule with a simple statement "Select firstName", I got this error saying missing K_SELECT. It appeared that "Select" was identified as "fieldName" (screenshot attached). 

Anything wrong with the grammar? 

Thanks,

Bing


Screen Shot 2016-02-08 at 11.15.52 PM.png

Norman Dunbar

unread,
Feb 9, 2016, 6:23:33 AM2/9/16
to antlr-di...@googlegroups.com

Morning,

First of all, the 'select' is being lexed as an ID. This is because the
two are pretty much identical, so as ID appears first in the grammar, it
is being selected. So you get an ID rather than a K_SELECT.

Move the ID rule down in the grammar to just above the fragments first
of all. Then try again.

This time, however, everything is being lexed as a STRING_LITERAL. The
reason for this is that you have made the delimiting single quotes
pretty much optional with the '*' for zero or more when you should have
a '+' for one or more. So change the STRING_LITERAL rule to the following:

STRING_LITERAL : ('\'' | '\'\'')+(ID)('\'' | '\'\'')+;

It should work now. However, should there be a "FROM" keyword and a
table_name? It looks strange to me (I'm an Oracle DBA by trade!) to see
something like:

select a,b,c.d where 1=7

And there's no trailing semi-colon either, should there be?


HTH

Cheers,
Norm.

Disclaimer: I'm not a compiler writer, nor do I play one on TV.


--
Norman Dunbar
Dunbar IT Consultants Ltd

Registered address:
27a Lidget Hill
Pudsey
West Yorkshire
United Kingdom
LS28 7LG

Company Number: 05132767

Bing Yuan

unread,
Feb 9, 2016, 2:34:09 PM2/9/16
to antlr-discussion
Hi Norman, 

Thanks for your response. 

I've made the changes as you've suggested. It's working now. 

As for the reason for not having "from" clause is because it's inferred from web service path. What I'm working on is a web service that allows the end user to use SQL like syntax to query POJOs. 

Norman Dunbar

unread,
Feb 10, 2016, 3:02:42 AM2/10/16
to antlr-di...@googlegroups.com

Morning Bing,

On 09/02/16 19:34, Bing Yuan wrote:

> Thanks for your response.
You are welcome.

> I've made the changes as you've suggested. It's working now.
I know! I set up a small test of your grammar and used that to debug it!
That's the only way I would be able to see what was the problem. I'm not
very good at this compiler/parser stuff - but I'm working on it!

> As for the reason for not having "from" clause is because it's inferred
> from web service path. What I'm working on is a web service that allows
> the end user to use SQL like syntax to query POJOs.
That makes sense, thanks.


Have fun.

Cheers,
Norm.

Disclaimer: I'm not a compiler writer, nor do I play one on TV.

--
Norman Dunbar
Dunbar IT Consultants Ltd

Registered address:
27a Lidget Hill
Pudsey
West Yorkshire
United Kingdom
LS28 7LG

Company Number: 05132767

--
Cheers,
Norm. [TeamT]
Reply all
Reply to author
Forward
0 new messages