def bin_priority(op,left,right):
  "I don't know how to handle order of operations in the LR grammar, so here it is"
  # note: recursion limits protect this from infinite looping. I'm serious. (i.e. it will crash rather than hanging)
  if isinstance(left,BinX) and left.op < op: return bin_priority(left.op,left.left,bin_priority(op,left.right,right))
  elif isinstance(left,UnX) and left.op < op: return un_priority(left.op,BinX(op,left.val,right)) # note: obviously, no need to do this when right is a UnX
  elif isinstance(right,BinX) and right.op < op: return bin_priority(right.op,bin_priority(op,left,right.left),right.right)
  else: return BinX(op,left,right)
def bin_priority ( op , left , right ) : if isinstance ( left , BinX ) and left . op < op : return bin_priority ( left . op , left . left , bin_priority ( op , left . right , right ) ) elif isinstance ( left , UnX ) and left . op < op : return un_priority ( left . op , BinX ( op , left . val , right ) ) elif isinstance ( right , BinX ) and right . op < op : return bin_priority ( right . op , bin_priority ( op , left , right . left ) , right . right ) else : return BinX ( op , left , right )
I don't know how to handle order of operations in the LR grammar, so here it is