Paste number 24849: Regular expression engine in 14 lines of Python

Index of paste annotations: 1 | 2 | 3 | 4 | 5 | 6 | 7

Paste number 24849: Regular expression engine in 14 lines of Python
Pasted by: psykotic
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+J69
Channel:None
Paste contents:
Raw Source | XML | Display As
def iconcat(xs, ys):
    for x in xs: yield x
    for y in ys: yield y

def nil(s):
    yield s

def seq(l, r):
    return lambda s: (sr for sl in l(s) for sr in r(sl))

def alt(l, r):
    return lambda s: iconcat(l(s), r(s))

def star(e):
    return lambda s: iconcat(nil(s), seq(e, star(e))(s))

def plus(e):
    return seq(e, star(e))

def char(c):
    def match(s):
        if s and s[0] == c: yield s[1:]
    return match

# c(a|d)+r
e = seq(char('c'), seq(plus(alt(char('a'), char('d'))), char('r')))

import sys
s = sys.stdin.readline()[:-1]
for r in e(s): print "Match with remainder:", r

Annotations for this paste:

Annotation number 1: 22222
Pasted by: 11111
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/1
Paste contents:
Raw Source | Display As
33333

Annotation number 2: 22222
Pasted by: 11111
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/2
Paste contents:
Raw Source | Display As
33333

Annotation number 3: Sir
Pasted by: Martin
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/3
Paste contents:
Raw Source | Display As
sdkao

Annotation number 4: multiple
Pasted by: Deepak
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/4
Paste contents:
Raw Source | Display As
23233

Annotation number 5: test
Pasted by: jens
When:7 years, 11 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/5
Paste contents:
Raw Source | Display As
testtext

Annotation number 6: 24849
Pasted by: 24849
When:7 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/6
Paste contents:
Raw Source | Display As
24849

Annotation number 7: oppo
Pasted by: dyrtrt
When:7 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+J69/7
Paste contents:
Raw Source | Display As
ertret

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.