-
Notifications
You must be signed in to change notification settings - Fork 0
/
gramatica.py
156 lines (138 loc) · 3.58 KB
/
gramatica.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
from itertools import *
import copy
import operator
def unq(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if x not in seen and not seen_add(x)]
class Symbol:
nume=None
def __init__(self,nume):
self.nume=nume
def __str__(self):
return self.nume
def __eq__(self,other):
return self.nume==other
def __repr__(self):
return self.__str__()
class Rule:
pleacaDin=None
pleacaIn=[]
def __init__(self,start,end):
self.pleacaDin=start
self.pleacaIn=end
def __str__(self):
s=self.pleacaDin
s=s+"->"
for x in self.pleacaIn:
s=s+x.nume
return s
def __repr__(self):
return self.__str__()
def nrOf(self,symbol):
c=0
for s in self.pleacaIn:
if symbol==s:
c=c+1
return c
f=open("gramatica.txt",'r')
gramatica=[]
for line in f:
st=line.rstrip().split('->')
start=st[0]
for g in st[1].split('|'):
end=[]
for l in g:
end.append(Symbol(l))
gramatica.append(Rule(start,end))
for x in gramatica:
print x
delGram=[]
newGram=[]
done=False
tt=[]
print "Gramatica just started:",gramatica
while not done:
print "------------------------START----------------------------"
done=True
for x in gramatica:
if '*' in x.pleacaIn:
done=False
for z in gramatica:
if x.pleacaDin in z.pleacaIn:
a=z.nrOf(x.pleacaDin)
comb=list(product([0,1],repeat=a))
for co in comb:
i=0
d=0
temp=copy.deepcopy(z.pleacaIn)
print "Temp:",temp,"Comb:",co
for s in z.pleacaIn:
if s==x.pleacaDin:
if co[i]==0:
print "Deleted:",temp[d]
del temp[d]
d=d-1
i=i+1
d=d+1
print "Temp final:",temp
if not temp in tt:
tt.append(temp)
if not temp:
temp=[Symbol('*'),]
newGram.append(Rule(z.pleacaDin,temp))
delGram.append(z)
delGram.append(x)
delGram=unq(delGram)
print x.pleacaDin,">>>>>",z,a,comb
gramatica=gramatica+newGram
gramatica=[i for i in gramatica if i not in delGram]
gramatica=unq(gramatica)
print "-------------------------END--------------------------------"
print "Gramatica partea 1:",gramatica
print "DelGram:",delGram
delGram=[]
for x in gramatica:
if len(x.pleacaIn)==1 and not x.pleacaIn[0]=='*' and x.pleacaIn[0].nume.isupper():
delGram.append(x)
for k in gramatica:
if k.pleacaDin==x.pleacaIn[0]:
gramatica.append(Rule(x.pleacaDin,k.pleacaIn))
gramatica=[i for i in gramatica if i not in delGram]
gramatica=unq(gramatica)
sym=dict()
rl=[]
print "Gramatica partea 2:",gramatica
surplus=1
for x in gramatica:
if len(x.pleacaIn)==3:
newStr="V"+str(surplus)
if x.pleacaIn[0].nume+x.pleacaIn[1].nume in rl:
newRule=Rule(sym[x.pleacaIn[0].nume+x.pleacaIn[1].nume],[x.pleacaIn[0],x.pleacaIn[1]])
x.pleacaIn=[newSymb,x.pleacaIn[2]]
else:
newSymb=Symbol(newStr)
sym.update({x.pleacaIn[0].nume+x.pleacaIn[1].nume:newStr})
rl.append(x.pleacaIn[0].nume+x.pleacaIn[1].nume)
newRule=Rule(newStr,[x.pleacaIn[0],x.pleacaIn[1]])
surplus=surplus+1
x.pleacaIn=[newSymb,x.pleacaIn[2]]
gramatica.append(newRule)
print "Gramatica partea 3:",gramatica
newGram=[]
newRules=[]
for x in gramatica:
if len(x.pleacaIn)>1:
for s in x.pleacaIn:
if s.nume.islower():
if not "U"+s.nume.lower() in newRules:
newRules.append("U"+s.nume.lower())
newGram.append(Rule("U"+s.nume.lower(),[Symbol(s.nume.lower()),]))
print "Changed:",s.nume,"to","U"+s.nume.lower()
s.nume="U"+s.nume.lower()
gramatica=gramatica+newGram
gramatica=unq(gramatica)
gramatica.sort(key=operator.attrgetter("pleacaDin"))
print "Gramatica finala:",gramatica,'\n'
for i in gramatica:
print i