-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProyecto 0.py
927 lines (837 loc) · 35.3 KB
/
Proyecto 0.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
import re
TOKENS = [
(r'defVar', 'DEFVAR'),
(r'defProc', 'DEFPROC'),
(r'jump|walk|leap|turn|turnto|drop|get|grap|letGo|nop', 'INSTRUCTIONS'),
(r'if|else|while|repeat|times', 'CONDITIONAL'),
(r'either|front|right|left|back|around', 'DIRECTION'),
(r'north|south|west|east', 'FACING'),
(r'can', 'CAN'),
(r'not', 'NOT'),
(r'\)|\(', 'PAREN'),
(r'\{|\}', 'BRACE'),
(r',', 'COMMA'),
(r';', 'SEMICOLON'),
(r'[a-zA-Z_]\w*', 'IDENTIFIER'),
(r'\d+', 'NUMBER'),
(r'=', 'EQUALS'),
]
dicc_defVar={ }
list_intruction=['jump','walk','leap','turn','turnto','drop','grap','letGo','nop']
list_conditional=['if','else','while','repeat','times']
def tokenize(program):
tokens = []
pattern = '|'.join(f'(?P<{name}>{regex})' for regex, name in TOKENS)
for match in re.finditer(pattern, program, re.IGNORECASE):
token_type = match.lastgroup
token_value = match.group()
tokens.append((token_type, token_value.lower()))
return tokens
def validate_program(tokens):
i = 0
result=True
while i < len(tokens):
token_type, token_value = tokens[i]
if token_type == 'DEFVAR':
if i + 1 < len(tokens):
next_token = tokens[i + 1]
next_token_type, next_value = next_token
if next_token_type != 'IDENTIFIER':
return False
else:
if i + 2 < len(tokens):
value = tokens[i + 2]
value_type, value_value = value
if value_type not in ['NUMBER', 'IDENTIFIER']:
return False
else:
i+=2
dicc_defVar[next_value]= value_value
else:
return False
else:
return False
elif token_type == 'IDENTIFIER':
result, val= parse_identifier(tokens, i)
if not result:
return False
else:
i=val
elif token_type == 'DEFPROC':
result, i = parse_deproc(tokens, i)
if not result:
return False
elif token_type== 'INSTRUCTIONS':
result, i = parse_instructions(tokens, i)
if not result:
return False
elif token_type == 'CONDITIONAL':
result, i = parse_conditionals(tokens, i)
if not result:
return False
elif token_type == '{':
i+=1
result, i= ver_deproc(tokens, i)
elif token_type == '}':
return False
i+=1
return result
def ver_deproc(lista, pos):
variable= lista[pos]
variable_type, variable_value= variable
if variable_type == 'INSTRUCTIONS':
result , pos =parse_instructions(lista, pos)
if not result:
return False, pos
elif variable_type == 'CONDITIONAL':
result, pos = parse_conditionals(lista, pos)
if not result:
return False, pos
elif variable_type== 'IDENTIFIER':
result, pos= parse_identifier(lista, pos)
if not result:
return False, pos
elif variable_type == 'DEFPROC':
result, pos= parse_identifier(lista, pos)
if not result:
return False, pos
elif variable_value =="}":
return True, pos
elif variable_value ==";":
return ver_deproc (lista, pos+1)
else:
return False, pos
return ver_deproc (lista, pos+1)
def parse_deproc(lista, pos):
if pos+1< len(lista):
pos+=1
next_token= lista[pos]
next_token_type, next_value = next_token
if next_token_type!= 'IDENTIFIER':
return False, pos
else:
result, pos= parse_identifier(lista, pos )
if not result:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
abrir = lista[pos]
abrir_type, abrir_value= abrir
if abrir_value != '{':
return False, pos
else:
if pos+1<len(lista):
pos+=1
result, pos = ver_deproc(lista, pos)
if not result:
return False, pos
else:
return True, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
def parse_identifier(lista, pos):
token= lista[pos]
token_type, token_value= token
before_token = lista[pos-1]
before_type, before_value = before_token
if before_type == 'DEFVAR':
if pos + 1 < len(lista):
value = lista[pos+1]
value_type, value_value = value
if value_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
else:
dicc_defVar[token_value]=value_type
else:
return False, pos
elif before_type == 'DEFPROC':
if pos + 1 < len(lista):
value = lista[pos+1]
value_type, value_value = value
pos+=1
if value_type not in ['PAREN']:
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
siguiente= lista[pos]
siguiente_type, siguiente_value = siguiente
if siguiente_type in ['NUMBER', 'IDENTIFIER']:
if pos + 1 < len(lista):
if pos + 1 < len(lista):
pos+=1
coma= lista[pos]
coma_type, coma_value = coma
if coma_type not in ['COMMA', 'PAREN']:
return False, pos
else:
if coma_type not in ['PAREN']:
if pos + 1 < len(lista):
pos+=1
val2= lista[pos]
val2_type, val2_value = val2
if val2_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
siguiente2= lista[pos]
siguiente2_type, siguiente2_value = siguiente2
if siguiente2_type not in ['PAREN']:
return False, pos
else:
dicc_defVar[token_value]= siguiente_value, val2_value
dicc_defVar[siguiente_value]=0
dicc_defVar[val2_value]=0
else:
return False, pos
else:
return False, pos
else:
dicc_defVar[token_value]= val1_value
else:
return False, pos
else:
if siguiente_type not in ['PAREN']:
return False, pos
else:
dicc_defVar[token_value]= 0
else:
return False, pos
else:
return False, pos
else:
if token not in dicc_defVar.keys() or dicc_defVar.values():
if pos + 1 < len(lista):
pos+=1
value = lista[pos]
value_type, value_value = value
if value_type not in ['PAREN']:
if pos + 1 < len(lista):
pos+=1
siguiente= lista[pos]
siguiente_type, siguiente_value = siguiente
if siguiente_type not in ['EQUALS']:
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
val1=lista[pos]
val1_type, val1_value = val1
if val1_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
else:
if pos+1<len(lista):
pos+=1
pun_co=lista[pos]
pun_co_type,pun_co_value=pun_co
if pun_co_type not in ['SEMICOLON']:
return False, pos
else:
dicc_defVar[token_value]= val1_value
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
val1=lista[pos]
val1_type, val1_value = val1
if val1_type not in ['NUMBER', 'IDENTIFIER']:
if val1_type not in ['PAREN']:
return False, pos
else:
dicc_defVar[token_value] = 0
else:
if pos + 1 < len(lista):
pos+=1
siguiente2= lista[pos]
siguiente2_type, siguiente2_value = siguiente2
if siguiente2_type not in ['COMMA', 'PAREN']:
return False, pos
else:
if siguiente2_type not in ['COMMA']:
dicc_defVar[token_value] = val1_value
else:
if pos + 1 < len(lista):
pos+=1
val2=lista[pos]
val2_type, val2_value = val2
if val2_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
siguiente2= lista[pos]
siguiente2_type, siguiente2_value = siguiente2
if siguiente2_type not in ['PAREN']:
return False, pos
else:
dicc_defVar[token_value]=val1, val2
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
return True,pos
def parse_instructions(lista, pos):
token= lista[pos]
token_type, token_value= token
if token_value == 'jump':
result, pos=parse_jump(lista, pos)
if not result:
return False, pos
elif token_value == 'walk' or token_value == 'leap':
result, pos = parse_walk_or_leap(lista, pos)
if not result:
return False, pos
elif token_value == 'drop' or token_value == 'get' or token_value == 'grap' or token_value == 'letgo':
pos+=1
result, pos= ver_one_paren(lista, pos)
if not result:
return False, pos
elif token_value == 'nop':
pos+=1
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
paren2= lista[pos]
paren2_type, paren2_value=paren2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
elif token_value == 'turn':
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num1= lista[pos]
num1_type, num1_value=num1
if num1_type not in ['DIRECTION']:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren_type not in ['PAREN']:
return False, pos
elif token_value == 'turnto':
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num1= lista[pos]
num1_type, num1_value=num1
if num1_type not in ['FACING']:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren_type not in ['PAREN']:
return False, pos
else:
return False, pos
return True, pos
def parse_jump(lista, pos):
if pos+1 <len(lista):
pos+=1
result, pos = ver_two_paren_val(lista, pos)
if not result:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
semicolon= lista[pos]
semicolon_type, semicolon_value=semicolon
if semicolon_type in ['SEMICOLON']:
return True, pos
else:
return False, pos
else:
return False, pos
def parse_walk_or_leap(lista, pos):
pos+=1
result, num = ver_one_paren(lista, pos)
if not result:
result, num = ver_two_paren_O(lista, pos)
if not result:
result, num = ver_two_paren_D(lista, pos)
if not result:
return False, pos
else:
pos = num
return True, pos
else:
pos = num
return True, pos
else:
pos = num
return True, pos
def parse_conditionals(lista, pos):
token= lista[pos]
token_type, token_value= token
if token_value == 'while':
result, pos = parce_while(lista, pos)
if not result:
return False, pos
else:
return True, pos
elif token_value == 'if':
result, pos = parce_if(lista, pos)
if not result:
return False, pos
else:
return True, pos
elif token_value == 'repeat':
result, pos = parce_repeat(lista, pos)
else:
return False, pos
def parce_while(lista, pos):
if pos +1 < len(lista):
pos+=1
can= lista[pos]
can_type, can_value= can
if can_type != 'CAN':
return False, pos
else:
result, pos= parse_can(lista, pos)
if not result:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
brace= lista[pos]
brace_type, brace_value= brace
if brace_value!="{":
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
valor= lista[pos]
valor_type, valor_value= valor
if valor_type == 'INSTRUCTIONS':
result, pos = parse_instructions(lista, pos)
if not result:
return False, pos
else:
if pos+ 1< len(lista):
pos+=1
brace2= lista[pos]
brace2_type, brace2_value= brace2
if brace2_value!="}":
return False, pos
else:
return False, pos
elif valor_value != '}':
return False, pos
else:
return False, pos
else:
return False, pos
return True, pos
def parce_if(lista, pos):
if pos +1 < len(lista):
pos+=1
can= lista[pos]
can_type, can_value= can
if can_type != 'CAN':
return False, pos
else:
result, pos= parse_can(lista, pos)
if not result:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
brace= lista[pos]
brace_type, brace_value= brace
if brace_value!="{":
return False, pos
else:
if pos +1< len(lista):
pos+=1
result, pos = ver_deproc(lista, pos)
if not result:
return False, pos
else:
if pos+1 < len(lista):
num_ver = pos +1
hay_else= lista[num_ver]
hay_else_type, hay_else_value= hay_else
if hay_else_value == 'else':
pos = num_ver
if pos+1 < len(lista):
pos+=1
brace2= lista[pos]
brace2_type, brace2_value= brace2
if brace2_value!="{":
return False, pos
else:
if pos +1< len(lista):
pos+=1
result, pos = ver_deproc(lista, pos)
if not result:
return False, pos
else:
return False, pos
else:
return False, pos
return True, pos
def parce_repeat(lista, pos):
if pos+1<len(lista):
pos+=1
num = lista[pos]
num_type, num_value= num
if num_type != 'NUMBER':
return False, pos
else:
if pos+1<len(lista):
pos+=1
times = lista[pos]
times_type, times_value= num
if times_value != 'times':
return False, pos
else:
if pos+1 <len(lista):
pos+=1
brace= lista[pos]
brace_type, brace_value= brace
if brace_value!="{":
return False, pos
else:
if pos +1< len(lista):
pos+=1
result, pos = ver_deproc(lista, pos)
if not result:
return False, pos
else:
if pos +1 < len(lista):
pos+=1
brace= lista[pos]
brace_type, brace_value= brace
if brace_value!='}':
return False, pos
else:
return False, pos
return True, pos
def parse_can(lista, pos):
pos+=1
parent= lista[pos]
parent_type, parent_value= parent
if parent_value!= '(':
return False, pos
else:
if pos + 1 < len(lista):
pos+=1
instruc= lista[pos]
instruc_type, instruc_value= instruc
if instruc_value in list_intruction:
result, pos = parse_instructions(lista, pos)
if not result:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
parent2= lista[pos]
parent2_type, parent2_value= parent2
if parent2_value == ')':
return True, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
def ver_one_paren(lista, pos):
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num1= lista[pos]
num1_type, num1_value=num1
if num1_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
elif num1_type== 'IDENTIFIER' and ((num1_value in dicc_defVar.keys()) or (num1_value in dicc_defVar.values())):
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
elif num1_type== 'NUMBER':
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
return True, pos
def ver_two_paren_val(lista, pos):
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num1= lista[pos]
num1_type, num1_value=num1
if num1_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
elif num1_type== 'IDENTIFIER' and (num1_value in dicc_defVar.keys() or num1_value in dicc_defVar.values()):
if pos+1 < len(lista):
pos+=1
coma=lista[pos]
coma_type, coma_value= coma
if coma_type not in ['COMMA']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num2=lista[pos]
num2_type, num2_value= num2
if num2_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
elif num2_type== 'IDENTIFIER' and (num2_value in dicc_defVar.keys or num2_value in dicc_defVar.values):
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
elif num2_type == 'NUMBER':
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
elif num1_type == 'NUMBER':
if pos+1 < len(lista):
pos+=1
coma=lista[pos]
coma_type, coma_value= coma
if coma_type not in ['COMMA']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num2=lista[pos]
num2_type, num2_value= num2
if num2_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
elif num2_type== 'IDENTIFIER' and (num2_value in dicc_defVar.keys or num2_value in dicc_defVar.values):
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
elif num2_type == 'NUMBER':
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
return True, pos
def ver_two_paren_O(lista, pos):
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num1= lista[pos]
num1_type, num1_value=num1
if num1_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
elif num1_type== 'IDENTIFIER' and (num1_value in dicc_defVar.keys() or num1_value in dicc_defVar.values()):
if pos+1 < len(lista):
pos+=1
coma=lista[pos]
coma_type, coma_value= coma
if coma_type not in ['COMMA']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num2=lista[pos]
num2_type, num2_value= num2
if num2_type not in ['FACING']:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
elif num1_type == 'NUMBER':
if pos+1 < len(lista):
pos+=1
coma=lista[pos]
coma_type, coma_value= coma
if coma_type not in ['COMMA']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num2=lista[pos]
num2_type, num2_value= num2
if num2_type not in ['FACING']:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
return True, pos
def ver_two_paren_D(lista, pos):
parent= lista[pos]
paren_type, paren_value= parent
if paren_type not in ['PAREN']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num1= lista[pos]
num1_type, num1_value=num1
if num1_type not in ['NUMBER', 'IDENTIFIER']:
return False, pos
elif num1_type== 'IDENTIFIER' and (num1_value in dicc_defVar.keys() or num1_value in dicc_defVar.values()):
if pos+1 < len(lista):
pos+=1
coma=lista[pos]
coma_type, coma_value= coma
if coma_type not in ['COMMA']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num2=lista[pos]
num2_type, num2_value= num2
if num2_type not in ['DIRECTION']:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
else:
return False, pos
elif num1_type== 'NUMBER':
if pos+1 < len(lista):
pos+=1
coma=lista[pos]
coma_type, coma_value= coma
if coma_type not in ['COMMA']:
return False, pos
else:
if pos+1 < len(lista):
pos+=1
num2=lista[pos]
num2_type, num2_value= num2
if num2_type not in ['DIRECTION']:
return False, pos
else:
if pos+1 <len(lista):
pos+=1
parent2 = lista[pos]
paren2_type, parent2_value= parent2
if paren2_type not in ['PAREN']:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
else:
return False, pos
return True, pos
def main():
with open('data/programa.txt', 'r') as file:
program = file.read()
tokens = tokenize(program)
try:
result = validate_program(tokens)
if result == True:
print("Yes")
else:
print("No")
except SyntaxError as e:
print(f"Syntax Error: {e}")
if __name__ == '__main__':
main()