pyebnf/operator.py
def infix_to_postfix(nodes, *, recurse_types=None):
  """Convert a list of nodes in infix order to a list of nodes in postfix order.

  E.G. with normal algebraic precedence, 3 + 4 * 5 -> 3 4 5 * +
  """
  output = []
  operators = []

  for node in nodes:
    if isinstance(node, OperatorNode):
      # Drain out all operators whose precedence is gte the node's...
      cmp_operator = node.operator
      while operators:
        current_operator = operators[-1].operator
        if current_operator.precedence > cmp_operator.precedence or \
           current_operator.precedence == cmp_operator.precedence and current_operator.association == Association.left:
          output.append(operators.pop())
        else:
          break
      operators.append(node)
    else:
      if recurse_types is not None and node.node_type in recurse_types:
        output.extend(infix_to_postfix(node.children, recurse_types=recurse_types))
      else:
        output.append(node)

  return output + list(reversed(operators))
def infix_to_postfix(nodes, *, recurse_types=None):
  """Convert a list of nodes in infix order to a list of nodes in postfix order.

  E.G. with normal algebraic precedence, 3 + 4 * 5 -> 3 4 5 * +
  """
  output = []
  operators = []

  for node in nodes:
    if isinstance(node, OperatorNode):
      # Drain out all operators whose precedence is gte the node's...
      cmp_operator = node.operator
      while operators:
        current_operator = operators[-1].operator
        if current_operator.precedence > cmp_operator.precedence or \
           current_operator.precedence == cmp_operator.precedence and current_operator.association == Association.left:
          output.append(operators.pop())
        else:
          break
      operators.append(node)
    else:
      if recurse_types is not None and node.node_type in recurse_types:
        output.extend(infix_to_postfix(node.children, recurse_types=recurse_types))
      else:
        output.append(node)

  return output + list(reversed(operators))
Convert a list of nodes in infix order to a list of nodes in postfix order.

  E.G. with normal algebraic precedence, 3 + 4 * 5 -> 3 4 5 * +