-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammar.py
512 lines (351 loc) · 12.1 KB
/
Grammar.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
import json
class Symbol(object):
def __init__(self, name, grammar):
self.Name = name
self.Grammar = grammar
def __str__(self):
return self.Name
def __repr__(self):
return repr(self.Name)
def __add__(self, other):
if isinstance(other, Symbol):
return Sentence(self, other)
raise TypeError(other)
def __or__(self, other):
if isinstance(other, (Sentence)):
return SentenceList(Sentence(self), other)
raise TypeError(other)
@property
def IsEpsilon(self):
return False
def __len__(self):
return 1
class NonTerminal(Symbol):
def __init__(self, name, grammar):
super().__init__(name, grammar)
self.productions = []
def __imod__(self, other):
if isinstance(other, (Sentence)):
p = Production(self, other)
self.Grammar.Add_Production(p)
return self
if isinstance(other, tuple):
assert len(other) > 1
if len(other) == 2:
other += (None,) * len(other[0])
assert len(other) == len(other[0]) + 2, "Debe definirse una, y solo una, regla por cada símbolo de la producción"
# assert len(other) == 2, "Tiene que ser una Tupla de 2 elementos (sentence, attribute)"
if isinstance(other[0], Symbol) or isinstance(other[0], Sentence):
p = AttributeProduction(self, other[0], other[1:])
else:
raise Exception("")
self.Grammar.Add_Production(p)
return self
if isinstance(other, Symbol):
p = Production(self, Sentence(other))
self.Grammar.Add_Production(p)
return self
if isinstance(other, SentenceList):
for s in other:
p = Production(self, s)
self.Grammar.Add_Production(p)
return self
raise TypeError(other)
@property
def IsTerminal(self):
return False
@property
def IsNonTerminal(self):
return True
@property
def IsEpsilon(self):
return False
class Terminal(Symbol):
def __init__(self, name, grammar):
super().__init__(name, grammar)
@property
def IsTerminal(self):
return True
@property
def IsNonTerminal(self):
return False
@property
def IsEpsilon(self):
return False
class EOF(Terminal):
def __init__(self, Grammar):
super().__init__('$', Grammar)
class Sentence(object):
def __init__(self, *args):
self._symbols = tuple(x for x in args if not x.IsEpsilon)
self.hash = hash(self._symbols)
def __len__(self):
return len(self._symbols)
def __add__(self, other):
if isinstance(other, Symbol):
return Sentence(*(self._symbols + (other,)))
if isinstance(other, Sentence):
return Sentence(*(self._symbols + other._symbols))
raise TypeError(other)
def __or__(self, other):
if isinstance(other, Sentence):
return SentenceList(self, other)
if isinstance(other, Symbol):
return SentenceList(self, Sentence(other))
raise TypeError(other)
def __repr__(self):
return str(self)
def __str__(self):
return ("%s " * len(self._symbols) % tuple(self._symbols)).strip()
def __iter__(self):
return iter(self._symbols)
def __getitem__(self, index):
return self._symbols[index]
def __eq__(self, other):
return self._symbols == other._symbols
def __hash__(self):
return self.hash
@property
def IsEpsilon(self):
return False
class SentenceList(object):
def __init__(self, *args):
self._sentences = list(args)
def Add(self, symbol):
if not symbol and (symbol is None or not symbol.IsEpsilon):
raise ValueError(symbol)
self._sentences.append(symbol)
def __iter__(self):
return iter(self._sentences)
def __or__(self, other):
if isinstance(other, Sentence):
self.Add(other)
return self
if isinstance(other, Symbol):
return self | Sentence(other)
class Epsilon(Terminal, Sentence):
def __init__(self, grammar):
super().__init__('epsilon', grammar)
def __str__(self):
return "e"
def __repr__(self):
return 'epsilon'
def __iter__(self):
yield from ()
def __len__(self):
return 0
def __add__(self, other):
return other
def __eq__(self, other):
return isinstance(other, (Epsilon,))
def __hash__(self):
return hash("")
@property
def IsEpsilon(self):
return True
class Production(object):
def __init__(self, nonTerminal, sentence):
self.Left = nonTerminal
self.Right = sentence
def __str__(self):
return '%s := %s' % (self.Left, self.Right)
def __repr__(self):
return '%s -> %s' % (self.Left, self.Right)
def __iter__(self):
yield self.Left
yield self.Right
def __eq__(self, other):
return isinstance(other, Production) and self.Left == other.Left and self.Right == other.Right
def __hash__(self):
return hash((self.Left, self.Right))
@property
def IsEpsilon(self):
return self.Right.IsEpsilon
class AttributeProduction(Production):
def __init__(self, nonTerminal, sentence, attributes):
if not isinstance(sentence, Sentence) and isinstance(sentence, Symbol):
sentence = Sentence(sentence)
super(AttributeProduction, self).__init__(nonTerminal, sentence)
self.attributes = attributes
def __str__(self):
return '%s := %s' % (self.Left, self.Right)
def __repr__(self):
return '%s -> %s' % (self.Left, self.Right)
def __iter__(self):
yield self.Left
yield self.Right
@property
def IsEpsilon(self):
return self.Right.IsEpsilon
# sintetizar en ingles??????, pending aggrement
def syntetice(self):
pass
class Grammar():
def __init__(self):
self.Productions = []
self.nonTerminals = []
self.terminals = []
self.startSymbol = None
# production type
self.pType = None
self.Epsilon = Epsilon(self)
self.EOF = EOF(self)
self.symbDict = { '$': self.EOF }
def NonTerminal(self, name, startSymbol = False):
name = name.strip()
if not name:
raise Exception("Empty name")
term = NonTerminal(name,self)
if startSymbol:
if self.startSymbol is None:
self.startSymbol = term
else:
raise Exception("Cannot define more than one start symbol.")
self.nonTerminals.append(term)
self.symbDict[name] = term
return term
def NonTerminals(self, names):
ans = tuple((self.NonTerminal(x) for x in names.strip().split()))
return ans
def Add_Production(self, production):
if len(self.Productions) == 0:
self.pType = type(production)
assert type(production) == self.pType, "The Productions most be of only 1 type."
production.Left.productions.append(production)
self.Productions.append(production)
def Terminal(self, name):
name = name.strip()
if not name:
raise Exception("Empty name")
term = Terminal(name, self)
self.terminals.append(term)
self.symbDict[name] = term
return term
def Terminals(self, names):
ans = tuple((self.Terminal(x) for x in names.strip().split()))
return ans
def __str__(self):
mul = '%s, '
ans = 'Non-Terminals:\n\t'
nonterminals = mul * (len(self.nonTerminals)-1) + '%s\n'
ans += nonterminals % tuple(self.nonTerminals)
ans += 'Terminals:\n\t'
terminals = mul * (len(self.terminals)-1) + '%s\n'
ans += terminals % tuple(self.terminals)
ans += 'Productions:\n\t'
ans += str(self.Productions)
return ans
def __getitem__(self, name):
try:
return self.symbDict[name]
except KeyError:
return None
@property
def to_json(self):
productions = []
for p in self.Productions:
head = p.Left.Name
body = []
for s in p.Right:
body.append(s.Name)
productions.append({'Head':head, 'Body':body})
d={'NonTerminals':[symb.Name for symb in self.nonTerminals], 'Terminals': [symb.Name for symb in self.terminals],\
'Productions':productions}
# [{'Head':p.Left.Name, "Body": [s.Name for s in p.Right]} for p in self.Productions]
return json.dumps(d)
@staticmethod
def from_json(data):
data = json.loads(data)
G = Grammar()
dic = {'epsilon':G.Epsilon}
for term in data['Terminals']:
dic[term] = G.Terminal(term)
for noTerm in data['NonTerminals']:
dic[noTerm] = G.NonTerminal(noTerm)
for p in data['Productions']:
head = p['Head']
dic[head] %= Sentence(*[dic[term] for term in p['Body']])
return G
def copy(self):
G = Grammar()
G.Productions = self.Productions.copy()
G.nonTerminals = self.nonTerminals.copy()
G.terminals = self.terminals.copy()
G.pType = self.pType
G.startSymbol = self.startSymbol
G.Epsilon = self.Epsilon
G.EOF = self.EOF
G.symbDict = self.symbDict.copy()
return G
@property
def IsAugmentedGrammar(self):
augmented = 0
for left, right in self.Productions:
if self.startSymbol == left:
augmented += 1
if augmented <= 1:
return True
else:
return False
def AugmentedGrammar(self, force=False):
if not self.IsAugmentedGrammar or force:
G = self.copy()
# S, self.startSymbol, SS = self.startSymbol, None, self.NonTerminal('S\'', True)
S = G.startSymbol
G.startSymbol = None
SS = G.NonTerminal('S\'', True)
if G.pType is AttributeProduction:
SS %= S + G.Epsilon, lambda x : x
else:
SS %= S + G.Epsilon
return G
else:
return self.copy()
#endchange
class Item:
def __init__(self, production, pos, lookaheads=[]):
self.production = production
self.pos = pos
self.lookaheads = frozenset(look for look in lookaheads)
def __str__(self):
s = str(self.production.Left) + " -> "
if len(self.production.Right) > 0:
for i,c in enumerate(self.production.Right):
if i == self.pos:
s += "."
s += str(self.production.Right[i])
if self.pos == len(self.production.Right):
s += "."
else:
s += "."
s += ", " + str(self.lookaheads)[10:-1]
return s
def __repr__(self):
return str(self)
def __eq__(self, other):
return (
(self.pos == other.pos) and
(self.production == other.production) and
(set(self.lookaheads) == set(other.lookaheads))
)
def __hash__(self):
return hash((self.production,self.pos,self.lookaheads))
@property
def IsReduceItem(self):
return len(self.production.Right) == self.pos
@property
def NextSymbol(self):
if self.pos < len(self.production.Right):
return self.production.Right[self.pos]
else:
return None
def NextItem(self):
if self.pos < len(self.production.Right):
return Item(self.production,self.pos+1,self.lookaheads)
else:
return None
def Preview(self, skip=1):
unseen = self.production.Right[self.pos+skip:]
return [ unseen + (lookahead,) for lookahead in self.lookaheads ]
def Center(self):
return Item(self.production, self.pos)