-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCFGparse.py
More file actions
295 lines (228 loc) · 9.3 KB
/
CFGparse.py
File metadata and controls
295 lines (228 loc) · 9.3 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
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
"""
LL(1) CFG parser
Given an LL(1) grammar and a string,
this program will determine whether the string belongs
to the language defined by the grammar.
If -e is given as an argument, any rejected input will
be modified to produce a similar accepted string.
Arguments:
inputfile [grammarfile] [-e]
"""
import sys
from collections import deque
EPSILON = ""
END = '$'
RESERVED = [END]
# 0. handle arguments
parse_file = 'tests/accept.txt'
grammar_file = 'grammars/corrected.grm'
recovery = False
if len(sys.argv) >= 2:
parse_file = sys.argv[1]
if len(sys.argv) == 4:
grammar_file = sys.argv[2]
if sys.argv[3] == "-e":
recovery = True
elif len(sys.argv) == 3:
if sys.argv[2] == "-e":
recovery = True
else:
grammar_file = sys.argv[2]
# 1. extract grammar
LINES = []
with open(grammar_file, 'r') as g:
for line in g:
LINES.append("".join(line.split()).replace("epsilon", EPSILON))
# whitespace is stripped, epsilon handled
# rules, variables
RULES = {}
START = LINES[0][0]
for line in LINES:
rule = line.split("->")
assert len(rule[0]) == 1
RULES[rule[0]] = set(rule[1].split("|"))
VARIABLES = RULES.keys()
# terminals
TERMINALS = [END]
for line in LINES:
for letter in line.replace("->", ""):
if not (letter in VARIABLES or letter in TERMINALS or letter == "|"):
if letter in RESERVED:
print "Reserved symbol in grammar:", letter
sys.exit(1)
TERMINALS.append(letter)
# 2. build parse table
""" Returns the FIRST set of a given string. """
def first(string):
symbol = string[:1]
if symbol in VARIABLES:
init_set = set()
for rule in RULES[symbol]:
if symbol != rule[:1]:
init_set |= first(rule)
if EPSILON in init_set:
return (init_set - {EPSILON}) | first(string[1:])
else:
return init_set
elif string == EPSILON:
return {EPSILON}
else:
return {symbol}
""" Returns the FOLLOW set of a given variable. """
def follow(variable, used):
follow_set = {END} if variable == START else set()
for var in VARIABLES:
for prod in RULES[var]:
index = prod.find(variable)
followed = False
while index != -1:
init_set = first(prod[index + 1:])
if not followed and var != variable and EPSILON in init_set and var not in used:
init_set |= follow(var, used | {var})
followed = True
follow_set |= (init_set - {EPSILON})
index = prod.find(variable, index + 1)
return follow_set
# Construct the parse table itself, which is a function (Variable, Terminal) -> String
P_TABLE = {var: {} for var in VARIABLES}
for var in RULES:
for prod in RULES[var]:
first_set = first(prod)
follow_set = set()
if EPSILON in first_set:
follow_set = follow(var, set())
if not first_set.isdisjoint(follow_set):
print "First, Follow sets on", var, "not disjoint; grammar not LL(1)"
sys.exit(1)
for term in (first_set | follow_set) - {EPSILON}:
if term not in P_TABLE[var]:
P_TABLE[var][term] = prod
else:
print "Production ambiguity on ["+var+","+term+"]; grammar not LL(1)"
sys.exit(1)
# 3. Parse String
""" Given a string and a parsing stack, returns a 'similar' string satisfying the stack"""
def find_close_valid(string, stack):
tried = []
# each queue entry is as follows:
# [invalid substring, stack at point of invalidation, initial valid substring]
queue = deque([[string, stack, []]])
while True:
current = queue.popleft()
# result[0] = invalid substring; result[1] = stack at point of invalidation
result = parse_string(*(current[:2]), verbose=False)
if result != -1:
result = list(result)
if result == -1:
return current[0] + current[2]
else:
tried.append(current[:2])
string_head = result[0][-1]
stack_head = result[1][-1]
# insertion reversal
# delete one character from result[0], leave result[1] the same
if string_head != END:
candidate = [ list(result[0][:-1]),
list(result[1]),
list(current[0][len(result[0][:-1]) + 1:] + current[2]) ]
if candidate not in tried:
queue.append(candidate)
# deletion reversal
# If symbol not in the grammar, must have been inserted, so don't reverse deletion
if string_head in TERMINALS and stack_head != END:
# retain result[1], prepend head of result[1] to result[0]
if stack_head in TERMINALS:
#insert what was expected -- naive, but catches point removals
candidate = [ list(result[0]) + [result[1][-1]],
list(result[1]),
current[0][len(result[0]):] + current[2] ]
if candidate not in tried:
queue.append(candidate)
# retain result[1], prepend valid terminals to result[0]
else:
for term in P_TABLE[stack_head].keys():
if term == END:
continue
candidate = [ list(result[0]) + [term],
list(result[1]),
current[0][len(result[0]):] + current[2] ]
if candidate not in tried:
queue.append(candidate)
"""
Attempts to parse a string.
If no initial string is given, the file given in the first argument is consulted.
If no initial stack is specified, the start symbol is used as the initialiser.
If correct_errors is true, invalid substrings will be corrected.
If verbose is false, no trace will be printed.
If the string was parsed successfully, returns -1,
otherwise, returns the last state of its remaining string and stack."""
def parse_string(init_string = None, init_stack = None, correct_errors=False, verbose=True):
# Obtain input string
if init_string is None:
with open(parse_file, 'r') as g:
string = "".join(g.read().split()) + END
remaining = list(string[::-1])
else:
remaining = list(init_string)
parse_stack = [END, START] if init_stack is None else list(init_stack)
# Parse the string
error = False
corrected = False
while remaining and parse_stack:
if verbose:
print ''.join(reversed(remaining)), ''.join(reversed(parse_stack))
error_string = "\n"
string_head = remaining[-1]
stack_head = parse_stack[-1]
# Reached a symbol not in the grammar
if string_head not in TERMINALS:
if verbose:
error_string += "ERROR:\033[1;31m "+string_head+" \033[0mis not a terminal of the given language.\n"
error = True
if stack_head in VARIABLES:
try:
substitute = P_TABLE[stack_head][string_head]
except KeyError:
if verbose:
error_string += "ERROR:\033[1;31m "+string_head+" \033[0mobtained; but variable\033[1;36m "+stack_head+" \033[0mexpects one of\033[1;32m "+str(P_TABLE[stack_head].keys())+" \033[0m\n"
error = True
# Update the stack if a production rule was found
if not error:
parse_stack.pop()
for sym in substitute[::-1]:
parse_stack.append(sym)
# If the stack symbol is not a variable, it must be a terminal
else:
if stack_head != string_head:
if verbose:
error_string += "ERROR: Terminal mismatch.\n"
error = True
else:
remaining.pop()
parse_stack.pop()
if error:
if verbose:
print error_string
# Correct the error and carry on as if nothing happened.
if correct_errors:
prev = remaining
remaining = find_close_valid(remaining, parse_stack)
string = string[:-len(prev)] + ''.join(reversed(remaining[1:]))
if verbose:
print "Correcting remaining string:", ''.join(reversed(prev)), "->", ''.join(reversed(remaining)), "\n"
corrected = True
error = False
continue
else:
break
if remaining or parse_stack:
if verbose:
print "REJECTED"
return (remaining, parse_stack)
else:
if verbose:
print "ACCEPTED"
if corrected:
print "\nCorrected surrogate string:\n" + string
return -1
parse_string(correct_errors = recovery)