-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProduction.py
More file actions
150 lines (115 loc) · 3.1 KB
/
Copy pathProduction.py
File metadata and controls
150 lines (115 loc) · 3.1 KB
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
# -*- coding: utf-8 -*-
class Production:
"""Class that represents a production of a grammar"""
Left=[]
Right=[]
def __init__(self,left,right):
self.Left=left
self.Right=right
def getLeftPart(self):
"""Returns the left part of the production"""
return self.Left
def getRightPart(self):
"""Returns the right part of the production"""
return self.Right
def Equals(self,other):
"""Check if this production is equivalent to other production"""
return self.Left==other.Left and self.Right==other.Right
def __str__(self):
string=''
for var in self.Left:
string+='"'+var+'" '
string+='-->'
for var in self.Right:
if type(var)==list:
for unit in var:
string+='"'+str(unit)+'" '
string+="|"
else:
string+='"'+var+'" '
return string
def copy(self):
"""Copy method for the Production object"""
if type(self.Left)!=list:
if type(self.Right)!=list:
return Production(self.Left,self.Right)
else:
return Production(self.Left,self.Right[:])
else:
if type(self.Right)!=list:
return Production(self.Left[:],self.Right[:])
else:
return Production(self.Left[:],self.Right)
def factorize(self):
"""Factorizes the production"""
factors=[]
common=[]
A=self.Left[0]
for P in self.Right:
if A in P:
factors.append(P.replace(A,''))
common.append(P)
for c in common:
if c in self.Right:
self.Right.remove(c)
print(factors)
if len(factors)>=2:
self.Right.append(''+' | '.join(factors)+' '+A)
if len(factors)==1:
self.Right.append(factors[0]+A)
def dotInit(self):
"""Returns a production with a dot at the beginning"""
c=Production(self.Left[:],self.Right[:])
c.Right.insert(0,"·")
# print(c.Right)
# print(c.Left)
return c
def dotAdvance(self):
"""Returns a production with the dot in the next position"""
c=Production(self.Left[:],self.Right[:])
if "·" in c.Right:
index=c.Right.index("·")
if index+1<len(c.Right):
c.Right.insert(index,c.Right[index+1])
c.Right.pop(index+2)
# print(c.Right)
return c
else:
print("dot at the end")
return False
def dotNextChar(self):
"""Returns the symbol ath the right of the dot"""
if "·" in self.Right:
index=self.Right.index("·")
if index+1<len(self.Right):
return self.Right[index+1]
else:
return ""
else:
return False
def getDotAlpha(self):
"""Returns the symbol at the left of the dot"""
if "·" in self.Right:
index=self.Right.index("·")
if index>0:
return self.Right[index-1]
else:
return""
else:
return False
def getDotBetha(self):
"""Returns the symbol at the second position at the right of the dot"""
if "·" in self.Right:
index=self.Right.index("·")
if index+2<len(self.Right):
return self.Right[index+2]
else:
return ""
else:
return False
def getDotIndex(self):
"""Returns the index of the dot in the production"""
if "·" in self.Right:
return self.Right.index("·")
else:
return -1