سؤال

In an old postI found a recomendation for a SQL parser I was searching for Lex and Yacc. Here is the link.

SQL lex yacc grammar

I later have found that it is the code that comes explained in the O'reilly book "lex & yacc.

I am trying to put it working and I have succesfully integrated in my aplication, but whenever I send an UPDATE command I get a syntax error, even with the simplest ones:

UPDATE user SET name = 'johnfoo'

I get the error on the = symbol. I have tried to trace everything but I cannot find why it gives this message. I have tried to analize the lex and yacc code and It makes no sense for me, as the code looks correct.

[UPDATE]The error I get is just:

1: syntax error at =
Embedded SQL parse failed

INSERT works perfectly.

هل كانت مفيدة؟

المحلول

After some different ways of trying, the sugested solution (now deleted by the author) worked in some way.

What he suggested was updating the lex and yacc definition of comparision.

In the lex file change

<SQL>"="    | 
<SQL>"<>"   |
<SQL>"<"    |
<SQL>">"    |
<SQL>"<="   |
<SQL>">="       TOK(COMPARISON)

by

<SQL>"="    TOK(EQ)
<SQL>"<>"   TOK(NE)
<SQL>"<"    TOK(LT)
<SQL>">"    TOK(GT)
<SQL>"<="   TOK(LE)
<SQL>">="   TOK(GE)

In the yacc file add:

comparison: 
        EQ
    | NE
    | LT
    | GT
    | LE
    | GE
    ;

And change all references to = with EQ and the other symbols and COMPARISON with comparison:

%left COMPARISON /* = <> < > <= >= */

by

%left EQ NE LT GT LE GE /* = <> < > <= >= */

assignment:
        column = scalar_exp
    |   column = NULLX 
    ;

by

assignment:
        column EQ scalar_exp
    |   column EQ NULLX 
    ;

And

comparison_predicate:
        scalar_exp COMPARISON scalar_exp
    |   scalar_exp COMPARISON subquery
    ;

by

comparison_predicate:
        scalar_exp comparison scalar_exp
    |   scalar_exp comparison subquery
    ;

And it works!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top