def parse_sql(sql, verbose=False):
  """Parse SQL string and return Node representing parse tree."""
  tokens = sql_tokenizer.tokenize_sql(sql)

  rules = []
  for rule_string in RULES:
    rules.append(Rule.from_string(rule_string))

  # Add rule `T -> token` for every non-keyword token in input.
  for token in set(tokens):
    if token not in KEYWORDS:
      rules.append(Rule("T", token))

  # Re-split on spaces.
  tokens = " ".join(tokens).split(" ")
  if verbose:
    print("Parsing SQL:")
    for idx, token in enumerate(tokens):
      print("%s - %s" % (idx, token))

  nodes = _run_parser(tokens, rules, verbose=verbose)
  if not nodes:
    raise ValueError("No parse for SQL: %s" % sql)
  elif len(nodes) > 1:
    raise ValueError("Multiple parses for SQL: %s (%s)" % (sql, nodes))
  return nodes[0]
Parse SQL string and return Node representing parse tree.
language/compgen/nqg/tasks/spider/sql_parser.py