-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
70 lines (63 loc) · 2.21 KB
/
parser.py
File metadata and controls
70 lines (63 loc) · 2.21 KB
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
from re import A
from s_lexer import *
from tablasGramaticas import *
stack = ['eof', SWhile]
def customParser(f):
global tok
global x
#f = open('fuente.c','r')
#lexer.input(f.read())
lexer.input(f+'$')
tok=lexer.token()
x=stack[-1] #primer elemento de der a izq
parserGen(tablaWhile)
def parserGen(tablaParsing):
global tok
global x
while True:
print("TOKEN: ",tok.type)
print("X: ",x)
if x == tok.type and x == 'eof':
print("Cadena reconocida exitosamente")
return #aceptar
else:
if x == tok.type and x != 'eof':
print('*Eliminando token: ',x)
stack.pop()
x=stack[-1]
tok=lexer.token()
if x == SOpe:
parserGen(tablaOperaciones)
elif x == SIfElse:
parserGen(tablaIfElse)
elif x == SWhile:
parserGen(tablaWhile)
if x in tokens and x != tok.type:
print("Error: Se esperaba ", x)
print('en la posicion: ', tok.lexpos+1);
return 0;
print(tok.type)
print('CHECKPOINT STACK: ',stack)
if x not in tokens: #es no terminal
celda=buscar_en_tabla(x,tok.type,tablaParsing)
if celda is None:
print("Error: No se esperaba", tok.type)
print('en la posicion: ', tok.lexpos+1);
return 0;
else:
stack.pop()
agregar_pila(celda)
x=stack[-1]
print(stack)
print('------------------------------------------------------------------')
def buscar_en_tabla(no_terminal,terminal,tabla):
for i in range(len(tabla)):
if( tabla[i][0] == no_terminal and tabla[i][1] == terminal):
return tabla[i][2] #retorno la celda
def agregar_pila(produccion):
for elemento in reversed(produccion):
if elemento != 'vacia': #la vacía no la inserta
stack.append(elemento)
f = open('./source/ExampleC.c','r')
customParser(f.read())
f.close()