forked from senarukana/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql1.l
180 lines (154 loc) · 3.96 KB
/
sql1.l
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
%{
/*
* AT&T lex can't handle this lexer due to lex bugs. It works with flex
* 2.3.7, pclex 2.0.5, and MKS lex 3.1a.
*/
#include "sql1.h"
#include <string.h>
int lineno = 1;
void yyerror(char *s);
%}
/* MKS needs the next line to increase the NFA table */
%e 1200
%%
/* literal keyword tokens */
ADA { return ADA; }
ALL { return ALL; }
AND { return AND; }
AVG { return AMMSC; }
MIN { return AMMSC; }
MAX { return AMMSC; }
SUM { return AMMSC; }
COUNT { return AMMSC; }
ANY { return ANY; }
AS { return AS; }
ASC { return ASC; }
AUTHORIZATION { return AUTHORIZATION; }
BETWEEN { return BETWEEN; }
BY { return BY; }
C { return C; }
CHAR(ACTER)? { return CHARACTER; }
CHECK { return CHECK; }
CLOSE { return CLOSE; }
COBOL { return COBOL; }
COMMIT { return COMMIT; }
CONTINUE { return CONTINUE; }
CREATE { return CREATE; }
CURRENT { return CURRENT; }
CURSOR { return CURSOR; }
DECIMAL { return DECIMAL; }
DECLARE { return DECLARE; }
DEFAULT { return DEFAULT; }
DELETE { return DELETE; }
DESC { return DESC; }
DISTINCT { return DISTINCT; }
DOUBLE { return DOUBLE; }
ESCAPE { return ESCAPE; }
EXISTS { return EXISTS; }
FETCH { return FETCH; }
FLOAT { return FLOAT; }
FOR { return FOR; }
FOREIGN { return FOREIGN; }
FORTRAN { return FORTRAN; }
FOUND { return FOUND; }
FROM { return FROM; }
GO[ \t]*TO { return GOTO; }
GRANT { return GRANT; }
GROUP { return GROUP; }
HAVING { return HAVING; }
IN { return IN; }
INDICATOR { return INDICATOR; }
INSERT { return INSERT; }
INT(EGER)? { return INTEGER; }
INTO { return INTO; }
IS { return IS; }
KEY { return KEY; }
LANGUAGE { return LANGUAGE; }
LIKE { return LIKE; }
MODULE { return MODULE; }
NOT { return NOT; }
NULL { return NULLX; }
NUMERIC { return NUMERIC; }
OF { return OF; }
ON { return ON; }
OPEN { return OPEN; }
OPTION { return OPTION; }
OR { return OR; }
ORDER { return ORDER; }
PASCAL { return PASCAL; }
PLI { return PLI; }
PRECISION { return PRECISION; }
PRIMARY { return PRIMARY; }
PRIVILEGES { return PRIVILEGES; }
PROCEDURE { return PROCEDURE; }
PUBLIC { return PUBLIC; }
REAL { return REAL; }
REFERENCES { return REFERENCES; }
ROLLBACK { return ROLLBACK; }
SCHEMA { return SCHEMA; }
SELECT { return SELECT; }
SET { return SET; }
SMALLINT { return SMALLINT; }
SOME { return SOME; }
SQLCODE { return SQLCODE; }
TABLE { return TABLE; }
TO { return TO; }
UNION { return UNION; }
UNIQUE { return UNIQUE; }
UPDATE { return UPDATE; }
USER { return USER; }
VALUES { return VALUES; }
VIEW { return VIEW; }
WHENEVER { return WHENEVER; }
WHERE { return WHERE; }
WITH { return WITH; }
WORK { return WORK; }
/* punctuation */
"=" |
"<>" |
"<" |
">" |
"<=" |
">=" { return COMPARISON; }
[-+*/:(),.;] { return yytext[0]; }
/* names */
[A-Za-z][A-Za-z0-9_]* { return NAME; }
/* numbers */
[0-9]+ |
[0-9]+"."[0-9]* |
"."[0-9]* { return INTNUM; }
[0-9]+[eE][+-]?[0-9]+ |
[0-9]+"."[0-9]*[eE][+-]?[0-9]+ |
"."[0-9]*[eE][+-]?[0-9]+ { return APPROXNUM; }
/* strings */
'[^'\n]*' {
int c = input();
unput(c); /* just peeking */
if(c != '\'') {
return STRING;
} else
yymore();
}
'[^'\n]*$ { yyerror("Unterminated string"); }
\n lineno++;
[ \t\r]+ ; /* white space */
"--".*$ ; /* comment */
%%
void
yyerror(char *s)
{
printf("%d: %s at %s\n", lineno, s, yytext);
}
main(int ac, char **av)
{
if(ac > 1 && (yyin = fopen(av[1], "r")) == NULL) {
perror(av[1]);
exit(1);
}
if(!yyparse()) {
printf("SQL parse worked\n");
}
else {
printf("SQL parse failed\n");
}
} /* main */