-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
224 lines (191 loc) · 6.58 KB
/
parser.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
"""
This file contains the JSON parser.
"""
from symbols import *
class SyntaxError(Exception):
pass
class Parser(object):
"""
Class with basic functionality for parsers.
--- DO NOT MODIFY THIS CLASS ---
"""
def __init__(self, tokens):
"""
Initialize the parser.
tokens is a list of pairs of the form:
[(t1, v1), (t2, v2), ...]
where ti's are in terminals, and vi's are values attached to them.
The list is in the format returned by the lexer.
--- DO NOT MODIFY THIS FUNCTION ---
"""
self.tokens = tokens
self.pos = -1
self.advance() # updates self.t, which keeps the current terminal
def advance(self):
"""
Return the value attached to current token, and advance by one.
Return EOF once all tokens are exhausted.
--- DO NOT MODIFY THIS FUNCTION ---
"""
if self.pos < len(self.tokens):
value = self.tokens[self.pos][1]
else:
value = EOF
self.pos += 1
if self.pos < len(self.tokens):
self.t = self.tokens[self.pos][0]
else:
self.t = EOF
return value
def match(self, terminal):
"""
Match the next token against the given terminal. Raise a
SyntaxError if they do not match.
If they do, return the value attached to the current token.
--- DO NOT MODIFY THIS FUNCTION ---
"""
if self.t == terminal:
value = self.advance()
print "matched {:10} {}".format(terminal, value)
return value
else:
raise SyntaxError("Syntax error: expected {}, found {}".format(
terminal, self.t))
class JsonParser(Parser):
"""
A JSON parser.
--- COMPLETE THIS CLASS IN QUESTION 5 ---
"""
def parse(self):
"""
Parse the input by parsing the start symbol (obj) and then matching EOF.
--- DO NOT MODIFY THIS FUNCTION ---
"""
result = self.parse_obj()
self.match(EOF)
return result
def parse_keyvalue(self):
"""
An example parse_<nonterminal> function.
--- DO NOT MODIFY THIS FUNCTION ---
"""
if self.t in [STRING]:
c1 = self.match(STRING)
c2 = self.match(COLON)
c3 = self.parse_value()
return (keyvalue, (c1, c2, c3))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_obj(self):
if self.t in [LB]:
c1 = self.match(LB)
c2 = self.parse_obj_right()
return (obj, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_obj_right(self):
if self.t in [RB]:
c1 = self.match(RB)
return (obj_right, (c1,))
elif self.t in [STRING]:
c1 = self.parse_members()
c2 = self.match(RB)
return (obj_right, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_members(self):
if self.t in [STRING]:
c1 = self.parse_keyvalue()
c2 = self.parse_members_right()
return (members, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_members_right(self):
if self.t in [RB]:
return (members_right, ())
elif self.t in [COMMA]:
c1 = self.match(COMMA)
c2 = self.parse_members()
return (members_right, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_value(self):
if self.t in [INT, STRING]:
c1 = self.match(self.t)
return (value, (c1,))
elif self.t in [LB]:
c1 = self.parse_obj()
return (value, (c1,))
elif self.t in [LS]:
c1 = self.parse_array()
return (value, (c1,))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_array(self):
if self.t in [LS]:
c1 = self.match(LS)
c2 = self.parse_array_right()
return (array, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_array_right(self):
if self.t in [RS]:
c1 = self.match(RS)
return (array_right, (c1,))
elif self.t in [INT, LB, STRING, LS]:
c1 = self.parse_array_members()
c2 = self.match(RS)
return (array_right, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_array_members(self):
if self.t in [INT, LB, STRING, LS]:
c1 = self.parse_value()
c2 = self.parse_array_members_right()
return (array_members, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def parse_array_members_right(self):
if self.t in [RS]:
return (array_members_right, ())
elif self.t in [COMMA]:
c1 = self.match(COMMA)
c2 = self.parse_array_members()
return (array_members_right, (c1, c2))
else:
raise SyntaxError("Syntax error: no rule for token: {}".format(self.t))
def main():
from lexer import lex
from tree_to_dot import tree_to_dot, view
json_example = open('json_example.json').read()
print json_example
tokens = lex(json_example)
parser = JsonParser(tokens)
parse_tree = parser.parse()
dot = tree_to_dot(parse_tree)
open('json_example.gv', 'w').write(dot)
view(dot)
print "\n*************************************"
print "Bad Example:\n"
json_bad_example = open('json_bad_example.json').read()
print json_bad_example
tokens = lex(json_bad_example)
parser = JsonParser(tokens)
try:
parser.parse()
except SyntaxError, e:
print e
else:
print "Error: expected syntax error in bad example wasn't raised."
print "\n*************************************"
print "Array Example:\n"
json_array_example = open('json_array_example.json').read()
print json_array_example
tokens = lex(json_array_example)
parser = JsonParser(tokens)
parse_tree = parser.parse()
dot = tree_to_dot(parse_tree)
open('json_array_example.gv', 'w').write(dot)
#view(dot)
if __name__ == '__main__':
main()