Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import sys
- pattern = r'[a-zA-Z][a-zA-Z0-9]*|!|->|&|\||[()]'
- var_patter = r'[a-zA-Z][a-zA-Z0-9]*'
- def parse_exception(inp):
- matching = re.findall(pattern, inp)
- matching = [i for i in matching]
- cursor = 0
- sz = len(matching)
- if sz is 0:
- return ""
- def parse_implication():
- nonlocal cursor, matching, sz
- r_a = parse_disjunction()
- while cursor < sz and matching[cursor] == '->':
- cursor += 1
- r_b = parse_implication()
- r_a = "(->,{},{})".format(r_a, r_b)
- return r_a
- def parse_disjunction():
- nonlocal cursor, matching, sz
- r_a = parse_conjunction()
- while cursor < sz and matching[cursor] is '|':
- cursor += 1
- r_b = parse_conjunction()
- r_a = "({},{},{})".format('|', r_a, r_b)
- return r_a
- def parse_conjunction():
- nonlocal cursor, matching, sz
- r_a = parse_unary()
- while cursor < sz and matching[cursor] is '&':
- cursor += 1
- r_b = parse_unary()
- r_a = "({},{},{})".format('&', r_a, r_b)
- return r_a
- def parse_unary():
- nonlocal cursor, matching, sz
- if matching[cursor] is '!':
- cursor += 1
- r_a = parse_unary()
- return "(!{})".format(r_a)
- elif matching[cursor] is '(':
- cursor += 1
- r_a = parse_implication()
- cursor += 1
- return r_a
- elif re.match(var_patter, matching[cursor]) is not None:
- r_a = matching[cursor]
- cursor += 1
- return r_a
- return parse_implication()
- def main():
- sys.setrecursionlimit(2 ** 30)
- formula = []
- with sys.stdin as f:
- for x in f.readline():
- if not x.isspace():
- formula.append(x)
- print(parse_exception("".join(formula)), end='')
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement