-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
220 lines (193 loc) · 4.83 KB
/
main.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
import re
def getAllTokens():
with open('input.txt','r') as f:
tokens = f.read()\
.replace(';',' ; ')\
.replace('(',' ( ')\
.replace(')',' ) ')\
.replace('\n',' \n ')\
.replace('>,','> ,')\
.replace(',<',', <')
tokens = re.sub(re.compile(f','),' , ',tokens)
return re.findall(r'\S+', tokens)
def messageError(incoming,expected):
raise TypeError(f'Error: Token "{incoming}" não reconhecido, esperado "{expected}"')
tokens = getAllTokens()
def _nextToken():
for i in tokens:
yield i
gerador = _nextToken()
def nextToken():
try:
t = next(gerador)
return t
except StopIteration:
raise TypeError('Error: ";" esperado.')
token = nextToken()
def ID():
global token
if not re.match(re.compile(f'[a-z]([a-z0-9])*',flags=re.IGNORECASE),token):
messageError(token, 'ID válido')
token = nextToken()
def S():
global token
if token.upper() == 'CREATE':
token = nextToken()
create()
elif token.upper() == 'USE':
token = nextToken()
ID()
if token != ';':
messageError(token, ';')
elif token.upper() == 'INSERT':
token = nextToken()
if token.upper() != 'INTO':
messageError(token,'INTO')
token = nextToken()
ID()
idParenthesesNoType()
if token.upper() != 'VALUES':
messageError(token,'VALUES')
token = nextToken()
valueParentheses()
elif token.upper() == 'SELECT':
token = nextToken()
if token == '*':
token = nextToken()
fromID()
else:
ID()
idNoParentheses()
fromID()
where()
orderBy()
if token != ';':
messageError(token, ';')
elif token.upper() == 'UPDATE':
token = nextToken()
ID()
Set()
elif token.upper() == 'DELETE':
token = nextToken()
fromID()
where()
elif token.upper() == 'TRUNCATE':
token = nextToken()
table()
else:
messageError(token, "CREATE, USE, INSERT, SELECT, UPDATE, DELETE, TRUNCATE")
def Set():
global token
if token.upper() != 'SET':
messageError(token, 'SET')
token = nextToken()
compare()
where()
def compare():
global token
ID()
if token != "=":
messageError(token, "=")
token = nextToken()
value()
def where():
global token
if token.upper() == 'WHERE':
token = nextToken()
compare()
else:
messageError(token, 'WHERE')
def value():
global token
if re.search(r"^((\"[^\"]*\")|(\'[^']*\')|([0-9]+))$", token):
token = nextToken()
else:
messageError(token, 'valor')
def orderBy():
global token
if token.upper() == 'ORDER':
token = nextToken()
if token.upper() != 'BY':
messageError(token, "BY")
else:
token = nextToken()
ID()
idNoParentheses()
if token.upper() in ['DESC', 'ASC']:
token = nextToken()
def fromID():
global token
if token.upper() != "FROM":
messageError(token, 'FROM')
else:
token = nextToken()
ID()
def create():
global token
if token.upper() == 'DATABASE':
token = nextToken()
ID()
if token != ';':
messageError(token,';')
elif token.upper() == 'TABLE':
table()
else:
messageError(token,'DATABASE ou TABLE')
def table():
global token
token = nextToken()
ID()
idParenthesesWithType()
if token != ';':
messageError(token,';')
def idParenthesesWithType():
global token
if token == '(':
token = nextToken()
ID()
ID()
idParenthesesWithType()
if token != ')':
messageError(token,')')
else:
token = nextToken()
if token == ',':
token = nextToken()
ID()
ID()
idParenthesesWithType()
def idParenthesesNoType():
global token
if token == '(':
token = nextToken()
ID()
idParenthesesNoType()
if token != ')':
messageError(token,')')
else:
token = nextToken()
if token == ',':
token = nextToken()
ID()
idParenthesesNoType()
def idNoParentheses():
global token
if token == ',':
token = nextToken()
ID()
idNoParentheses()
def valueParentheses():
global token
if token == '(':
token = nextToken()
value()
valueParentheses()
if token != ')':
messageError(token,')')
else:
token = nextToken()
if token == ',':
token = nextToken()
value()
valueParentheses()
S()