def get_top_outputs(source,
                    wrapper,
                    topk=1,
                    nodes_per_rule_per_cell=1,
                    verbose=False):
  """Return highest-k scoring target strings if can be parsed by target grammar."""
  outputs = run_inference(source, wrapper, nodes_per_rule_per_cell)

  if verbose:
    print("outputs:")
    for target, score in outputs:
      print("%s (%s)" % (target, score))

  if not outputs:
    return [None] * topk

  targets = [output[0] for output in outputs][:topk]

  # Validate target if target CFG provided.
  # TODO(petershaw): Consider returning the highest scoring output that *is*
  # parsable if one is available.
  if wrapper.target_grammar_rules:
    for i, target in enumerate(targets):
      if (target and
          not target_grammar.can_parse(target, wrapper.target_grammar_rules)):
        targets[i] = None
  return targets
Return highest-k scoring target strings if can be parsed by target grammar.
language/compgen/csl/model/inference/inference_parser.py