Parser for SQL Query Select - YACC Program - Compiler Design

Program:

// Lex Program :sq.l


alpha [A-Za-z]
digit [0-9]
%%


[ \t\n]
select            return SELECT;
distinct         return DISTINCT;
from             return FROM;
where           return WHERE;
like               return LIKE;
desc              return DESC;
asc                return ASC;
"group by"    return GROUP;
having          return HAVING;
"order by"    return ORDER;
or                 return OR;
and               return AND;
{digit}+       return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="             return LE;
">="             return GE;
"=="             return EQ;
"!="              return NE;
.                   return yytext[0];

%%


// Yacc Program: sq.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE

%%

    S         : ST1';' {printf("INPUT ACCEPTED.... \n\t\t...www.2k8cse.cu.cc\n");exit(0);};
    ST1     : SELECT attributeList FROM tableList ST2
               | SELECT DISTINCT attributeList FROM tableList ST2
               ;
    ST2     : WHERE COND ST3
               | ST3
               ;
    ST3     : GROUP attributeList ST4
               | ST4
               ;
    ST4     : HAVING COND ST5
               | ST5
               ;
    ST5     : ORDER attributeList ST6
               |
               ;
    ST6     : DESC
               | ASC
               |
               ;
  attributeList :     ID','attributeList
               | '*'
               | ID
               ;
 tableList    : ID',' tableList
               | ID
               ;
    COND    : COND OR COND
               | COND AND COND
               | E
               ;
    E         : F '=' F
               | F '<' F
               | F '>' F 
               | F LE F
               | F GE F
               | F EQ F
               | F NE F
               | F OR F
               | F AND F
               | F LIKE F
               ;
    F         : ID
               | NUM 
               ;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
    printf("Enter the query:");
    yyparse();
}          


Output:
nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select name,address from emp where age>20 group by name having age<40 order by name desc;
INPUT ACCEPTED...
                                   ...www.2k8cse.cu.cc
nn@linuxmint ~ $

7 comments:

  1. Replies
    1. helped me in doing my assignment.
      thanks for the program.

      Delete
  2. can i have the program which have nesting also

    ReplyDelete
  3. Are you satisfied with this?

    http://2k8618.blogspot.com/2011/09/parser-for-sql-nested-queries-compiler.html

    ReplyDelete
  4. can i have the lexical analysis for this program

    ReplyDelete
  5. upload the yacc program for checking the syntax of insert statement of the sql

    ReplyDelete
  6. Why have you used ?
    Why do we need set precdence in this ?

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...