Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from queue import Queue
- """
- http://algo1567.ru/wp-content/uploads/2015/09/4_expressions.pdf
- """
- def calculate(formula):
- formula = "".join(formula.split())
- matching = re.findall(pattern, formula)
- tokens = [i for i in matching]
- pattern = r'[A-Z][A-Z0-9]*|!|->|&|\||[()]'
- var_patter = r'[A-Z][A-Z0-9]*'
- with open("b.txt") as f:
- formula = f.readline()
- matching = re.findall(pattern, formula)
- matching = [i for i in matching]
- cursor = 0
- sz = len(matching)
- def parse_impl():
- global cursor
- r_a = parse_disj()
- while cursor < sz and matching[cursor] == '->':
- cursor += 1
- r_b = parse_impl()
- r_a = "(->,{},{})".format(r_a, r_b)
- return r_a
- def parse_disj():
- global cursor
- r_a = parse_conj()
- while cursor < sz and matching[cursor] is '|':
- operator = matching[cursor]
- cursor += 1
- r_b = parse_conj()
- r_a = "({},{},{})".format(operator, r_a, r_b)
- return r_a
- def parse_conj():
- global cursor
- r_a = parse_unary()
- while cursor < sz and matching[cursor] is '&':
- operator = matching[cursor]
- cursor += 1
- r_b = parse_unary()
- r_a = "({},{},{})".format(operator, r_a, r_b)
- return r_a
- def parse_unary():
- global cursor
- if matching[cursor] is '!':
- cursor += 1
- r_a = parse_unary()
- return "(!{})".format(r_a)
- elif matching[cursor] is '(':
- cursor += 1
- r_a = parse_impl()
- cursor += 1
- return r_a
- elif re.match(var_patter, matching[cursor]) is not None:
- print("here{}".format(matching[cursor]))
- r_a = matching[cursor]
- cursor += 1
- return r_a
- a = parse_impl()
- print(a == '(->,P,(->,(!QQ),(|,(&,(!R10),S),(&,(&,(!T),U),V))))')
- print(a)
- print('(->,P,(->,(!QQ),(|,(&,(!R10),S),(&,(&,(!T),U),V))))')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement