-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathre_compiler.py
More file actions
32 lines (24 loc) · 1.04 KB
/
re_compiler.py
File metadata and controls
32 lines (24 loc) · 1.04 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
#!bin/python2.7
"""
CS212 Udacity Design of computer Programs
Module 3 - Problem 3
Regular Expression API implementation - Compiler Form
"""
from re_api import search, test_search
def match(pattern, text):
"Match pattern against start of text; return longest match found or None."
remainders = pattern(text)
if remainders:
shortest = min(remainders, key=len)
return text[:len(text)-len(shortest)]
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)
def oneof(chars): return lambda text: set(text[1:]) if (text and text[0] in chars) else null
dot = lambda text: set(text[1:]) if text else null
eol = lambda text: set(['']) if text else null
def star(x): return lambda text: (set([text]) |
set(t2 for t1 in x(text) if t1 != text
for t2 in star(x)(t1)))
if __name__ == '__main__':
print test_search()