def deserialize_top(serialized):
  """Deserializes the logical form string into a TopLF object.

  Args:
    serialized: Serialized logical form string.

  Returns:
    a TopLF object if the deserialization is successful; None otherwise.
  """
  # If there is no colon after IN, the logical form needs to be deformatted.
  if serialized.startswith("[IN "):
    serialized = deformat_serialized(serialized)
  try:
    stack = [[]]
    for token in serialized.split():
      if token[0] == "[":
        # Begin a subtree: start a new stack item to collect children.
        new_stack_item = [token[1:]]
        stack.append(new_stack_item)
      elif token == "]":
        # End a subtree: create a TopLF object and add it to the parent.
        child = TopLF(stack.pop())
        stack[-1].append(child)
      else:
        # Leaf token: directly add to the parent.
        if len(stack[-1]) > 1 and isinstance(stack[-1][-1], str):
          # Combine consecutive tokens together.
          stack[-1][-1] += " " + token
        else:
          stack[-1].append(token)
    assert len(stack) == 1 and len(stack[0]) == 1
    lf = stack[0][0]
    assert isinstance(lf, TopLF)
    return lf
  except (IndexError, AssertionError):
    return None
Deserializes the logical form string into a TopLF object.

  Args:
    serialized: Serialized logical form string.

  Returns:
    a TopLF object if the deserialization is successful; None otherwise.
  
se2/src/utils/top_utils.py