-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntax_analyzer.py
1919 lines (1753 loc) · 61.1 KB
/
syntax_analyzer.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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# @author: Milder Hernandez Cagua
# @author: Fabian David Conejo Piraquive
import re
from sys import stdin
import sys
def initialize_keywords():
global keywords
keywords = {}
keywords["algoritmo"] = "algoritmo"
keywords["finalgoritmo"] = "finalgoritmo"
keywords["proceso"] = "proceso"
keywords["finproceso"] = "finproceso"
keywords["definir"] = "definir"
keywords["como"] = "como"
keywords["numero"] = "numero"
keywords["numerico"] = "numerico"
keywords["entero"] = "entero"
keywords["caracter"] = "caracter"
keywords["real"] = "real"
keywords["logico"] = "logico"
keywords["texto"] = "texto"
keywords["cadena"] = "cadena"
keywords["verdadero"] = "verdadero"
keywords["falso"] = "falso"
keywords["leer"] = "leer"
keywords["escribir"] = "escribir"
keywords["dimension"] = "dimension"
keywords["para"] = "para"
keywords["hasta"] = "hasta"
keywords["con"] = "con"
keywords["paso"] = "paso"
keywords["hacer"] = "hacer"
keywords["finpara"] = "finpara"
keywords["borrar"] = "borrar"
keywords["pantalla"] = "pantalla"
keywords["esperar"] = "esperar"
keywords["tecla"] = "tecla"
keywords["segundos"] = "segundos"
keywords["milisegundos"] = "milisegundos"
keywords["si"] = "si"
keywords["entonces"] = "entonces"
keywords["sino"] = "sino"
keywords["finsi"] = "finsi"
keywords["segun"] = "segun"
keywords["caso"] = "caso"
keywords["de"] = "de"
keywords["otro"] = "otro"
keywords["modo"] = "modo"
keywords["finsegun"] = "finsegun"
keywords["mientras"] = "mientras"
keywords["finmientras"] = "finmientras"
keywords["repetir"] = "repetir"
keywords["hasta"] = "hasta"
keywords["que"] = "que"
keywords["subproceso"] = "subproceso"
keywords["finsubproceso"] = "finsubproceso"
keywords["funcion"] = "funcion"
keywords["finfuncion"] = "finfuncion"
keywords["limpiar"] = "limpiar"
#Operators as keywords
keywords["mod"] = "mod"
keywords["no"] = "no"
keywords["o"] = "o"
keywords["y"] = "y"
def initialize_operators():
global operators
operators = {}
operators["~"] = "token_neg"
operators["="] = "token_igual"
operators["<-"] = "token_asig"
operators["<>"] = "token_dif"
operators["<"] = "token_menor"
operators[">"] = "token_mayor"
operators["<="] = "token_menor_igual"
operators[">="] = "token_mayor_igual"
operators["+"] = "token_mas"
operators["-"] = "token_menos"
operators["/"] = "token_div"
operators["*"] = "token_mul"
operators["%"] = "token_mod"
operators[";"] = "token_pyc"
operators[":"] = "token_dosp"
operators["("] = "token_par_izq"
operators[")"] = "token_par_der"
operators["["] = "token_cor_izq"
operators["]"] = "token_cor_der"
operators["|"] = "token_o"
operators["&"] = "token_y"
operators["o"] = "token_o"
operators[","] = "token_coma"
operators["^"] = "token_pot"
operators["y"] = "token_y"
operators["no"] = "token_neg"
operators["mod"] = "token_mod"
def initialize_alias():
global alias
alias.append(['algoritmo', 'algoritmo'])
alias.append(['borrar', 'borrar'])
alias.append(['cadena', 'cadena'])
alias.append(['caracter', 'caracter'])
alias.append(['caso', 'caso'])
alias.append(['como', 'como'])
alias.append(['con', 'con'])
alias.append(['de', 'de'])
alias.append(['definir', 'definir'])
alias.append(['dimension', 'dimension'])
alias.append(['entero', 'entero'])
alias.append(['entonces', 'entonces'])
alias.append(['escribir', 'escribir'])
alias.append(['esperar', 'esperar'])
alias.append(['falso', 'falso'])
alias.append(['finalgoritmo', 'finalgoritmo'])
alias.append(['finfuncion', 'finfuncion'])
alias.append(['finmientras', 'finmientras'])
alias.append(['finpara', 'finpara'])
alias.append(['finproceso', 'finproceso'])
alias.append(['finsegun', 'finsegun'])
alias.append(['finsi', 'finsi'])
alias.append(['finsubproceso', 'finsubproceso'])
alias.append(['funcion', 'funcion'])
alias.append(['hacer', 'hacer'])
alias.append(['hasta', 'hasta'])
alias.append(['id', 'identificador'])
alias.append(['leer', 'leer'])
alias.append(['limpiar', 'limpiar'])
alias.append(['logico', 'logico'])
alias.append(['mientras', 'mientras'])
alias.append(['milisegundos', 'milisegundos'])
alias.append(['modo', 'modo'])
alias.append(['numerico', 'numerico'])
alias.append(['numero', 'numero'])
alias.append(['otro', 'otro'])
alias.append(['pantalla', 'pantalla'])
alias.append(['para', 'para'])
alias.append(['paso', 'paso'])
alias.append(['proceso', 'proceso'])
alias.append(['que', 'que'])
alias.append(['real', 'real'])
alias.append(['repetir', 'repetir'])
alias.append(['segun', 'segun'])
alias.append(['segundos', 'segundos'])
alias.append(['si', 'si'])
alias.append(['sino', 'sino'])
alias.append(['subproceso', 'subproceso'])
alias.append(['tecla', 'tecla'])
alias.append(['texto', 'texto'])
alias.append(['token_asig', '<-'])
alias.append(['token_cadena', 'valor_cadena'])
alias.append(['token_coma', ','])
alias.append(['token_cor_der', ']'])
alias.append(['token_cor_izq', '['])
alias.append(['token_dif', '<>'])
alias.append(['token_div', '/'])
alias.append(['token_dosp', ':'])
alias.append(['token_entero', 'valor_entero'])
alias.append(['token_igual', '='])
alias.append(['token_mas', '+'])
alias.append(['token_mayor', '>'])
alias.append(['token_mayor_igual', '>='])
alias.append(['token_menor', '<'])
alias.append(['token_menor_igual', '<='])
alias.append(['token_menos', '-'])
alias.append(['token_mod', '%'])
alias.append(['token_mul', '*'])
alias.append(['token_neg', '~'])
alias.append(['token_o', '|'])
alias.append(['token_par_der', ')'])
alias.append(['token_par_izq', '('])
alias.append(['token_pot', '^'])
alias.append(['token_pyc', ';'])
alias.append(['token_real', 'valor_real'])
alias.append(['token_y', '&'])
alias.append(['verdadero', 'verdadero'])
#####################################################################################################
############################################### LEXER ###############################################
#####################################################################################################
def is_other(character):
return re.match("[ \t\n]", character)
def is_digit(character):
return re.match("[0-9]", character)
def is_letter(character):
return re.match("[a-zA-Z]", character)
def is_operator(character):
global operators
return character in operators
def is_string(character):
return character == "\"" or character == "'"
def lex_error():
global row, col
return ">>> Error lexico (linea: " + str(row + 1) + ", posicion: " + str(col + 1) + ")"
def read_comment(line):
global row, col
length_line = len(line)
token = None
advance = 0
index = col
if index + 1 < length_line and line[index + 1] == "/": # It's a comment
token = line[index:]
advance = len(token)
return token, advance
def read_number(line):
global row, col
length_line = len(line)
index = col
token = None
advance = 0
while index < length_line:
character = line[index]
if is_digit(character) or character == ".":
index += 1
elif is_operator(character) or is_other(character): # Read integer number
break
else:
return None, 0 # Lexer error
token = line[col:index]
advance = len(token)
return token, advance
def read_identifier(line):
global row, col
length_line = len(line)
token = ""
advance = 0
index = col
while index < length_line:
character = line[index]
if is_letter(character) or is_digit(character) or character == "_":
index += 1
token += character
else:
break
if token is not None:
advance = len(token)
return token, advance
def read_keyword_identifier(line):
global row, col, keywords
length_line = len(line)
index = col
token = None
advance = 0
type = None
while index < length_line:
character = line[index]
if not is_letter(character):
if is_digit(character) or character == "_":
token, advance = read_identifier(line)
type = "id"
else:
value = line[col:index:].lower()
if value in keywords:
token = keywords[value]
advance = len(token)
type = "token_keyword"
else:
token, advance = read_identifier(line)
type = "id"
break
else:
index += 1
if index == length_line:
value = line[col:index:].lower()
if value in keywords:
token = keywords[value]
advance = len(token)
type = "token_keyword"
else:
token, advance = read_identifier(line)
type = "id"
return type, token, advance
def read_operator(line):
global row, col
length_line = len(line)
index = col
token = None
advance = 0
if index + 1 < length_line and is_operator(line[index:index + 2: 1]):
token = line[index:index + 2:]
advance = 2
else:
token = line[index]
advance = 1
return token, advance
def read_string(line):
global row, col
length_line = len(line)
advance = 0
token = None
index = col + 1 # Skip quote or apostrophe
while index < length_line:
if line[index] == "\"" or line[index] == "'":
token = line[col + 1:index]
return token, len(token) + 2
index += 1
return token, advance
def next_token(line):
global col, operators
character = line[col]
type = None
token = None
advance = 0
# End line there is not token
if col == len(line):
token = ""
advance = 0
type = "fin_linea"
# Space, tab or end line
if token is None and is_other(character):
token = character
advance = 1
type = "token_otro"
# Could be a comment
if token is None and character == "/":
token, advance = read_comment(line)
if token is not None:
type = "token_comentario"
# Integer or real number
if token is None and is_digit(character):
token, advance = read_number(line)
if token is not None:
#Not sure about this (pseint read it right)
if token[-1] == ".":
type = "lex_error"
else:
type = "token_real" if "." in token else "token_entero"
# String
if token is None and is_string(character):
token, advance = read_string(line)
if token is not None:
type = "token_cadena"
# Keyword or identifier
if token is None and is_letter(character):
type, token, advance = read_keyword_identifier(line)
# Operator
if token is None and is_operator(character):
token, advance = read_operator(line)
if token is not None:
type = operators[token]
if token is None:
type = "lex_error"
return type, token, row, col
else:
index = col
col += advance
return type, token, row, index
def build_token(title, lexema, r, c):
global keywords, operators
token = "<"
if lexema in keywords:
if lexema not in operators:
token += lexema
else:
token += operators[lexema]
elif lexema in operators:
token += title
else:
token += title + "," + lexema.lower()
token += "," + str(r) + "," + str(c) + ">"
return token
class Token(object):
def __init__(self, title = None, lexema = None, r = None, c = None):
global keywords, operators
if title != None:
if lexema in keywords:
if lexema not in operators:
self.lexema = lexema
self.type = lexema
else:
self.lexema = operators[lexema]
self.type = operators[lexema]
elif lexema in operators:
self.type = title
self.lexema = lexema
else:
self.lexema = lexema.lower() if lexema != "EOF" else "EOF"
self.type = title
self.row = r
self.col = c
current_token = 0
def get_next_token():
global current_token, tokens
c = current_token
current_token += 1
return tokens[c]
def generate_tokens(input = None, output = None):
global tokens, row, col
lines = []
if input != None:
stdin = open(input, "r")
lines = stdin.readlines()
else:
lines = sys.stdin.readlines()
if output != None:
stdout = open(output, "w")
tokens = []
type = ""
for line in lines:
while col < len(line) and type is not "lex_error":
type, token, x, y = next_token(line)
if type is not "token_otro" and type is not "lex_error" and type is not "token_comentario":
#print build_token(type, token, x + 1, y + 1)
#if output != None:
# stdout.write(build_token(type, token, x + 1, y + 1) + "\n")
tokens.append(Token(type, token, x + 1, y + 1))
if type is "lex_error":
#print lex_error()
#if output != None:
# stdout.write(lex_error() + "\n")
return
row += 1
col = 0
tokens.append(Token("EOF", "EOF", row + 1, col + 1))
tokens = []
alias = []
keywords = {}
operators = {}
initialize_keywords()
initialize_operators()
initialize_alias()
row = 0
col = 0
#####################################################################################################
######################################### SYNTAX ANALYZER ###########################################
#####################################################################################################
def generate_prediction_sets(grammar):
global expected
grammar_array = grammar.split(";")
for g in grammar_array:
temp = g.split(":")
rule = temp[0]
left_part = rule.split("->")[0]
pred = temp[1]
predictions[rule] = pred.split(",")
if left_part not in expected:
expected[left_part] = {}
for p in predictions[rule]:
expected[left_part][p] = True
def syntax_error(expec, token, error_syntax_in_match):
error = ""
error += "<" + str(token.row) + ":" + str(token.col) + "> Error sintactico: se encontro: \"" + token.lexema + "\"; se esperaba: "
if error_syntax_in_match:
for e in alias:
if e[0] in expec:
error += "\"" + e[1] + "\". "
break
else:
for e in alias:
if e[0] in expected[expec]:
error += "\"" + e[1] + "\", "
return error[:-2] + "."
def match(expected_token):
global token
if token.type == expected_token:
token = get_next_token()
else:
print syntax_error(expected_token, token, True)
return False
return True
def PSEINT():
global token
if token.type in predictions["PSEINT->FUNCION_SUBPROC-PROCESO-FUNCION_SUBPROC"]:
if not FUNCION_SUBPROC():
return False
if not PROCESO():
return False
if not FUNCION_SUBPROC():
return False
elif "PSEINT->epsilon" in predictions:
return True
else:
print syntax_error("PSEINT", token, False)
return False
return True
def FUNCION_SUBPROC():
global token
if token.type in predictions["FUNCION_SUBPROC->PROC-FUNCION_SUBPROC"]:
if not PROC():
return False
if not FUNCION_SUBPROC():
return False
elif "FUNCION_SUBPROC->epsilon" in predictions:
return True
else:
print syntax_error("FUNCION_SUBPROC", token, False)
return False
return True
def PROCESO():
global token
if token.type in predictions["PROCESO->INICIO_PROCESO-id-BLOQUE_PROCESO"]:
if not INICIO_PROCESO():
return False
if not match("id"):
return False
if not BLOQUE_PROCESO():
return False
elif "PROCESO->epsilon" in predictions:
return True
else:
print syntax_error("PROCESO", token, False)
return False
return True
def INICIO_PROCESO():
global token
if token.type in predictions["INICIO_PROCESO->proceso"]:
if not match("proceso"):
return False
elif token.type in predictions["INICIO_PROCESO->algoritmo"]:
if not match("algoritmo"):
return False
elif "INICIO_PROCESO->epsilon" in predictions:
return True
else:
print syntax_error("INICIO_PROCESO", token, False)
return False
return True
def BLOQUE_PROCESO():
global token
if token.type in predictions["BLOQUE_PROCESO->BLOQUE-FIN_PROCESO"]:
if not BLOQUE():
return False
if not FIN_PROCESO():
return False
elif "BLOQUE_PROCESO->epsilon" in predictions:
return True
else:
print syntax_error("BLOQUE_PROCESO", token, False)
return False
return True
def FIN_PROCESO():
global token
if token.type in predictions["FIN_PROCESO->finproceso"]:
if not match("finproceso"):
return False
elif token.type in predictions["FIN_PROCESO->finalgoritmo"]:
if not match("finalgoritmo"):
return False
elif "FIN_PROCESO->epsilon" in predictions:
return True
else:
print syntax_error("FIN_PROCESO", token, False)
return False
return True
def PROC():
global token
if token.type in predictions["PROC->INICIO_PROC-id-FIRMA-BLOQUE_PROC"]:
if not INICIO_PROC():
return False
if not match("id"):
return False
if not FIRMA():
return False
if not BLOQUE_PROC():
return False
elif "PROC->epsilon" in predictions:
return True
else:
print syntax_error("PROC", token, False)
return False
return True
def INICIO_PROC():
global token
if token.type in predictions["INICIO_PROC->funcion"]:
if not match("funcion"):
return False
elif token.type in predictions["INICIO_PROC->subproceso"]:
if not match("subproceso"):
return False
elif "INICIO_PROC->epsilon" in predictions:
return True
else:
print syntax_error("INICIO_PROC", token, False)
return False
return True
def BLOQUE_PROC():
global token
if token.type in predictions["BLOQUE_PROC->BLOQUE-FIN_PROC"]:
if not BLOQUE():
return False
if not FIN_PROC():
return False
elif "BLOQUE_PROC->epsilon" in predictions:
return True
else:
print syntax_error("BLOQUE_PROC", token, False)
return False
return True
def FIN_PROC():
global token
if token.type in predictions["FIN_PROC->finfuncion"]:
if not match("finfuncion"):
return False
elif token.type in predictions["FIN_PROC->finsubproceso"]:
if not match("finsubproceso"):
return False
elif "FIN_PROC->epsilon" in predictions:
return True
else:
print syntax_error("FIN_PROC", token, False)
return False
return True
def FIRMA():
global token
if token.type in predictions["FIRMA->token_asig-id-ARG_PROC"]:
if not match("token_asig"):
return False
if not match("id"):
return False
if not ARG_PROC():
return False
elif token.type in predictions["FIRMA->ARG_PROC"]:
if not ARG_PROC():
return False
elif "FIRMA->epsilon" in predictions:
return True
else:
print syntax_error("FIRMA", token, False)
return False
return True
def ARG_PROC():
global token
if token.type in predictions["ARG_PROC->token_par_izq-LISTA_ARG_PROC-token_par_der"]:
if not match("token_par_izq"):
return False
if not LISTA_ARG_PROC():
return False
if not match("token_par_der"):
return False
elif "ARG_PROC->epsilon" in predictions:
return True
else:
print syntax_error("ARG_PROC", token, False)
return False
return True
def LISTA_ARG_PROC():
global token
if token.type in predictions["LISTA_ARG_PROC->id-LISTA_ARG_PROC1"]:
if not match("id"):
return False
if not LISTA_ARG_PROC1():
return False
elif "LISTA_ARG_PROC->epsilon" in predictions:
return True
else:
print syntax_error("LISTA_ARG_PROC", token, False)
return False
return True
def LISTA_ARG_PROC1():
global token
if token.type in predictions["LISTA_ARG_PROC1->token_coma-id-LISTA_ARG_PROC1"]:
if not match("token_coma"):
return False
if not match("id"):
return False
if not LISTA_ARG_PROC1():
return False
elif "LISTA_ARG_PROC1->epsilon" in predictions:
return True
else:
print syntax_error("LISTA_ARG_PROC1", token, False)
return False
return True
def BLOQUE():
global token
if token.type in predictions["BLOQUE->DECLARACION-BLOQUE"]:
if not DECLARACION():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->ASIGNACION_LLAMADA-BLOQUE"]:
if not ASIGNACION_LLAMADA():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->DIMENSION-BLOQUE"]:
if not DIMENSION():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->SI-BLOQUE"]:
if not SI():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->PARA-BLOQUE"]:
if not PARA():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->MIENTRAS-BLOQUE"]:
if not MIENTRAS():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->REPETIR-BLOQUE"]:
if not REPETIR():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->SEGUN-BLOQUE"]:
if not SEGUN():
return False
if not BLOQUE():
return False
elif token.type in predictions["BLOQUE->OTRO-BLOQUE"]:
if not OTRO():
return False
if not BLOQUE():
return False
elif "BLOQUE->epsilon" in predictions:
return True
else:
print syntax_error("BLOQUE", token, False)
return False
return True
def ASIGNACION_LLAMADA():
global token
if token.type in predictions["ASIGNACION_LLAMADA->id-ASIGNACION_LLAMADA1"]:
if not match("id"):
return False
if not ASIGNACION_LLAMADA1():
return False
elif "ASIGNACION_LLAMADA->epsilon" in predictions:
return True
else:
print syntax_error("ASIGNACION_LLAMADA", token, False)
return False
return True
def ASIGNACION_LLAMADA1():
global token
if token.type in predictions["ASIGNACION_LLAMADA1->ASIGNACION1"]:
if not ASIGNACION1():
return False
elif token.type in predictions["ASIGNACION_LLAMADA1->LLAMADA_ID_VOID-token_pyc"]:
if not LLAMADA_ID_VOID():
return False
if not match("token_pyc"):
return False
elif "ASIGNACION_LLAMADA1->epsilon" in predictions:
return True
else:
print syntax_error("ASIGNACION_LLAMADA1", token, False)
return False
return True
def DECLARACION():
global token
if token.type in predictions["DECLARACION->definir-LISTA_DEFINIR_ID-como-TIPO_DATO-token_pyc"]:
if not match("definir"):
return False
if not LISTA_DEFINIR_ID():
return False
if not match("como"):
return False
if not TIPO_DATO():
return False
if not match("token_pyc"):
return False
elif "DECLARACION->epsilon" in predictions:
return True
else:
print syntax_error("DECLARACION", token, False)
return False
return True
def ASIGNACION():
global token
if token.type in predictions["ASIGNACION->id-ASIGNACION1"]:
if not match("id"):
return False
if not ASIGNACION1():
return False
elif "ASIGNACION->epsilon" in predictions:
return True
else:
print syntax_error("ASIGNACION", token, False)
return False
return True
def ASIGNACION1():
global token
if token.type in predictions["ASIGNACION1->token_cor_izq-LISTA_EXPR-token_cor_der-token_asig-EXPRESION-token_pyc"]:
if not match("token_cor_izq"):
return False
if not LISTA_EXPR():
return False
if not match("token_cor_der"):
return False
if not match("token_asig"):
return False
if not EXPRESION():
return False
if not match("token_pyc"):
return False
elif token.type in predictions["ASIGNACION1->token_asig-EXPRESION-token_pyc"]:
if not match("token_asig"):
return False
if not EXPRESION():
return False
if not match("token_pyc"):
return False
elif "ASIGNACION1->epsilon" in predictions:
return True
else:
print syntax_error("ASIGNACION1", token, False)
return False
return True
def DIMENSION():
global token
if token.type in predictions["DIMENSION->dimension-id-LLAMADA_DIM-DIMENSION1-token_pyc"]:
if not match("dimension"):
return False
if not match("id"):
return False
if not LLAMADA_DIM():
return False
if not DIMENSION1():
return False
if not match("token_pyc"):
return False
elif "DIMENSION->epsilon" in predictions:
return True
else:
print syntax_error("DIMENSION", token, False)
return False
return True
def DIMENSION1():
global token
if token.type in predictions["DIMENSION1->token_coma-id-LLAMADA_DIM-DIMENSION1"]:
if not match("token_coma"):
return False
if not match("id"):
return False
if not LLAMADA_DIM():
return False
if not DIMENSION1():
return False
elif "DIMENSION1->epsilon" in predictions:
return True
else:
print syntax_error("DIMENSION1", token, False)
return False
return True
def SI():
global token
if token.type in predictions["SI->si-EXPRESION-entonces-BLOQUE_SI"]:
if not match("si"):
return False
if not EXPRESION():
return False
if not match("entonces"):
return False
if not BLOQUE_SI():
return False
elif "SI->epsilon" in predictions:
return True
else:
print syntax_error("SI", token, False)
return False
return True
def BLOQUE_SI():
global token
if token.type in predictions["BLOQUE_SI->BLOQUE-SI1"]:
if not BLOQUE():
return False
if not SI1():
return False
elif "BLOQUE_SI->epsilon" in predictions:
return True
else:
print syntax_error("BLOQUE_SI", token, False)
return False
return True
def SI1():
global token
if token.type in predictions["SI1->sino-BLOQUE_SI1"]:
if not match("sino"):
return False
if not BLOQUE_SI1():
return False
elif token.type in predictions["SI1->finsi"]:
if not match("finsi"):
return False
elif "SI1->epsilon" in predictions:
return True