-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz3-11.py
32 lines (24 loc) · 839 Bytes
/
quiz3-11.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
#----------------
# User Instructions
#
# Write the compiler for alt(x, y) in the same way that we
# wrote the compiler for lit(s) and seq(x, y).
'''
def matchset(pattern, text):
op, x, y = components(pattern)
if 'lit' == op:
return set([text[len(x):]]) if text.startswith(x) else null
elif 'seq' == op:
return set(t2 for t1 in matchset(x, text) for t2 in matchset(y, t1))
elif 'alt' == op:
return matchset(x, text) | matchset(y, text)
'''
def lit(s): return lambda text: set([text[len(s):]]) if text.startswith(s) else null
def seq(x, y): return lambda text: set().union(*map(y, x(text)))
def alt(x, y): return lambda text: x(text) | y(text)
null = frozenset([])
def test():
g = alt(lit('a'), lit('b'))
assert g('abc') == set(['bc'])
return 'test passes'
print test()