/
vslp.py
574 lines (484 loc) · 20.7 KB
/
vslp.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
# -*- coding: utf-8 -*-
"""
VS.LP
Very Simple Language Parser
for my Compilers course at Tec de Monterrey, Campus Querétaro
coded by avathar & grevych
Lexical class for analex
Paser class for anasin
Quadruples for intermediate code
Generator for output code
follow us: @avatharBot @grevych
"""
import sys
import re
import util
from collections import namedtuple
class Lexical:
def __init__(self, filenames, debugger):
self.source = None #sources
self.filenames = filenames
try:
self.source = open(self.filenames) # or "a+", whatever you need
except IOError:
print "Could not open file!"
return
#mandar llamar instancia de error y terminar
self.line = 0
self.status = ""
self.flags = {}
self.debugger = debugger
self.terminals = []
self.nonterminals = []
self.parsing_table = []
TokenWithDelimiter = namedtuple('Token', ('name', 'regexp', 'regexp_at_start', ))
Token = namedtuple('Token', ('name', 'regexp', ))
self.tokens = [Token(
token[0],
re.compile(token[1])) for token in util.TOKENS]
self.tokens_with_del = [TokenWithDelimiter(
token[0],
re.compile(token[1]),
re.compile(token[2])) for token in util.TOKENS_WITH_DELIMITERS]
self.t = []
self.d = []
def get_grammar():
return self.__grammar
def run(self):
with self.source:
buffer_str = ""
buffer_str_del = ""
is_token_with_del = False
for char in iter(lambda: self.source.read(1), ''):
if not char.isspace():
self.line = (self.line, self.line + 1)[char.count('\n')]
buffer_str += char
continue
if is_token_with_del:
buffer_str_del += buffer_str + char
for token in self.tokens_with_del:
match = token.regexp.match(buffer_str_del)
if match:
print match.group()
if token.name == 'ID' or token.name == 'NUM':
self.d.append(token.name + "," + match.group())
else:
self.d.append(token.name)
self.t.append(token.name.lower())
buffer_str = buffer_str_del.replace(match.group(), '', 1)
self.source.seek(- buffer_str.__len__(), 1)
buffer_str_del = buffer_str = ""
is_token_with_del = False
break
else:
for token in self.tokens_with_del:
match = token.regexp_at_start.match(buffer_str)
if match:
print match.group() + " NUEVO EN DELIM"
is_token_with_del = True
buffer_str_del = buffer_str
buffer_str = ""
break
is_token_with_del = False
if not is_token_with_del:
while buffer_str:
has_match = False
for token in self.tokens:
match = token.regexp.match(buffer_str)
if match:
if token.name == 'ID' or token.name == 'NUM':
self.d.append(token.name + "," + match.group())
else:
self.d.append(token.name)
self.t.append(token.name.lower())
buffer_str = buffer_str.replace(match.group(), '', 1)
print match.group() + "\t\t NUEVO" + buffer_str
has_match = True
break
if not has_match:
print "NO ACEPTADO"
return
if is_token_with_del:
buffer_str_del += buffer_str + char
for token in self.tokens_with_del:
match = token.regexp.match(buffer_str_del)
if match:
print match.group()
if token.name == 'ID' or token.name == 'NUM':
self.d.append(token.name + "," + match.group())
else:
self.d.append(token.name)
self.t.append(token.name.lower())
buffer_str_del = buffer_str = ""
is_token_with_del = False
break
else:
for token in self.tokens_with_del:
match = token.regexp_at_start.match(buffer_str)
if match:
print match.group() + " NUEVO EN DELIM"
is_token_with_del = True
buffer_str_del = buffer_str
buffer_str = ""
break
is_token_with_del = False
if not is_token_with_del:
while buffer_str:
has_match = False
for token in self.tokens:
match = token.regexp.match(buffer_str)
if match:
if token.name == 'ID' or token.name == 'NUM':
self.d.append(token.name + "," + match.group())
else:
self.d.append(token.name)
self.t.append(token.name.lower())
buffer_str = buffer_str.replace(match.group(), '', 1)
print match.group() + " NUEVO" + buffer_str
has_match = True
break
if not has_match:
print "NO ACEPTADO"
return
if buffer_str:
print buffer_str
print "NO ACEPTADO"
return
print "\nAnalisis Lexico: Cadena Aceptada\n"
def anasin(self):
if not util.GRAMMAR.__len__():
print "Error en gramática -> inexistente"
return
self.__grammar = [{production[0]: production[1]} for production in util.GRAMMAR]
self.__next = {}
self.__first = {}
self.terminals = []
self.nonterminals = []
self.parsing_table = []
for production in util.GRAMMAR:
self.__get_first(production[0])
self.__get_next(production[0])
self.get_terminals()
self.get_nonterminals()
self.__set_parsing_table()
print 'GRAMATICA'
for production in util.GRAMMAR:
print "\t%s -> %s" % (production[0], production[1])
print 'PRIMEROS'
for key in self.__first:
print "\t%s = %s" % (key, self.__first[key])
print 'SIGUIENTES'
for key in self.__next:
print "\t%s = %s" % (key, self.__next[key])
print 'TERMINALES = ', self.terminals
print 'NO TERMINALES = ', self.nonterminals
self.print_table()
def print_table(self):
print "\ntabla ll \n".upper()
for row in self.parsing_table:
string = ""
for index in row:
try:
string = string + "[%2d ]" % index
except TypeError:
string = string + "[eps]"
print string
def get_terminals(self):
for production in util.GRAMMAR:
for element in production[1].split():
if element.islower() and element not in self.terminals and element.find('epsilon'):
self.terminals.append(element)
self.terminals.append('$')
def get_nonterminals(self):
for production in util.GRAMMAR:
if production[0] not in self.nonterminals:
self.nonterminals.append(production[0])
def __set_parsing_table(self):
for nonterminal in self.nonterminals:
self.parsing_table.append([-1 for terminal in self.terminals])
index_table = 0
for production in util.GRAMMAR:
x = self.nonterminals.index(production[0])
element = production[1].split()[0]
if not element.find('epsilon'):
y = [self.terminals.index(elem) for elem in self.__next[production[0]] if elem != 'epsilon'] #diferente de epsilon
for index in y:
if self.parsing_table[x][index] == -1:
self.parsing_table[x][index] = 'epsilon' #'epsilon'
elif self.parsing_table[x][y] != index_table:
print "Error en tabla de parsing__ %d" % (index_table, )
return
elif element in self.terminals:
y = self.terminals.index(element)
if self.parsing_table[x][y] == -1:
self.parsing_table[x][y] = index_table
elif self.parsing_table[x][y] != index_table:
print "Error en tabla de parsing_ %d %d %d %s" % (index_table, x, y, element)
return
else:
y = [self.terminals.index(elem) for elem in self.__first[element] if elem != 'epsilon'] #diferente de epsilon
for index in y:
if self.parsing_table[x][index] == -1:
self.parsing_table[x][index] = index_table
elif self.parsing_table[x][y] != index_table:
print "Error en tabla de parsing %d" % (index_table, )
return
index_table = index_table + 1
def __get_first(self, key):
if self.__first.has_key(key):
return self.__first[key]
self.__first[key] = []
for production in self.__grammar:
if production.has_key(key):
elements = production[key].split()
if elements[0] != key:
if elements[0].islower():
self.__first[key].append(elements[0])
else:
epsilons = 0
is_epsilon = False
for element in elements:
for first in self.__get_first(element):
if not first.find('epsilon'):
epsilons = epsilons + 1
is_epsilon = True
continue
if not first in self.__first[key]:
self.__first[key].append(first)
if not is_epsilon:
break
is_epsilon = False
if epsilons == elements.__len__():
if not 'epsilon' in self.__first[key]:
self.__first[key].append('epsilon')
return self.__first[key]
def __get_next(self, key):
if self.__next.has_key(key):
return self.__next[key]
self.__next[key] = []
if key == util.GRAMMAR[0][0] and '$' not in self.__next[key]:
self.__next[key].append('$')
for production in self.__grammar:
elements = production.values()[0].split()
index = 0
for element in elements:
if element.__eq__(key):
if index == elements.__len__() - 1:
if production.keys()[0] != key:
for next in self.__get_next(production.keys()[0]):
if next not in self.__next[key]:
self.__next[key].append(next)
else:
if elements[index + 1].islower():
self.__next[key].append(elements[index + 1])
else:
is_epsilon = False
for next in self.__get_first(elements[index + 1]):
if not next.find('epsilon'):
is_epsilon = True
continue
if next not in self.__next[key]:
self.__next[key].append(next)
if is_epsilon:
for next in self.__get_next(production.keys()[0]):
if next not in self.__next[key]:
self.__next[key].append(next)
index = index + 1
return self.__next[key]
class Parser:
def __init__(self, lex):
"""
Parser object to read grammar and make an syntax parser
"""
self.lex = lex
self.grammar = [production[1].split() for production in util.GRAMMAR]
self.intercode = lex.d
self.did_pass_syntax_analysis = False
self.string = []
self.terminal = lex.terminals
self.non_terminal = lex.nonterminals
self.non_terminal.extend(self.terminal)
#hard coded for project, don't panic
self.parsing_table = lex.parsing_table
def show_grammar(self):
"""
so we can take a look at parser grammar
very straightforward
"""
print " --> Grammar <-- "
for rule in self.grammar:
print rule
def start_parser(self):
"""
A modified LL(1) algorithm for string parsing with this language grammar
Using list indexes references to match grammar rules and parsing table
"""
# starting symbol
stack = ["A"]
# non_terminal list representing user input
self.string = lex.t
self.string.reverse()
while len(self.string) > 0:
print stack
# getting input and stack top elements
token = self.string.pop()
token_index = self.terminal.index(token)
top = stack.pop( )
top_index = self.non_terminal.index(top)
# accept token , build intermediate code and continue analysis
if top == token:
self.build_intercode(token)
continue
# return token to input string
if top != token:
self.string.append(token)
# get grammar rule from parsing table
try:
production = self.parsing_table[top_index][token_index]
except IndexError:
print " --> Syntax error: expecting -> [ %s ] got [ %s ]" % (top, token)
exit( )
# report error and exit
if production == -1:
print " --> Syntax error on grammar rule -> %s " % self.grammar[top_index]
exit( )
# add grammar rule elements to stack
else:
rule = self.grammar[production]
rule.reverse( )
for r in rule: stack.append(r)
rule.reverse()
print "\nAnalisis Sintactico: Cadena Aceptada\n"
self.did_pass_syntax_analysis = True
def build_intercode(self,token):
pass
def show_intercode(self):
print self.intercode
class Quadruples:
"""
Quadruples generator from intermediate code
"""
def __init__(self, intercode):
self.intercode = intercode
self.intercode.reverse()
self.quadruple = ""
self.inner_vars = 0
self.in_template = False
self.line = 0
self.vars = []
def show_quadruples(self):
print " Cuadruplos que describen la cadena de entrada: "
print self.quadruple
def get_var_value(self,identifier):
par = identifier.split(",")
if par[0] == "NUM":
try:
value = int(par[1])
except ValueError:
print "Error de sintaxis, se esperaba numero"
elif self.in_template:
if not identifier in self.vars:
print "Error de sintaxis, variable [ %s ] no declarada previamente" % par[1]
print self.vars
exit()
elif par[0] == "ID" and not self.in_template:
self.vars.append(identifier)
return par[1]
def add_quadruple(self, one = "_", two = "_", three = "_", four = "_"):
self.quadruple = "%s (%s, %s, %s, %s)\n" % (self.quadruple, one, two, three, four)
self.line = self.line + 1
def inner_quadruples(self, delimiter, current):
token = self.intercode.pop()
while(token != delimiter):
if token == "SET" or token == "STYLE":
next = self.intercode.pop()
if next == "LPAR" and token == "STYLE":
self.style_constructor(token)
elif next == "LPAR" and token == "SET":
self.set_constructor(token)
else:
self.assignment(token, next)
elif token == "COMMA":
self.comma()
elif token == "RPAR":
# consume symbol
dot_comma = self.intercode.pop()
elif token == "ININEST":
self.inner_quadruples("ENDNEST",current + 1)
elif token == "FOR":
self.for_loop(token)
elif token == "SELE" or token == "APOS":
pass
else:
self.id(token)
token = self.intercode.pop()
def id(self, token):
self.add_quadruple("{% %}",self.get_var_value(token),four="#template")
def for_loop(self, token):
inipar = self.intercode.pop()
num1 = self.intercode.pop().split(",")[1]
comma = self.intercode.pop()
num2 = self.intercode.pop().split(",")[1]
endpar = self.intercode.pop()
inikey = self.intercode.pop()
self.add_quadruple("for",num1,num2,token)
self.inner_quadruples("RKEY", 1)
def comma(self):
attr = self.intercode.pop().split(",")[1]
two_dots = self.intercode.pop()
value = self.intercode.pop().split(",")[1]
self.add_quadruple(":", value, four=attr)
def set_constructor(self, obj_type):
name = self.intercode.pop()
self.add_quadruple("()",name,four=obj_type)
def style_constructor(self, obj_type):
apos = self.intercode.pop()
name = self.intercode.pop()
self.add_quadruple("()",name,four=obj_type)
def assignment(self, obj_type, var_name):
# consuming equals symbol
self.intercode.pop()
self.add_quadruple("=",self.get_var_value(var_name),four=obj_type)
def start_quadruples(self):
token = self.intercode.pop()
# full input
if token == "INISET":
self.add_quadruple(four = "#set")
nametag = self.intercode.pop()
name = self.get_var_value(self.intercode.pop())
self.add_quadruple(":", name, four = "#set" )
self.inner_quadruples("ENDSET",3)
self.add_quadruple(four = "#endset")
# full input so we have template section
token = self.intercode.pop()
self.add_quadruple(four = "#template" )
nametag = self.intercode.pop()
name = self.get_var_value(self.intercode.pop())
self.add_quadruple(":", name, four = "#set" )
self.in_template = True
self.inner_quadruples("ENDTEMP",3)
self.add_quadruple(four = "#endtemplate")
else:
self.add_quadruple(four="#template")
nametag = self.intercode.pop()
name = self.get_var_value(self.intercode.pop())
self.add_quadruple(":",name,four="#template")
self.inner_quadruples("ENDTEMP",3)
self.add_quadruple(four="#endtemplate")
print "\nAnalisis Semantico: Cadena Aceptada\n"
class Generator:
"""docstring for Generator"""
def __init__(self, arg):
self.arg = arg
# Lexical object
lex = Lexical("prueba", None)
lex.run()
lex.anasin()
# Parser object
parser = Parser( lex )
print "\nPila del parser LL\n"
parser.start_parser()
# Quadruples object
quad = Quadruples( parser.intercode )
quad.start_quadruples()
quad.show_quadruples()