aboutsummaryrefslogtreecommitdiff
path: root/compilador/parser.py
blob: 61ab21a33d67dac1edd6e1c60d7528a67298f315 (plain)
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
221
222
223
224
225
226
227
228
229
230
from enum import Enum
from tabla import LexToken, TablaLex, tokens
from arbol import Arbol, Nodo
from shared import Control
from pprint import pprint

valores = ['IDENT', 'BOOLEAN_LIT', 'CHAR_LIT', 'INT_LIT', 'STRING_LIT']

operadores = [
    '>=', '<=', '==', '!=', '&&', '||', '++', '--',
    '=', '+', '-', '&', '|', '!', '<', '>'
]

class Selector(Enum):
    NINGUNO = 0
    DEF_VARIABLE = 1
    DIRECTIVA = 2
    EXPRESION = 3
    IF = 4
    FOR = 5
    WHILE = 6
    FUNCION = 7

class Parser:
    def __init__(self, tabla):
        self.arbol = Arbol()
        self.pila_selector = [
            [Selector.NINGUNO, []] # selector, recolector
        ]
        self.pila_arbol = [self.arbol.raiz]
        self.expresion = None
        self.tabla = tabla
    
    def inicio (self):
        for t in self.tabla.tabla:
            r = self.procesar(t)
            if r == 2: return
            while r != Control.SIGUIENTE:
                r = self.procesar(t)
                if r == Control.ERROR: return

        print(str(self.arbol))

    def procesar (self, t: LexToken):
        if len(self.pila_selector) == 0:
            return Control.SIGUIENTE
            
        pprint (self.pila_selector[-1])
        
        cima = self.pila_selector[-1]
        
        if cima[0] == Selector.NINGUNO:
            # Entrada a definición de variable (o función)
            if t.tipo in ['BOOLEAN', 'CHAR', 'INT', 'VOID']:
                self.pila_selector.pop()
                self.pila_selector.append([Selector.DEF_VARIABLE, [t]])
                return Control.SIGUIENTE
            # Entrada a directiva del lenguaje
            elif t.tipo in ['PRINT', 'READ', 'RETURN']:
                self.pila_selector.pop()
                self.pila_selector.append([Selector.DIRECTIVA, [t]])
                return Control.SIGUIENTE
            # Entrada a expresión
            elif t.tipo in valores:
                self.pila_selector.pop()
                self.pila_selector.append([Selector.EXPRESION, [t]])
                return Control.SIGUIENTE
            # Entrada a if
            elif t.tipo == 'IF':
                self.pila_selector.pop()
                self.pila_selector.append([Selector.IF, []])
                return Control.SIGUIENTE
            # Entrada a for
            elif t.tipo == 'FOR':
                self.pila_selector.pop()
                self.pila_selector.append([Selector.FOR, []])
                return Control.SIGUIENTE
            # Entrada a while
            elif t.tipo == 'WHILE':
                self.pila_selector.pop()
                self.pila_selector.append([Selector.WHILE, []])
                return Control.SIGUIENTE

        if cima[0] == Selector.DEF_VARIABLE:
            return self.procesar_def_variable(t)

        if cima[0] == Selector.DIRECTIVA:
            return self.procesar_directiva(t)

        if cima[0] == Selector.EXPRESION:
            return self.procesar_expresion(t)

        if cima[0] == Selector.IF:
            return self.procesar_if(t)

        if cima[0] == Selector.FOR:
            return self.procesar_for(t)

        if cima[0] == Selector.WHILE:
            return self.procesar_while(t)

        if cima[0] == Selector.FUNCION:
            return self.procesar_funcion(t)

        return Control.SIGUIENTE

    def procesar_def_variable(self, t):
        recol = self.pila_selector[-1][1]
        
        # tipo
        if len(recol) == 1:
            if t.tipo != 'IDENT':
                print('Error: se esperaba identificador')
                return Control.ERROR
            recol.append(t)
            return Control.SIGUIENTE
            
        # tipo + ident
        if len(recol) == 2:
            if t.tipo == ';':
                self.pila_arbol[-1].hijos.append(Nodo({
                    'selector': Selector.DEF_VARIABLE,
                    'tipo': recol[0].tipo,
                    'nombre': recol[1].nombre
                }))
                self.pila_selector.pop()
                self.pila_selector.append([Selector.NINGUNO, []])
            elif t.tipo == '=':
                recol.append(t)
            else:
                print('Error: se esperaba `;` o `=`')
                return Control.ERROR
            return Control.SIGUIENTE

        # tipo + ident + =
        if len(recol) == 3:
            if t.tipo in valores:
                self.pila_selector.append([Selector.EXPRESION, [t]])
                recol.append(t)
            else:
                print('Error: se esperaba una expresión')
                return Control.ERROR
            return Control.SIGUIENTE

        # tipo + ident + = + expr
        if len(recol) == 4:
            if t.tipo == ';':
                self.pila_arbol[-1].hijos.append(Nodo({
                    'selector': Selector.DEF_VARIABLE,
                    'tipo': recol[0].tipo,
                    'nombre': recol[1].nombre,
                    'valor': self.expresion
                }))
                self.expresion = None
                self.pila_selector.pop()
                self.pila_selector.append([Selector.NINGUNO, []])
            else:
                print('Error: se esperaba `;`')
                return Control.ERROR

        return Control.SIGUIENTE

    def procesar_directiva(self, t):
        recol = self.pila_selector[-1][1]
        
        # directiva
        if len(recol) == 1:
            if t.tipo in valores:
                self.pila_selector.append([Selector.EXPRESION, [t]])
                recol.append(t)
            else:
                print('Error: se esperaba una expresión')
                return Control.ERROR
            return Control.SIGUIENTE

        # directiva + expr
        if len(recol) == 2:
            if t.tipo == ';':
                self.pila_arbol[-1].hijos.append(Nodo({
                    'selector': Selector.DIRECTIVA,
                    'expresion': self.expresion
                }))
                self.expresion = None
                self.pila_selector.pop()
                self.pila_selector.append([Selector.NINGUNO, []])
            else:
                print('Error: se esperaba `;`')
                return Control.ERROR

        return Control.SIGUIENTE

    def procesar_expresion(self, t):
        recol = self.pila_selector[-1][1]
        tipo_ultimo = recol[-1].tipo

        if len(recol) == 1:
            if tipo_ultimo in valores:
                recol.append(recol[-1])
            else:
                print('Error: se esperaba un identificador o una literal')
                return Control.ERROR
            
        if tipo_ultimo in valores and t.tipo in operadores:
            recol.append(t)  
        elif tipo_ultimo in operadores and t.tipo in valores:
            recol.append(t)
        elif tipo_ultimo in valores and t.tipo in valores:
            print('Error: se esperaba un operador')
            return Control.ERROR
        elif tipo_ultimo in operadores and t.tipo in operadores:
            print('Error: se esperaba un identificador o una literal')
            return Control.ERROR
        else:
            self.expresion = recol[1:]
            self.pila_selector.pop()
            return Control.REPETIR

        return Control.SIGUIENTE

    def procesar_if(self, t):
        return

    def procesar_for(self, t):
        return

    def procesar_while(self, t):
        return

    def procesar_funcion(self, t):
        return