-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsers.py
469 lines (396 loc) · 16.9 KB
/
parsers.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
'''
Phi - Programmation Heuristique Interface
parsers.py - Contains the various parsers for Phi
----------------
Author: Tanay Kar
----------------
'''
from constants import *
from exceptions import *
class ExpressionParser:
''' Parses an expression and returns an ExpressionNode
Takes a bare expression and returns the first expression available.
For example : x+5,y-8 -> <x+5>
Arguments:
----------
tokens : list
Methods:
--------
parse() : Parses the expression and returns an ExpressionNode
'''
def __init__(self, tokens):
self.master_tokens = tokens
self.tokens = tokens
self.current_token: Token = None
self.index = -1
self.startindex, self.endindex = 0, 0
self.advance()
def advance(self):
'''Advance the index and set the current token'''
while self.index < len(self.tokens):
self.current_token = self.tokens[self.index]
self.index += 1
if self.current_token.type != 'EOL':
break
def peek(self):
'''Return the next token'''
return self.tokens[self.index+1] if self.index+1 < len(self.tokens) else None
def return_slice(self):
'''Return the start and end index of the expression'''
return self.startindex, self.endindex
def parse(self):
'''Parse the expression'''
self.startindex = self.index - 1
parsed = self.parse_expression()
self.endindex = self.index - 1
if getattr(parsed, 'type', None) is not None and parsed.type == "FACTOR":
if parsed.value.type in ('ID', 'FUNCTION'):
parsed = parsed.value
return parsed
if parsed.value.type == 'NUMBER':
parsed = parsed.value.value if parsed.sign != '-' else f'-{parsed.value.value}'
return ExpressionNode(parsed, type_hint='NUM')
elif not parsed:
return parsed
return ExpressionNode(parsed)
def parse_expression(self):
'''Parse the expression of addition and subtraction'''
left = self.parse_term()
while self.current_token.type in ('PLUS', 'MINUS'):
op = self.current_token.type
self.advance()
right = self.parse_term()
left = BinOpNode(left, op, right)
return left
def parse_term(self):
'''Parse the term of multiplication and division'''
left = self.parse_radical()
while self.current_token.type in ('MULT', 'DIV', 'DOT'):
op = self.current_token.type if self.current_token.type not in (
'DOT') else 'MULT'
self.advance()
right = self.parse_radical()
left = BinOpNode(left, op, right)
return left
def parse_radical(self):
'''Parse the radical of exponentiation'''
left = self.parse_factor()
while self.current_token.type == 'CARET':
op = self.current_token.type
self.advance()
right = self.parse_factor()
left = BinOpNode(left, op, right)
return left
def parse_factor(self):
'''Parse the factor of a number or an identifier or a function or an expression'''
if self.current_token.type == 'NUMBER':
value = self.current_token
self.advance()
if self.current_token.type == 'ID' or self.current_token.type == 'FUNCTION':
var = self.current_token
self.advance()
return BinOpNode(FactorNode(value), 'MULT', FactorNode(var))
elif self.current_token.type == 'EXPRESSION':
var = self.current_token
self.advance()
return BinOpNode(FactorNode(value), 'MULT', var.expression)
return FactorNode(value)
elif self.current_token.type == 'ID':
value = self.current_token
self.advance()
return FactorNode(value)
elif self.current_token.type == 'FUNCTION':
value = self.current_token
self.advance()
return FactorNode(value)
elif self.current_token.type == 'MINUS': # Unary negation handling
self.advance()
right = self.parse_factor()
if self.current_token.type == 'ID' or self.current_token.type == 'FUNCTION':
var = self.current_token
self.advance()
return BinOpNode(FactorNode(value,sign='-'), 'MULT', FactorNode(var))
elif self.current_token.type == 'EXPRESSION':
var = self.current_token
self.advance()
return BinOpNode(FactorNode(value,sign='-'), 'MULT', var.expression)
return FactorNode(right, sign='-')
elif self.current_token.type == 'EXPRESSION':
value = self.current_token.expression
self.advance()
if self.current_token.type == 'ID' or self.current_token.type == 'FUNCTION':
var = self.current_token
self.advance()
return BinOpNode(value, 'MULT', FactorNode(var))
elif self.current_token.type == 'EXPRESSION':
var = self.current_token
self.advance()
return BinOpNode(value, 'MULT', var.expression)
return value
# Handle opening parenthesis "("
elif self.current_token.type == 'LPAREN':
raise NotImplementedError('Nested Paranthesis not supported yet')
class ExpressionParserWrapper:
''' A wrapper for ExpressionParser that parses a list of expressions
Iteratively parses a list of expressions and returns all the expressions in the token corpus.
Arguments:
----------
tokens : list
Methods:
--------
parse() : Parses the token corpus and returns a combination of ExpressionNode and Token objects
Note
----
- This class is used to parse a list of expressions. It is not used to parse a single expression.
- The token corpus must be succeeded by an 'EOL' token.
'''
def __init__(self, tokens):
self.index = -1
self.tokens = tokens
self.current_token: Token = None
self.advance()
def advance(self):
'''Advance the index and set the current token'''
while self.index < len(self.tokens):
self.current_token = self.tokens[self.index]
self.index += 1
if self.current_token.type != "EOL":
break
def return_tokens(self):
'''Return the token corpus'''
return self.tokens
def parse(self):
'''Parse the token corpus'''
while self.current_token.type != "EOL":
ep = ExpressionParser(self.tokens[self.index-1:])
pr = ep.parse()
sl1, sl0 = [i + self.index - 1 for i in ep.return_slice()]
if pr:
self.tokens[sl1:sl0] = [pr]
self.index = sl1 + 1
self.advance()
return self.return_tokens()
class TupleParser:
''' Parses all the tuples and returns a TupleToken in their place
Also creates functions nodes and returns them in their place
Arguments:
----------
tokens : list
Methods:
--------
parse() : Parses the token corpus and returns a combination of ExpressionNode and Token objects
Note:
-----
This class will be updated in its mechanism in the future
'''
def __init__(self, tokens):
self.tokens = tokens
self.current_token = tokens[0]
self.tuple = TupleToken
self.index = -1
self.item_corpus = []
self.advance()
def advance(self):
'''Advance the index and set the current token'''
self.index += 1
if self.index < len(self.tokens):
self.current_token = self.tokens[self.index]
else:
self.current_token = Token('EOL')
def couplet(self, items, stindex):
'''Return a couplet of next two items'''
return items[stindex], items[stindex+1]
def assert_type(self, token, type, enforceable=False, alt_type=None):
'''Assert/Check the type of a token'''
if token.type not in (type, alt_type):
if enforceable:
raise Exception(self.tokens + '\nExpected token type ' +
type + ' but got ' + token.type)
else:
return False
else:
return True
def parse_item(self, items_arg):
'''Parse the actual set of tokens and amalgamation of commas'''
tupletok = TupleToken()
index = 0
items = ExpressionParserWrapper(
items_arg+[Token('EOL', 'EOL')]).parse()
items.pop(-1) # Remove the EOL token
# check if the list is empty
if len(items) == 0:
return tupletok
# check if the list has only one item
elif len(items) == 1:
if self.assert_type(items[0], 'ID') or self.assert_type(items[0], 'EXPRESSION'):
tupletok.add(items)
return tupletok
# check if the list has the format of a tuple
id, sep = self.couplet(items, 0)
if (self.assert_type(id, 'ID') or self.assert_type(id, 'EXPRESSION')) and self.assert_type(sep, 'COMMA'):
tupletok.add(id)
index += 1
while index < len(items):
sep, id = self.couplet(items, index)
self.assert_type(sep, 'COMMA', enforceable=True)
self.assert_type(id, 'ID', enforceable=True,
alt_type='EXPRESSION')
tupletok.add(id)
index += 2
return tupletok
else:
pass # I have no idea what this block does either 🤷♂️
def replace(self, st, end, new_item):
'''Replace a set of tokens with a new token'''
self.tokens[st:end] = [new_item]
self.index = st
def parse(self):
'''Parse the token corpus'''
t_items = []
while self.index < len(self.tokens):
if self.current_token.type == 'LPAREN':
st = self.index
while self.current_token.type != 'RPAREN' and self.current_token.type != 'EOL':
t_items.append(self.current_token)
self.advance()
if self.tokens[self.index].type == 'RPAREN':
t_items.pop(0)
end = self.index + 1
self.replace(st, end, self.parse_item(t_items))
t_items = []
elif self.tokens[self.index].type == 'EOL':
raise ParseError('Missing closing parenthesis')
self.advance()
self.parse_declarations()
self.reverse_expr_parse()
return self.tokens
def parse_declarations(self):
'''Parse functions and replace them with a DeclarationNode'''
self.index = -1
while self.index < len(self.tokens):
if self.current_token and self.current_token.type == 'ID' and self.index + 1 < len(self.tokens) and self.tokens[self.index+1].type == 'TUPLE':
self.replace(self.index, self.index+2,
DeclarationNode(self.current_token, self.tokens[self.index+1]))
self.advance()
def reverse_expr_parse(self):
'''Reverse the expression parsing if the tuple is not a function'''
self.index = -1
while self.index < len(self.tokens):
if self.current_token.type == 'TUPLE' and len(self.current_token.variables) == 1:
self.tokens[self.index] = self.current_token.values[0][0]
self.advance()
class MasterParser:
''' Grammer Parser . Ensures overall syntax is correct with the help of a grammar file
Takes a list of tokens and a grammar file and returns a LineNode matching the tokens.
Iteratively goes through the corpus , and checks for a specific rule that the corpus matches.
Arguments:
----------
tokens : list
grammar : dict
Methods:
--------
parse() : Parses the token corpus and returns a LineNode object
Note:
-----
The grammar file can be modified to add new rules and syntaxes.
'''
def __init__(self, tokens, grammar: dict):
self.tokens = tokens
self.prepare()
self.grammar_raw = grammar
self.master_lexeme = None
self.applicable_rules = []
self.absolute_rule = None
self.index = -1
self.current_token: Token = None
self.enforce_grammar = False
self.advance()
def prepare(self):
'''Prepare the token corpus for parsing'''
tok = TupleParser(self.tokens).parse()
tok = ExpressionParserWrapper(tok).parse()
self.tokens = tok
def reset_index(self):
'''Reset the index and current token'''
self.index = -1
self.current_token: Token = None
self.advance()
def advance(self):
'''Advance the index and set the current token'''
while self.index < len(self.tokens):
self.current_token = self.tokens[self.index]
self.index += 1
if self.current_token.type != "EOL":
break
def get_master_rule(self):
'''Get the master rule for the token corpus on the basis of the first token type'''
first_token_type = self.current_token.type
self.primary_keyword = first_token_type
if first_token_type in self.grammar_raw:
self.master_lexeme = first_token_type
self.applicable_rules = [self.grammar_raw[first_token_type]['syntax'][i]['syntax'].split()
for i in self.grammar_raw[first_token_type]['syntax']
]
self.rule_ids = [self.grammar_raw[first_token_type]['syntax'][i]['spec_id']
for i in self.grammar_raw[first_token_type]['syntax']
]
def get_item(self, index, list):
'''Get the item at a specific index in a list'''
if index < len(list):
return list[index]
else:
return None
def grammer_match(self):
'''Match the token corpus with the applicable rules'''
for i in range(len(self.tokens)):
self.enforce_grammar = True if len(
self.applicable_rules) == 1 else False
rules, rules_id = [], []
if self.enforce_grammar:
if self.get_item(i, self.applicable_rules[0]) != None:
if self.get_item(i, self.applicable_rules[0]) == self.tokens[i].type or self.get_item(i, self.applicable_rules[0]) == getattr(self.tokens[i], 'type_hint', None) or self.get_item(i, self.applicable_rules[0]) == getattr(self.tokens[i], 'base_type', None):
pass
else:
if self.get_item(i, self.applicable_rules[0]) == 'EOL':
raise ParseError(
f'{self.tokens} \nUnexpected {self.tokens[i].type} \'{self.tokens[i].value}\'')
raise ParseError(
f'{self.tokens} \nExpected {self.get_item(i,self.applicable_rules[0])} but recieved {self.tokens[i].type} \'{self.tokens[i].value}\'')
else:
raise ParseError(
f'{self.tokens} \nUnexpected {self.tokens[i].type} \'{self.tokens[i].value}\'')
else:
for j in range(len(self.applicable_rules)):
rule = self.applicable_rules[j]
id = self.rule_ids[j]
if self.get_item(i, rule) == self.tokens[i].type or self.get_item(i, rule) == getattr(self.tokens[i], 'type_hint', None) or self.get_item(i, rule) == getattr(self.tokens[i], 'base_type', None):
rules.append(rule)
rules_id.append(id)
self.applicable_rules = rules
self.rule_ids = rules_id
if len(self.applicable_rules) != 1:
raise ParseError(f'Syntax Error')
elif len(self.applicable_rules) == 1:
self.absolute_rule = self.applicable_rules[0]
self.absolute_rule_id = self.rule_ids[0]
def parse(self):
'''Wrapper function for all the parsing methods'''
self.get_master_rule()
self.grammer_match()
line = LineNode(self.tokens, self.absolute_rule_id,
self.primary_keyword, self.grammar_raw)
return line
if __name__ == '__main__':
from lexer import Lexer
import json
lexer = Lexer('solve (eq1,eq2) for (x,y)')
tokens = lexer.get_tokens()
print(tokens)
with open('grammar.json', 'r') as f:
grammar = json.load(f)
parser = MasterParser(tokens, grammar)
print(parser.parse())
'''lexer = Lexer('x+y,x')
tokens = lexer.get_tokens()
epw = ExpressionParserWrapper(tokens)
print(epw.parse())'''