-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparsing_table.py
282 lines (213 loc) · 7.29 KB
/
parsing_table.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
from tabulate import tabulate
import re
from collections import OrderedDict
import sys
import os
grammar_pattern = re.compile(r"(.+?)[=-]>\|?(.+)")
END = '$'
EMPTY = ''
EMPTY_IN = '€'
EMPTY_OUT = 'ε'
OR = '|'
AND = ' '
def grammar(g):
G = OrderedDict()
def add(k, v):
m = G.get(k, [])
m += v
G[k] = m
for i, match in enumerate(grammar_pattern.finditer(g)):
t, s2 = match.group(1), match.group(2)
t = t.strip()
s2 = [s.strip().split(AND) for s in s2.split(OR)]
add(t, s2)
K = list(G.keys())
for i in range(len(K)):
G[i] = G[K[i]]
del G[K[i]]
mp = lambda s: K.index(s) if s in K else s if s != EMPTY_IN else EMPTY
for k in G:
G[k] = [[mp(s) for s in n] for n in G[k]]
return G, K
def first_set(G):
first = dict()
def add(k, v):
m = first.get(k, set())
m.add(v)
first[k] = m
def f(k):
r = []
for X in G[k]:
for x in X:
if isinstance(x, str):
add(x, x)
r.insert(0, x)
if x == EMPTY:
add(k, x)
else:
break
else:
Z = f(x)
for x__ in {Z[i] for i in range(Z.index(EMPTY) if EMPTY in Z else len(Z))}:
r.insert(0, x__)
add(k, x__)
for n in r:
add(k, n)
return r
for k in G:
f(k)
return first
def follow_set(G, S, first = None):
first = first or first_set(G)
follow = dict()
def add(k, v):
m = follow.get(k, set())
m.add(v)
follow[k] = m
add(S, '$')
def f(A):
for X in G[A]:
for i, x in enumerate(X):
if not isinstance(x, str):
fst = []
for y in X[i+1:]:
m = first.get(y, {y})
fst += m
if EMPTY not in m:
break
for y in fst:
if y != EMPTY:
add(x, y)
if len(fst) == 0 or EMPTY in fst:
for a in follow.get(A, {}):
add(x, a)
h = lambda f: hash(n for v in f.values() for n in v)
f_old = h(follow)
while True:
for k in G:
f(k)
if f_old == h(follow):
break
else:
f_old = h(follow)
return follow
def parse_table(G, first = None, follow = None):
first = first or first_set(G)
follow = follow or follow_set(G, 0, first)
M = dict()
def add(N, k, F, t):
m = M.get(N, dict())
hm = m.get(k, set())
hm.add((F, t))
m[k] = hm
M[N] = m
def frst(a):
frs = set()
for x in a:
f = first[x]
frs = frs.union(f)
if EMPTY not in f:
break
return frs - {EMPTY}
def P(A):
for i, X in enumerate(G[A]):
for a in frst(X):
add(A, a, A, i)
for x in X:
if isinstance(x, str):
if x == EMPTY:
for b in follow[A]:
add(A, b, A, i)
else:
break
for g in G:
P(g)
return M
def as_table(G, N, M = None, first = None, follow = None, **kwargs):
first = first or first_set(G)
follow = follow or follow_set(G, 0, first)
terminals = set(n for v in first.values() for n in v).union(set(n for v in follow.values() for n in v))
terminals -= {EMPTY}
nonterminals = set(v for v in follow.keys())
M = M or parse_table(G, first, follow)
s = lambda s: s if s != EMPTY else EMPTY_OUT
z = sorted(list(terminals))
z.reverse()
if len(z) > 4:
z[4], z[3] = z[3], z[4]
data = []
for n in nonterminals:
data2 = [N[n]]
for t in z:
l3 = ""
if M.get(n) and M[n].get(t):
for i, p in enumerate(M[n][t]):
A, x = p
x = "".join([s(e) if isinstance(e, str) else N[e] for e in G[A][x]])
if i > 0:
l3 += "\n"
l3 += ("%-2s → %-2s" % (N[A], x))
data2.append(l3)
data2.append(" ".join(s(c) for c in first[n]))
data2.append(" ".join(s(c) for c in follow[n]))
data.append(data2)
return tabulate(data, headers=["NON -\nTERMINALS"] + z+["FIRST", "FOLLOW"], **kwargs)
if __name__ == "__main__":
l = len(sys.argv)
fmt = 'fancy_grid'
fmts = ["plain" , "simple" , "github" , "grid" , "fancy_grid" , "pipe" , "orgtbl" ,
"jira" , "presto" , "psql" , "rst" , "mediawiki" , "moinmoin" , "youtrack" ,
"html" , "latex" , "latex_raw" , "latex_booktabs" , "textile"]
g = """
E -> T E'
E' -> + T E' | €
T -> F T'
T' -> * F T' | €
F -> ( E ) | id
"""
if l >= 2:
arg = sys.argv[1]
if arg in ("--help", "-h"):
print("Give me grammar!")
print("Like this: \n%s" % g)
print("""€ is empty terminal
Use space (' ') as seperator in production
Seperate productions with pipe ('|') or with different arrow-productions
Any string on the left side of -> is a nonterminal
Any non-nonterminal on the rightside of -> is a terminal
""")
print("Third argument is style of table. Default: fancy_grid \nValid options are:")
for i, f in enumerate(fmts):
if i % 12 == 11:
print()
print(f, end=" ")
print()
exit()
elif os.path.isfile(arg):
with open(arg, "r") as file:
G, N = grammar(file.read())
if len(G) == 0:
print("Invalid grammar in file.")
exit(-1)
elif re.match(grammar_pattern, arg):
G, N = grammar(arg)
else:
print("No grammar was found.")
exit(-1)
if l >= 3:
if sys.argv[2] in fmts:
fmt = sys.argv[2]
else:
print("Invalid table style: %s\nTry without style option or see --help for valid options" % arg)
exit(-1)
else:
print("""
No recognized grammar!
For more info see --help
Example parse table:""")
G, N = grammar(g)
try:
table = as_table(G, N, tablefmt=fmt)
print(table)
except RecursionError:
print("Left recursive grammar!")