-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyParser.py
More file actions
189 lines (163 loc) · 6.54 KB
/
MyParser.py
File metadata and controls
189 lines (163 loc) · 6.54 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
from AST import *
def parse(tokens: [str, str]) -> Node:
if len(tokens) == 1:
if isinstance(tokens[0], Node):
return tokens[0]
return parse_leaf(tokens)
tokens = parse_parentheses(tokens)
tokens = parse_binary_op(tokens, ['*', '/', 'MOD'], reverse=False, sub=False)
tokens = parse_binary_op(tokens, ['+', '-'], reverse=False, sub=False)
tokens = parse_binary_op(tokens, ['=', '<', '>', '<=', '>='], reverse=False, sub=False)
tokens = parse_unary_op(tokens, ['NOT'], sub=False)
tokens = parse_binary_op(tokens, ['AND'], reverse=False, sub=False)
tokens = parse_binary_op(tokens, ['OR'], reverse=False, sub=False)
tokens = parse_binary_op(tokens, ['IMPLIES'], reverse=True, sub=False)
tokens = parse_binary_op(tokens, ['EQUIV'], reverse=False, sub=False)
tokens = parse_unary_op(tokens, ['EXISTS', 'FORALL'], sub=True)
tokens = parse_unary_op(tokens, ['PREVIOUS', 'ONCE'], sub=True)
tokens = parse_unary_op(tokens, ['ALWAYS'], sub=False)
tokens = parse_binary_op(tokens, ['SINCE'], reverse=True, sub=True)
tokens = parse_aggregation(tokens)
if len(tokens) == 1:
if isinstance(tokens[0], Node):
return tokens[0]
else:
raise RuntimeError("Parse failure:", tokens)
def parse_parentheses(tokens: [str, str]) -> []:
new_tokens = []
depth = 0
in_parenthesis = []
for token in tokens:
if depth == 0:
if not isinstance(token, Node):
if token[0] == '(':
depth += 1
elif token[0] == ')':
raise RuntimeError("Unexpected token: \")\"")
else:
new_tokens.append(token)
else:
new_tokens.append(token)
else:
if isinstance(token, Node):
in_parenthesis.append(token)
else:
if token[0] == '(':
depth += 1
in_parenthesis.append(token)
elif token[0] == ')':
depth -= 1
if depth == 0:
new_tokens.append(parse(in_parenthesis))
in_parenthesis = []
else:
in_parenthesis.append(token)
else:
in_parenthesis.append(token)
if depth != 0:
raise RuntimeError("Expected token missing: \")\"")
return new_tokens
def parse_unary_op(tokens, op, sub):
new_tokens = []
tokens = list(reversed(tokens))
for i, token in enumerate(tokens):
if not isinstance(token, Node):
if token[0] in op:
next_token = new_tokens.pop()
if sub and not isinstance(next_token, Node):
if next_token[1] in ['INTERVAL', 'VARLIST']:
next_next_token = new_tokens.pop()
node = UnaryNode(token[0], parse([next_next_token]), parse_sub(next_token))
else:
node = UnaryNode(token[0], parse([next_token]))
else:
node = UnaryNode(token[0], parse([next_token]))
new_tokens.append(node)
else:
new_tokens.append(token)
else:
new_tokens.append(token)
return list(reversed(new_tokens))
def parse_binary_op(tokens, op, reverse, sub):
new_tokens = []
skip_node = 0
if reverse:
tokens = list(reversed(tokens))
for i, token in enumerate(tokens):
if skip_node:
skip_node -= 1
continue
if not isinstance(token, Node):
if token[0] in op:
if reverse:
next_token = new_tokens.pop()
prev_token = tokens[i + 1]
else:
next_token = tokens[i + 1]
prev_token = new_tokens.pop()
if sub and not isinstance(next_token, Node):
if next_token[1] in ['INTERVAL']:
if reverse:
next_next_token = new_tokens.pop()
else:
next_next_token = tokens[i + 2]
skip_node += 1
node = BinaryNode(token[0], parse([prev_token]), parse([next_next_token]), parse_sub(next_token))
else:
node = BinaryNode(token[0], parse([prev_token]), parse([next_token]))
skip_node += 1
else:
node = BinaryNode(token[0], parse([prev_token]), parse([next_token]))
skip_node += 1
new_tokens.append(node)
else:
new_tokens.append(token)
else:
new_tokens.append(token)
if reverse:
return list(reversed(new_tokens))
else:
return new_tokens
def parse_leaf(tokens):
return Leaf(tokens[0][1], tokens[0][0])
def parse_sub(token):
if token[1] == 'VARLIST':
return token[0][1:].split(",")
elif token[1] == 'INTERVAL':
return [int(e) for e in token[0][1:-1].split(",")]
else:
raise RuntimeError("Could not parse sub: " + token[0])
def parse_aggregation(tokens):
new_tokens = []
skip_node = 0
for i, token in enumerate(tokens):
if skip_node:
skip_node -= 1
continue
if not isinstance(token, Node):
if token[0] == '<-':
out = new_tokens.pop()
op = tokens[i + 1]
aggreg = tokens[i + 2]
secondary = [out[0], aggreg[0]]
if len(tokens) > i + 3:
if not isinstance(tokens[i + 3], Node):
if tokens[i + 3][0] == ';':
secondary.append(parse_sub(tokens[i + 4]))
skip_node = 5
node = UnaryNode(op[0], parse([tokens[i + 5]]), secondary)
else:
skip_node = 3
node = UnaryNode(op[0], parse([tokens[i + 3]]), secondary)
else:
skip_node = 3
node = UnaryNode(op[0], parse([tokens[i + 3]]), secondary)
else:
skip_node = 3
node = UnaryNode(op[0], parse([tokens[i + 3]]), secondary)
new_tokens.append(node)
else:
new_tokens.append(token)
else:
new_tokens.append(token)
return new_tokens