-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebnf_semantic.py
332 lines (247 loc) · 7.24 KB
/
ebnf_semantic.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
import ebnf_syntax
class AST_Info(object):
def __init__(self, top_id, rules, offsets, text):
self._top_id = top_id
self._rules = rules
self._offsets = offsets
self._text = text
def __repr__(self):
return "%s(%s, %s, %s, %s)"%(type(self).__name__, repr(self.top_id), repr(self.rules), repr(self.offsets), repr(self.text))
@property
def top_id(self):
return self._top_id
@property
def rules(self):
return self._rules
@property
def offsets(self):
return self._offsets
@property
def text(self):
return self._text
class EBNF_Pattern(object):
def __hash__(self):
return hash(repr(self))
def __eq__(self, other):
return repr(self) == repr(other)
def __ne__(self, other):
return repr(self) != repr(other)
class Terminal(EBNF_Pattern):
def __init__(self, terminal, offset=0):
self._terminal = terminal
self._offset = offset
def __repr__(self):
return "%s(%s)"%(type(self).__name__, repr(self.terminal))
@property
def terminal(self):
return self._terminal
@property
def offset(self):
return self._offset
class Identifier(EBNF_Pattern):
def __init__(self, identifier, offset=0):
self._identifier = identifier
self._offset = offset
def __repr__(self):
return "%s(%s)"%(type(self).__name__, repr(self.identifier))
@property
def identifier(self):
return self._identifier
@property
def offset(self):
return self._offset
class Optional(EBNF_Pattern):
def __init__(self, rhs, offset=None):
self._rhs = rhs
if not offset:
self._offset = rhs.offset
else:
self._offset = offset
def __repr__(self):
return "%s(%s)"%(type(self).__name__, repr(self.rhs))
@property
def rhs(self):
return self._rhs
@property
def offset(self):
return self._offset
class Repetition(EBNF_Pattern):
def __init__(self, rhs, offset=None):
self._rhs = rhs
if not offset:
self._offset = rhs.offset
else:
self._offset = offset
def __repr__(self):
return "%s(%s)"%(type(self).__name__, repr(self.rhs))
@property
def rhs(self):
return self._rhs
@property
def offset(self):
return self._offset
class Concatenation(EBNF_Pattern):
def __init__(self, terms, offset=None):
_terms = []
for term in terms:
if type(term) == Concatenation:
_terms.extend(term.terms)
else:
_terms.append(term)
self._terms = tuple(_terms)
if not offset:
if _terms[0] == None:
self._offset = 0 # Should not happen for real things anyway
else:
self._offset = _terms[0].offset
else:
self._offset = offset
def __repr__(self):
return "%s(%s)"%(type(self).__name__, repr(self.terms))
@property
def terms(self):
return self._terms
@property
def offset(self):
return self._offset
class Alternation(EBNF_Pattern):
def __init__(self, clauses, offset=None):
_clauses = []
for clause in clauses:
if type(clause) == Alternation:
_clauses.extend(clause.clauses)
else:
_clauses.append(clause)
self._clauses = tuple(_clauses)
if not offset:
if _clauses[0] == None:
self._offset = 0 # Should not happen for real things anyway
else:
self._offset = _clauses[0].offset
else:
self._offset = offset
def __repr__(self):
return "%s(%s)"%(type(self).__name__, repr(self.clauses))
@property
def clauses(self):
return self._clauses
@property
def offset(self):
return self._offset
def get_line_col(text, offset):
len_so_far = 0
prev_len_so_far = 0
for i, line in enumerate(text.split("\n")):
prev_len_so_far = len_so_far
len_so_far += len(line) + 1
if len_so_far > offset:
break
linenum = i + 1
colnum = offset - prev_len_so_far + 1
return linenum, colnum
def print_error(text, grammar):
thing = grammar.error
while type(thing) == ebnf_syntax.InvalidRule:
thing = thing.error
linenum, colnum = get_line_col(text, thing.offset)
print "EBNF error at line %d, column %d"%(linenum, colnum)
print thing
def get_identifier(lhs):
if lhs.contents[0].identifier == "identifier":
return lhs.contents[0].text, lhs.contents[0].offset
return lhs.contents[1].text, lhs.contents[1].offset
def unescape_character(escaped_char):
unescapes = {"n": "\n", "t": "\t", "\\": "\\", "'": "'", '"': '"'}
code = escaped_char.contents[1].text
return unescapes[code]
def get_character(character12):
if type(character12.contents) == ebnf_syntax.Rule:
character = character12.contents
if character.contents.identifier == "escape_sequence":
return unescape_character(character.contents)
return character.text
return character12.text
def get_terminal(terminal):
result = ""
for element in terminal.contents[1:-1]:
character = get_character(element)
result += character
return result
def get_term(spaced_term):
if spaced_term.contents[0].identifier == "term":
term = spaced_term.contents[0]
else:
term = spaced_term.contents[1]
if type(term.contents) == ebnf_syntax.Rule and term.contents.identifier == "identifier":
return Identifier(term.contents.text, term.offset)
elif type(term.contents) == ebnf_syntax.Rule and term.contents.identifier == "terminal":
terminal = get_terminal(term.contents)
return Terminal(terminal, term.offset)
elif term.contents[0].text == "[":
rhs = get_rhs(term.contents[1])
return Optional(rhs, term.offset)
elif term.contents[0].text == "{":
rhs = get_rhs(term.contents[1])
return Repetition(rhs, term.offset)
else:
# Group "(...)" is just a passthrough
rhs = get_rhs(term.contents[1])
return rhs
def get_clause(clause):
terms = []
for element in clause.contents:
if type(element) == ebnf_syntax.Rule and element.identifier == "spaced_term":
term = get_term(element)
terms.append(term)
if len(terms) == 1:
return terms[0]
return Concatenation(terms, terms[0].offset)
def get_rhs(rhs):
clauses = []
offset = None
for element in rhs.contents:
if type(element) == ebnf_syntax.Rule and element.identifier == "clause":
clause = get_clause(element)
clauses.append(clause)
if offset == None or clause.offset < offset:
offset = clause.offset
if len(clauses) == 1:
return clauses[0]
return Alternation(clauses, offset)
def get_rule(rule):
lhs = rule.contents[0]
rhs = rule.contents[2]
identifier, offset = get_identifier(lhs)
rhs = get_rhs(rhs)
return identifier, offset, rhs
def get_rules(grammar, top_id):
rules = {}
offsets = {}
for element in grammar.contents:
if type(element) == ebnf_syntax.Rule and element.identifier == "rule":
identifier, offset, rhs = get_rule(element)
if identifier in rules:
linenumA, colnumA = get_line_col(grammar.text, offsets[identifier])
linenumB, colnumB = get_line_col(grammar.text, offset)
if offsets[identifier] < offset:
linenum1 = linenumA
linenum2 = linenumB
colnum1 = colnumA
colnum2 = colnumB
else:
linenum1 = linenumB
linenum2 = linenumA
colnum1 = colnumB
colnum2 = colnumA
print "Duplicate definition of rule %s at line %d, column %d (previous definition at line %d, column %d)."%(repr(identifier), linenum2, colnum2, linenum1, colnum1)
quit()
rules[identifier] = rhs
offsets[identifier] = offset
return AST_Info(top_id, rules, offsets, grammar.text)
def create_ast(description, top_id):
grammar = ebnf_syntax.parse(description)
if type(grammar) == ebnf_syntax.InvalidRule:
print_error(description, grammar)
quit()
ast_info = get_rules(grammar, top_id)
return ast_info