-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalisador_semantico.py
166 lines (139 loc) · 3.5 KB
/
analisador_semantico.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
t_count = 1
def getTemp():
"""."""
global t_count
if t_count < 10:
aux = '0%s
' % t_count
else:
aux = str(t_count)
t_count += 1
return aux
def op_pri(op):
"""."""
if op in ('*', '/'):
return 2
elif op in ('+', '-'):
return 1
else:
return 0
class AnalisadorSemantico(object):
"""."""
def __init__(self, expr, is_sub=False):
"""."""
self.expr = expr
self.operators = []
self.numbers = []
self.output = ''
self.is_sub = is_sub
def __repr__(self):
"""."""
return str(vars(self))
def add_num(self, num):
"""."""
self.numbers.append(num)
def add_op(self, op):
"""."""
self.operators.append(op)
def drop_num(self):
"""."""
return self.numbers.pop()
def drop_op(self):
"""."""
return self.operators.pop()
def eh_simbolo(self):
"""."""
return self.expr.value() in ['+', '-', '*', '/']
def gera_codigo(self):
"""."""
a = self.drop_num()
b = self.drop_num()
o = self.drop_op()
t = '#T%s' % getTemp()
self.add_num(t)
self.output += 'LDA %s\n' % b
if o == '+':
self.output += 'ADA %s\n' % a
elif o == '-':
self.output += 'SUB %s\n' % a
elif o == '*':
self.output += 'MUL %s\n' % a
elif o == '/':
self.output += 'DIV %s\n' % a
else:
raise Exception('Operador desconhecido')
self.output += 'STA %s\n' % t
if self.operators:
self.gera_codigo()
else:
return
def op_ok(self):
"""."""
if not self.operators:
return True
elif op_pri(self.expr.value()) > op_pri(self.operators[-1]):
return True
else:
return False
def start(self):
"""."""
return self.e0()
def e0(self):
"""."""
if self.expr.fim():
self.gera_codigo()
return
elif self.expr.value() == '(':
self.expr.up_index()
sub = AnalisadorSemantico(self.expr, True)
sub.start()
self.output += sub.output
self.add_num(sub.drop_num())
self.e0()
elif self.expr.value() == ')':
self.expr.up_index()
if self.is_sub:
self.gera_codigo()
return
elif self.expr.value().isdigit():
self.e1()
elif self.eh_simbolo():
self.e2()
else:
raise Exception(self.expr.value())
def e1(self):
"""."""
self.add_num(self.expr.value())
self.expr.up_index()
self.e0()
def e2(self):
"""."""
if self.op_ok():
self.add_op(self.expr.value())
self.expr.up_index()
else:
self.gera_codigo()
self.e0()
class Expressao(object):
"""."""
def __init__(self, expr):
"""."""
self.expr = expr
self.index = 0
def up_index(self):
self.index += 1
def fim(self):
"""."""
return self.index == len(self.expr)
def value(self):
"""."""
if not self.fim():
return self.expr[self.index]
else:
return ''
def __repr__(self):
"""."""
return str(self)
def __str__(self):
"""."""
return str(vars(self))