-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse.py
49 lines (42 loc) · 1.19 KB
/
parse.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
from errors import LisSyntaxError
# KEYWORDS and TYPE CONVERSION =========
def atom(token):
"""
Takes a single token and tries in order:
(1) to convert to an int or float,
(2) to convert '#t' or '#f' into a boolean,
and returns the string if none of the above succeed.
"""
try:
return int(token)
return float(token)
except ValueError:
if token == '#t':
return True
elif token == '#f':
return False
else:
return token
# PARSER ===============================
def parse_tokens(tokens, parens=0):
"""
Parsing function: this relies on tokens being a generator
so that each token is only seen once.
---------
Arguments:
tokens - a generator of tokens
Output:
A list of lists representing the syntax tree.
"""
out = []
for t in tokens:
if t == '(':
lst, parens = parse_tokens(tokens, parens+1)
out.append(lst)
elif parens == 0 and t == ')':
raise LisSyntaxError('Unexpected ")"')
elif t == ')':
return (out, parens - 1)
else:
out.append(atom(t))
return out