def _create_mapping(grammar):
    """
    Create mapping between symbols and rules rewritable to these symbols.
    :param grammar: Grammar to use.
    :return: Tuple of two dictionaries.
    First dictionary maps rule to terminal hash.
    Second dictionary maps rule to nonterminals hash.
    """
    termmap = dict()
    rulemap = dict()
    for r in grammar.rules:
        if len(r.right) == 1:
            h = hash(r.toSymbol)
            if h not in termmap:
                termmap[h] = set()
            termmap[h].add(r)
        else:
            key = hash(tuple(r.right))
            if key not in rulemap:
                rulemap[key] = set()
            rulemap[key].add(r)
    return termmap, rulemap
