forked from nicolaskribas/DFA-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsintatico.py
197 lines (183 loc) · 5.79 KB
/
sintatico.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
import xml.etree.ElementTree as ET
dic = {'$':'0','j':'20','D':'22','K' :'10','Q':'23','I':'5','g':'15','B':'17','J':'6','W':'18','X':'11','e':'9','Y':'12','^':'21','H':'19','c':'14','_':'13','b':'4','l':'7','k':'8','`':'3','d':'16'}
fita = []
pilha = ['0']
arquivo = "parser.xml"
tree = ET.parse(arquivo)
root = tree.getroot()
filtro = "*"
in_state = 0
acpt = 0
rt = 0
TS = []
def empilha(estado):
pilha.append(fita[0])
pilha.append(estado)
del(fita[0])
def salto(estado):
pilha.append(estado)
def reduz(prod):
global rt
in_state = 0
tam = len(pilha)
for child in root.iter(filtro):
if child.tag == "Production" and child.attrib["Index"] == prod:
for i in range(int(child.attrib["SymbolCount"]) * 2):
if (int(child.attrib["SymbolCount"])*2) >= tam:
rt = 0
break
del(pilha[tam-i-1])
pilha.append(child.attrib["NonTerminalIndex"])
value = child.attrib["NonTerminalIndex"]
if child.tag == "LALRState":
if child.attrib["Index"] == pilha[len(pilha)-2]:
in_state=1
if child.tag == "LALRAction" and in_state == 1:
if child.attrib["SymbolIndex"] == pilha[len(pilha)-1]:
pilha.append(child.attrib["Value"])
break
def percorre():
in_state = 0
global acpt
global rt
for child in root.iter(filtro):
if child.tag == "LALRState":
if child.attrib["Index"] == pilha[len(pilha)-1] :
in_state = 1
else:
in_state = 0
if child.tag == "LALRAction" and in_state == 1:
if child.attrib["SymbolIndex"] == fita[0]:
if child.attrib["Action"]=='1':
rt = 1
empilha(child.attrib["Value"])
break
if child.attrib["Action"]=='2':
rt = 1
reduz(child.attrib["Value"])
break
if child.attrib["Action"]=='3':
rt = 1
salto(child.attrib["Value"])
break
if child.attrib["Action"] == '4':
acpt = 1
rt = 1
break
def translate():
for i in range(len(fita)):
fita[i] = dic[fita[i]]
def infos():
index = 0
cont = 0
where = 'all'
while index<len(TS):
if cont == 0:
where = 'all'
if TS[index][0] == 'j':
TS[index][3] = 'decl'
TS[index].append(where)
index = index + 1
TS[index][3]= 'var'
TS[index].append(where)
index = index + 1
TS[index][3] = 'N'
TS[index].append('N')
elif TS[index][0] == 'Q':
TS[index][3] = 'cond'
TS[index].append(where)
where = 'cond'
cont = cont + 1
#if
elif TS[index][0] == '^':
cont = cont + 1
TS[index][3] = 'N'
TS[index].append('N')
where = 'cond'
elif TS[index][0] == 'Y':
cont = cont - 1
TS[index][3] = 'N'
TS[index].append('N')
elif TS[index][0] == 'I':
TS[index][3] = 'N'
TS[index].append('N')
elif TS[index][0] == 'X':
TS[index][3] = 'N'
TS[index].append('N')
elif TS[index][0] == 'D':
index = index + 1
if TS[index][0] == 'e':
TS[index-1][3] = 'opvar'
TS[index-1].append(where)
TS[index][3] = 'N'
TS[index].append('N')
index = index + 1
while(TS[index][0] != 'K'):
if TS[index][0] == 'D':
TS[index][3] = 'opvar'
TS[index].append(where)
else:
TS[index][3] = 'N'
TS[index].append('N')
index = index + 1
TS[index][3] = 'N'
TS[index].append('N')
else:
TS[index-1][3] = 'oplvar'
TS[index-1].append(where)
TS[index][3] = 'N'
TS[index].append('N')
index = index + 1
if TS[index][0] == 'e':
TS[index][3] = 'oplvar'
TS[index].append(where)
else:
TS[index][3] = 'N'
TS[index].append('N')
index = index + 1
TS[index][3] = 'N'
TS[index].append('N')
#opl ou op
elif TS[index][0] == 'H':
TS[index][3] = 'rep'
TS[index].append(where)
where = 'rep'
cont = cont + 1
#while/fon
index = index + 1
print('Tabela de Simbolos com informações')
print(TS)
def sintatico():
translate()
global rt
while acpt == 0:
percorre()
#print(pilha)
#print(fita)
if acpt == 1:
print("Nenhum erro sintatico localizado")
infos()
elif rt == 0:
print("Erro sintatico!!!")
break
rt = 0
arquivo = open('fita_saida.txt','r')
for linha in arquivo:
for caractere in linha:
fita.append(caractere)
arquivo.close()
arquivo = open('TS.txt','r')
for linha in arquivo:
linha = linha.rstrip('\n')
aux = linha.split(' ')
TS.append(aux)
arquivo.close()
sintatico()
if acpt == 1:
with open('TS.txt', 'w') as arquivo:
for valor in TS:
for v in valor:
arquivo.write(str(v) + ' ')
arquivo.write('\n')
arquivo.close()
import semantico