line.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from node import Leaf
  2. def generate_line(root):
  3. """
  4. Print an expression tree in a single text line. Where needed, add
  5. parentheses.
  6. >>> from node import Node, Leaf
  7. >>> l0, l1 = Leaf(1), Leaf(2)
  8. >>> plus = Node('+', l0, l1)
  9. >>> print generate_line(plus)
  10. 1 + 2
  11. >>> plus2 = Node('+', l0, l1)
  12. >>> times = Node('*', plus, plus2)
  13. >>> print generate_line(times)
  14. (1 + 2) * (1 + 2)
  15. >>> l2 = Leaf(3)
  16. >>> uminus = Node('-', l2)
  17. >>> times = Node('*', plus, uminus)
  18. >>> print generate_line(times)
  19. (1 + 2) * -3
  20. >>> exp = Leaf('x')
  21. >>> inf = Leaf('oo')
  22. >>> minus_inf = Node('-', inf)
  23. >>> integral = Node('int', exp, minus_inf, inf)
  24. >>> print generate_line(integral)
  25. int(x, -oo, oo)
  26. """
  27. operators = [
  28. ('+', '-'),
  29. ('*', '/', 'mod'),
  30. ('^', )
  31. ]
  32. max_pred = len(operators)
  33. def is_operator(node):
  34. """
  35. Check if a given node is an operator (otherwise, it's a function).
  36. """
  37. label = node.title()
  38. either = lambda a, b: a or b
  39. return reduce(either, map(lambda x: label in x, operators))
  40. def pred(node):
  41. """
  42. Get the precedence of an operator node.
  43. """
  44. # Check binary and n-ary operators
  45. if not isinstance(node, Leaf) and len(node) > 1:
  46. op = node.title()
  47. for i, group in enumerate(operators):
  48. if op in group:
  49. return i
  50. # Unary operator and leaves have highest precedence
  51. return max_pred
  52. def traverse(node):
  53. """
  54. The expression tree is traversed using preorder traversal:
  55. 1. Visit the root
  56. 2. Traverse the subtrees in left-to-right order
  57. """
  58. if not node:
  59. return '<empty expression>'
  60. op = node.title()
  61. if not node.nodes:
  62. return op
  63. arity = len(node)
  64. if is_operator(node):
  65. if arity == 1:
  66. # Unary operator
  67. result = op + traverse(node[0])
  68. else:
  69. # N-ary operator
  70. node_pred = pred(node)
  71. result = ''
  72. sep = ' ' + op + ' '
  73. e = []
  74. for i, child in enumerate(node):
  75. exp = traverse(child)
  76. # Check if there is a precedence conflict
  77. # If so, add parentheses
  78. child_pred = pred(child)
  79. if child_pred < node_pred or \
  80. (i and child_pred == node_pred and op != child.title()):
  81. exp = '(' + exp + ')'
  82. e.append(exp)
  83. # Check if a multiplication sign is nessecary
  84. if op == '*':
  85. left, right = node
  86. # Get the previous multiplication element if the arity is
  87. # greater than 2
  88. if left.title() == '*':
  89. left = left[1]
  90. # a * b -> ab
  91. # a * 2 -> a * 2
  92. # a * (b) -> a(b)
  93. # (a) * b -> (a)b
  94. # (a) * (b) -> (a)(b)
  95. # 2 * a -> 2a
  96. left_id = is_id(left)
  97. right_id = is_id(right)
  98. left_paren = e[0][-1] == ')'
  99. right_paren = e[1][0] == '('
  100. left_int = is_int(left)
  101. if (left_id or left_paren or left_int) \
  102. and (right_id or right_paren):
  103. sep = ''
  104. result += sep.join(e)
  105. else:
  106. # Function call
  107. result = op + '(' + ', '.join(map(traverse, node)) + ')'
  108. return result
  109. return traverse(root)
  110. def is_id(node):
  111. return isinstance(node, Leaf) and not node.title().isdigit()
  112. def is_int(node):
  113. return isinstance(node, Leaf) and node.title().isdigit()