-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyParser.py
213 lines (167 loc) · 3.83 KB
/
MyParser.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
import re
linhas = 1
def getAllTokens():
with open("input.txt", "r") as f:
tokens = (
f.read()
.replace(";", " ; ")
.replace("(", " ( ")
.replace(")", " ) ")
.replace("\n", " \n ")
.replace(">,", "> ,")
.replace(",<", ", <")
).lower()
tokens = tokens + " $"
tokens = re.sub(re.compile(f","), " , ", tokens)
return re.findall(r"\S+", tokens)
def messageError(incoming, expected):
if incoming == "$":
raise TypeError(
f'Error: Erro de token, esperado "{expected}" no comando:{linhas}')
else:
raise TypeError(
f'Error: Token "{incoming}" não reconhecido, esperado "{expected}" no comando:{linhas}')
tokens = getAllTokens()
def _nextToken():
for i in tokens:
yield i
gerador = _nextToken()
def nextToken():
t = next(gerador)
return t
token = nextToken()
def equality(expected):
global token
if token != expected:
messageError(token, expected)
token = nextToken()
def S():
global token, linhas
if token == "create":
token = nextToken()
if token == "table":
token = nextToken()
ID()
equality("(")
ID()
TIPO()
PT()
equality(")")
elif token == "database":
token = nextToken()
ID()
else:
messageError(token, "TABLE ou DATABASE")
elif token == "insert":
token = nextToken()
INSERT()
elif token == "select":
token = nextToken()
SELECT()
WHERE()
ORDER()
elif token == "update":
token = nextToken()
ID()
equality("set")
ID()
equality("=")
VALOR()
if token != 'where':
messageError(token, 'where')
WHERE()
elif token == "delete":
token = nextToken()
FROM()
if token != 'where':
messageError(token, 'where')
WHERE()
elif token == "truncate":
token = nextToken()
equality("table")
ID()
elif token == "use":
token = nextToken()
ID()
else:
messageError(
token, 'CREATE, USE, INSERT, SELECT, UPDATE, DELETE ou TRUNCATE')
equality(";")
linhas += 1
def TIPO():
ID()
def INSERT():
equality("into")
ID()
equality("(")
ID()
P()
equality(")")
equality("values")
equality("(")
VALOR()
VALOR_VARIOS()
equality(")")
def SELECT():
global token
if token == "*":
token = nextToken()
else:
ID()
P()
FROM()
def WHERE():
global token
if token == "where":
token = nextToken()
ID()
equality("=")
VALOR()
def ORDER():
global token
if token == "order":
token = nextToken()
equality("by")
ID()
def FROM():
global token
if token == "from":
token = nextToken()
ID()
else:
messageError(token, "from")
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 VALOR():
global token
if not re.match(
re.compile(f"^([a-z]([a-z0-9])*|[0-9]*)$", flags=re.IGNORECASE), token
):
messageError(token, "Valor válido")
token = nextToken()
def VALOR_VARIOS():
global token
if token == ",":
token = nextToken()
VALOR()
VALOR_VARIOS()
def PT():
global token
if token == ",":
token = nextToken()
ID()
TIPO()
PT()
def P():
global token
if token == ",":
token = nextToken()
ID()
P()
def main():
while token != "$":
S()
main()