-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconocedor.py
196 lines (168 loc) · 4.69 KB
/
reconocedor.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
import re
import os
def clear_console():
os_name = os.name
if os_name == 'posix':
# UNIX, Linux, macOS
os.system('clear')
elif os_name == 'nt':
# Windows
os.system('cls')
QE = -1 # Estado de error
# Matriz de transición
MT_MATRIX = {
# ROBOT
(0, "ROBOT"): 1,
(1, "ID"): 2,
(2, ","): 1,
(2, "("): 3,
(3, "NUM"): 4,
(4, ","): 5,
(5, "NUM"): 6,
(6, ")"): 2,
# AVANZAR
(0, "AVANZAR"): 7,
(7, "ID"): 8,
(8, ","): 9,
(9, "NUM"): 10,
# GIRAR
(0, "GIRAR"): 11,
(11, "ID"): 12,
(12, ","): 13,
(13, "IZQ"): 14,
(13, "DER"): 14,
# TELEPORT
(0, "TELEPORT"): 15,
(15, "ID"): 16,
(16, "("): 17,
(17, "NUM"): 18,
(18, ","): 19,
(19, "NUM"): 20,
(20, ")"): 21,
}
final_states = {
2: "ROBOT",
10: "AVANZAR",
14: "GIRAR",
21: "TELEPORT"
}
# Funciones auxiliares
def scanner(command):
return re.findall(r'[a-zA-Z][a-zA-Z\d]*|\d+|[(),]', command)
def MT(q, token):
# PALABRAS RESERVADAS
if token in ["ROBOT", "AVANZAR", "GIRAR", "TELEPORT", "IZQ", "DER"]:
return MT_MATRIX.get((q, token), QE)
# ID
elif re.match(r'^[a-zA-Z][a-zA-Z\d]*$', token):
return MT_MATRIX.get((q, "ID"), QE)
# NUM
elif re.match(r'^\d+$', token):
return MT_MATRIX.get((q, "NUM"), QE)
# OTROS
else:
return MT_MATRIX.get((q, token), QE)
def get_parameters(tokens, command_type):
if command_type == "ROBOT":
params = {}
i = 1
while i < len(tokens):
if i + 1 < len(tokens) and tokens[i+1] == '(':
params[tokens[i]] = (int(tokens[i+2]), int(tokens[i+4]))
i = i + 7
else:
params[tokens[i]] = None
i+=2
return params
elif command_type == "AVANZAR":
return {"ID": tokens[1], "steps": int(tokens[3])}
elif command_type == "GIRAR":
return {"ID": tokens[1], "direction": tokens[3]}
elif command_type == "TELEPORT":
return {"ID": tokens[1], "coords": (int(tokens[3]), int(tokens[5]))}
return {}
# Funciones para los comandos:
def robot(params):
for id, coords in params.items():
if coords:
print(f"Creando robot {id} en coordenadas {coords}")
else:
print(f"Creando robot {id} sin coordenadas")
def avanzar(params):
print(f"El robot {params['ID']} avanza {params['steps']} pasos")
def girar(params):
print(f"El robot {params['ID']} gira hacia la {params['direction']}")
def teleport(params):
print(f"Teleportando robot {params['ID']} a coordenadas {params['coords']}")
def reconocer(q, tokens):
params = get_parameters(tokens, final_states[q])
print("El comando es válido.")
print("Estado final:", final_states[q])
print("Parámetros:", params)
# Llama a la función relevante según el comando:
if final_states[q] == "ROBOT":
robot(params)
elif final_states[q] == "AVANZAR":
avanzar(params)
elif final_states[q] == "GIRAR":
girar(params)
elif final_states[q] == "TELEPORT":
teleport(params)
def error():
print("El comando es inválido.")
def eval_command(command):
tokens = scanner(command)
q = 0
i = 0
print("Tokens:", tokens)
while i < len(tokens) and q != QE:
q = MT(q, tokens[i])
i += 1
# Equivalente a q !=QF
if q in [2, 10, 14, 21]:
reconocer(q, tokens)
else:
error()
def test():
# Pruebas
test_commands = [
"ROBOT A(1,3)",
"ROBOT Robo1(1,3)",
"ROBOT B(2,5), C, D(1,6)",
"ROBOT A,B(2,5),C,D(1,6)",
"AVANZAR Robo1,2",
"AVANZAR A, 3",
"GIRAR A,IZQ",
"TELEPORT A(0,1)",
"TELEPORT A(0,1",
"TELEPORT A(0,1,3)",
]
for command in test_commands:
clear_console()
print("Comando: ", command)
eval_command(command)
input("\nPresione ENTER para continuar...")
def main():
while True:
clear_console()
print("\n--- Menú ---")
print("1. Ingresar comando")
print("2. Test")
print("3. Salir")
opcion = input("Elija una opción: ")
if opcion == "1":
clear_console()
comando = input("\nIngrese su comando: ")
eval_command(comando)
input("\nPresione ENTER para continuar...")
elif opcion == "2":
test()
elif opcion == "3":
clear_console()
break
else:
clear_console()
print("\nOpción no reconocida. Intente de nuevo.")
# Si el script se ejecuta como programa principal, llama a main()
if __name__ == "__main__":
main()