-
Notifications
You must be signed in to change notification settings - Fork 3
/
lex.py
228 lines (158 loc) · 3.1 KB
/
lex.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import ply.lex as lex
tokens = [
'create',
'use',
'show',
'insert',
'select',
'update',
'delete',
'drop',
'exit',
'databases',
'database',
'tables',
'table',
'into',
'values',
'from',
'all',
'where',
'set',
'compare',
'logic',
'char',
'int',
'id',
'number',
'string',
'comma',
'semicolon',
'left_paren',
'right_paren'
]
def t_create(t):
r"""(C|c)(R|r)(E|e)(A|a)(T|t)(E|e)"""
t.value = "CREATE"
return t
def t_use(t):
r"""(U|u)(S|s)(E|e)"""
t.value = "USE"
return t
def t_show(t):
r"""(S|s)(H|h)(O|o)(W|w)"""
t.value = "SHOW"
return t
def t_insert(t):
r"""(I|i)(N|n)(S|s)(E|e)(R|r)(T|t)"""
t.value = "INSERT"
return t
def t_select(t):
r"""(S|s)(E|e)(L|l)(E|e)(C|c)(T|t)"""
t.value = "SELECT"
return t
def t_update(t):
r"""(U|u)(P|p)(D|d)(A|a)(T|t)(E|e)"""
t.value = 'UPDATE'
return t
def t_delete(t):
r"""(D|d)(E|e)(L|l)(E|e)(T|t)(E|e)"""
t.value = "DELETE"
return t
def t_drop(t):
r"""(D|d)(R|r)(O|o)(P|p)"""
t.value = "DROP"
return t
def t_exit(t):
r"""(E|e)(X|x)(I|i)(T|t)"""
t.value = "EXIT"
return t
def t_databases(t):
r"""(D|d)(A|a)(T|t)(A|a)(B|b)(A|a)(S|s)(E|e)(S|s)"""
t.value = "DATABASES"
return t
def t_database(t):
r"""(D|d)(A|a)(T|t)(A|a)(B|b)(A|a)(S|s)(E|e)"""
t.value = "DATABASE"
return t
def t_tables(t):
r"""(T|t)(A|a)(B|b)(L|l)(E|e)(S|s)"""
t.value = "TABLES"
return t
def t_table(t):
r"""(T|t)(A|a)(B|b)(L|l)(E|e)"""
t.value = "TABLE"
return t
def t_into(t):
r"""(I|i)(N|n)(T|t)(O|o)"""
t.value = "INTO"
return t
def t_values(t):
r"""(V|v)(A|a)(L|l)(U|u)(E|e)(S|s)"""
t.value = "VALUES"
return t
def t_from(t):
r"""(F|f)(R|r)(O|o)(M|m)"""
t.value = "FROM"
return t
def t_all(t):
r"""\*"""
return t
def t_where(t):
r"""(W|w)(H|h)(E|e)(R|r)(E|e)"""
t.value = "WHERE"
return t
def t_set(t):
r"""(S|s)(E|e)(T|t)"""
t.value = "SET"
return t
def t_compare(t):
r"""(=)|(!=)|(>)|(<)|(>=)|(<=)"""
return t
def t_logic(t):
r"""((A|a)(N|n)(D|d))|((O|o)(R|r))"""
return t
def t_char(t):
r"""(C|c)(H|h)(A|a)(R|r)"""
t.value = "CHAR"
return t
def t_int(t):
r"""(I|i)(N|n)(T|t)"""
t.value = "INT"
return t
def t_id(t):
r"""[A-Za-z][A-Za-z0-9_]*"""
return t
def t_number(t):
r"""[0-9]+"""
return t
def t_string(t):
r"""\'[A-Za-z][A-Za-z0-9_]*\'"""
return t
def t_comma(t):
r""","""
return t
def t_semicolon(t):
r""";"""
return t
def t_left_paren(t):
r"""\("""
return t
def t_right_paren(t):
r"""\)"""
return t
t_ignore = " \t\n"
def t_newline(t):
r"""\n+"""
t.lexer.lineno += t.value.count('\n')
def t_error(t):
print("Illegal character '%s' in line %d" % (t.value[0], t.lexer.lineno))
t.lexer.skip(1)
lexer = lex.lex()
# with open('testData.txt', 'r') as f:
# s = f.read()
#
# lexer.input(s)
#
# for token in lexer:
# print(token)