Skip to content

Commit 4c7c449

Browse files
committed
Bug no Parser reparado
1 parent 3354c18 commit 4c7c449

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

abstract_syntax_tree.py

+21-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def __init__(self, name, expression):
4444
self.name = name
4545
self.expression = expression
4646
self.type = "Assignment"
47-
variable[name] = expression
4847

4948
class Token:
5049
def __init__(self, value, type, line, column):
@@ -83,6 +82,7 @@ def parseVS(self):
8382
self.consume("EQUAL")
8483
exp = self.parseE()
8584
e = EAssignment(var.value, exp)
85+
variable[var.value] = exp
8686

8787
if not self.peek("NEWLINE"):
8888
if self.peek("EOF"):
@@ -133,10 +133,18 @@ def parseE(self):
133133

134134
elif self.peek("SUM"):
135135
self.consume("SUM")
136+
137+
if not self.peek("NUM") and not self.peek("VARIABLE") and not self.peek("PARENTESES_L") and not self.peek("FUNCTION"):
138+
raise SyntaxError("Expected token of type NUMBER, VARIABLE, PARENTESES_L or FUNCTION at line " + str(self.next_token.line) + " column " + str(self.next_token.column))
139+
136140
e = EBinary(e, "+", self.parseT())
137141

138142
elif self.peek("SUB"):
139143
self.consume("SUB")
144+
145+
if not self.peek("NUM") and not self.peek("VARIABLE") and not self.peek("PARENTESES_L") and not self.peek("FUNCTION"):
146+
raise SyntaxError("Expected token of type NUMBER, VARIABLE, PARENTESES_L or FUNCTION at line " + str(self.next_token.line) + " column " + str(self.next_token.column))
147+
140148
e = EBinary(e, "-", self.parseT())
141149

142150
else:
@@ -152,10 +160,18 @@ def parseT(self):
152160

153161
elif self.peek("MULT"):
154162
self.consume("MULT")
163+
164+
if not self.peek("NUM") and not self.peek("VARIABLE") and not self.peek("PARENTESES_L") and not self.peek("FUNCTION"):
165+
raise SyntaxError("Expected token of type NUMBER, VARIABLE, PARENTESES_L or FUNCTION at line " + str(self.next_token.line) + " column " + str(self.next_token.column))
166+
155167
e = EBinary(e, "*", self.parseF())
156168

157169
elif self.peek("DIV"):
158170
self.consume("DIV")
171+
172+
if not self.peek("NUM") and not self.peek("VARIABLE") and not self.peek("PARENTESES_L") and not self.peek("FUNCTION"):
173+
raise SyntaxError("Expected token of type NUMBER, VARIABLE, PARENTESES_L or FUNCTION at line " + str(self.next_token.line) + " column " + str(self.next_token.column))
174+
159175
e = EBinary(e, "/", self.parseF())
160176

161177
else:
@@ -189,12 +205,11 @@ def parseF(self):
189205
elif self.peek("FUNCTION"):
190206
function = self.consume("FUNCTION").value
191207

192-
if self.peek("PARENTESES_L"):
193-
self.consume("PARENTESES_L")
194-
e = self.parseE()
195-
self.consume("PARENTESES_R")
208+
self.consume("PARENTESES_L")
209+
e = self.parseE()
210+
self.consume("PARENTESES_R")
196211

197-
return EFunction(function, [e])
212+
return EFunction(function, [e])
198213

199214
def evaluate_expression(expression, variables = variable):
200215
match expression.type:

main.py

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def read_file_tokens(code_file):
4343

4444
test.control_test()
4545
test.lexer_test()
46+
test.parser_test()
4647
test.evaluation_test()
4748

4849
print("Testes concluídos com sucesso!")

test.py

+22
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ def lexer_test(self):
5656
assert(lexer.lexical_analysis("1 + 2 * 3") == [('NUM', '1'), ('SUM', '+'), ('NUM', '2'), ('MULT', '*'), ('NUM', '3'), ('EOF', 'EOF')])
5757
print("Lexer testado com sucesso!\n")
5858

59+
def parser_test(self):
60+
try:
61+
error_test_1 = main.read_file_tokens("@1 +* 2")
62+
main.Parser(error_test_1[0]).parseS()
63+
except Exception as e:
64+
print(e)
65+
66+
try:
67+
error_test_2 = main.read_file_tokens("@1 + 2 *")
68+
main.Parser(error_test_2[0]).parseS()
69+
except Exception as e:
70+
print(e)
71+
72+
try:
73+
error_test_3 = main.read_file_tokens("@1 2 * 3")
74+
main.Parser(error_test_3[0]).parseS()
75+
except Exception as e:
76+
print(e)
77+
78+
print("Parser testado com sucesso!\n")
79+
return
80+
5981
def evaluation_test(self):
6082
# Carrega os arquivos de teste na pasta tests
6183
for file in os.listdir(os.path.dirname(__file__) + "/tests"):

0 commit comments

Comments
 (0)