47 lines
941 B
Python
47 lines
941 B
Python
def consume(i):
|
|
return bool(i) and (i[0], i[1:])
|
|
|
|
def op(i):
|
|
return bool(i) and i[0] in '|&>' and (i[0], i[1:])
|
|
|
|
def atom(i):
|
|
return bool(i) and i[0].isalpha() and (i[0], i[1:])
|
|
|
|
def left_b(i):
|
|
return bool(i) and i[0] in '(' and (i[0], i[1:])
|
|
|
|
def right_b(i):
|
|
return bool(i) and i[0] in ')' and (i[0], i[1:])
|
|
|
|
def either(i, a, b):
|
|
return bool(i) and (a(i) or b(i))
|
|
|
|
def seq(*parsers):
|
|
def apply(i):
|
|
res = []
|
|
for p in parsers:
|
|
if not (n := p(i)):
|
|
print(i)
|
|
print(p)
|
|
return False
|
|
val, i = n
|
|
res.append(val)
|
|
return (res, i)
|
|
return apply
|
|
|
|
def term(i):
|
|
return either(i, atom, closed_exp)
|
|
|
|
def closed_exp(i):
|
|
return seq(left_b, term, op, term, right_b)(i)
|
|
|
|
def exp(i):
|
|
return seq(term, op, term)(i)
|
|
|
|
text = "a|(b&c)"
|
|
|
|
if result := exp(text):
|
|
val, left = result
|
|
print(val)
|
|
print(left)
|