def can_parse(self, tokens, verbose=False):
    """Return True if can be parsed given target CFG."""
    input_symbols = []
    terminal_ids = set()
    for token in tokens:
      if qcfg_rule.is_nt(token):
        idx = self.converter.nonterminals_to_ids[PLACEHOLDER_NT]
        input_symbols.append(cfg_rule.CFGSymbol(idx, cfg_rule.NON_TERMINAL))
      else:
        if token not in self.converter.terminals_to_ids:
          if verbose:
            print("token `%s` not in `converter.terminals_to_ids`: %s" % (
                token, self.converter.terminals_to_ids))
          return False
        idx = self.converter.terminals_to_ids[token]
        terminal_ids.add(idx)
        input_symbols.append(cfg_rule.CFGSymbol(idx, cfg_rule.TERMINAL))

    # Filter rules that contain terminals not in the input.
    def should_include(parser_rule):
      for symbol in parser_rule.rhs:
        if symbol.type == cfg_rule.TERMINAL and symbol.idx not in terminal_ids:
          return False
      return True

    filtered_rules = [
        rule for rule in self.parser_rules if should_include(rule)]
    if verbose:
      print("filtered_rules:")
      for rule in filtered_rules:
        print(rule)

    def populate_fn(unused_span_begin, unused_span_end,
                    unused_parser_rule, unused_children):
      return [True]

    nonterminals = set(self.converter.nonterminals_to_ids.values())
    parses = cfg_parser.parse_symbols(
        input_symbols,
        filtered_rules,
        nonterminals,
        nonterminals,
        populate_fn,
        postprocess_fn=None,
        max_single_nt_applications=2,
        verbose=verbose)
    if parses:
      return True
    else:
      return False
Return True if can be parsed given target CFG.
language/compgen/csl/qcfg/qcfg_target_parser.py