def convert_to_cfg_rule(self,
                          lhs,
                          rhs,
                          rule_idx,
                          nonterminal_prefix,
                          allowed_terminals=None):
    """Convert symbol strings to CFGRule.

    Args:
      lhs: String symbol for LHS.
      rhs: List of string symbols for RHS.
      rule_idx: Integer index for rule.
      nonterminal_prefix: String prefix for nonterminal symbols in `rhs`.
      allowed_terminals: If set, returns None if rhs contains terminals not in
        this set.

    Returns:
      A CFGRule.
    """
    rhs_symbols = []
    lhs_idx = self._get_nonterminal_id(lhs)
    for token in rhs:
      if token.startswith(nonterminal_prefix):
        symbol_idx = self._get_nonterminal_id(token[len(nonterminal_prefix):])
        rhs_symbols.append(
            cfg_rule.CFGSymbol(idx=symbol_idx, type=cfg_rule.NON_TERMINAL))
      else:
        if allowed_terminals and token not in allowed_terminals:
          return None
        symbol_idx = self._get_terminal_id(token)
        rhs_symbols.append(
            cfg_rule.CFGSymbol(idx=symbol_idx, type=cfg_rule.TERMINAL))
    rule = cfg_rule.CFGRule(idx=rule_idx, lhs=lhs_idx, rhs=rhs_symbols)
    return rule
Convert symbol strings to CFGRule.

    Args:
      lhs: String symbol for LHS.
      rhs: List of string symbols for RHS.
      rule_idx: Integer index for rule.
      nonterminal_prefix: String prefix for nonterminal symbols in `rhs`.
      allowed_terminals: If set, returns None if rhs contains terminals not in
        this set.

    Returns:
      A CFGRule.
    
language/compgen/csl/cky/cfg_converter.py