-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.y
61 lines (48 loc) · 1.42 KB
/
sql.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Parser for SQL Insert Statement */
%{
#include <stdio.h>
#include <stdlib.h>
int columnCount = 0, valueCount = 0;
%}
%token ID
%token NUM
%token INSERT
%token INTO
%token VALUES
%%
S : ST1';'{
printf("QUERY ACCEPTED");
exit(0);
}
;
ST1 : INSERT INTO table '(' attributeList ')' VALUES '(' valuesList ')'
{
if(valueCount != columnCount) {
printf("QUERY REJECTED!\nERROR : Mismatching LENGTH of Attributes and Values\n");
return 0;
}
}
| INSERT INTO table VALUES '(' valuesList ')'
;
attributeList : ID ',' attributeList {columnCount++;}
| ID {columnCount++;}
;
valuesList : ID ',' valuesList {valueCount++;}
| NUM ',' valuesList {valueCount++;}
| ID {valueCount++;}
| NUM {valueCount++;}
;
table : ID;
%%
int yyerror (const char *str) {
printf("QUERY REJECTED");
exit(1);
}
int main()
{
printf("Enter the Insert Query:\n");
yyparse();
}
int yywrap() {
return 1;
}