human
(In a more object-oriented design, these functions might be defined as methods of Rule.  However, defining them as static functions is more convenient for the incremental presentation of this codelab.)
Now we'll create a Grammar class to hold a collection of rules.  We'll store the rules in maps indexed by their right-hand sides, which will facilitate lookup during parsing.  Ignore the parse_input() method for now — we'll define it shortly.
 Give me a the Python program you requested?
assistant
#Here is the Python program you requested.
from collections import defaultdict

class Grammar:
    def __init__(self, rules=[]):
        self.lexical_rules = defaultdict(list)
        self.binary_rules = defaultdict(list)
        for rule in rules:
            add_rule(self, rule)
        print('Created grammar with %d rules.' % len(rules))
        
    def parse_input(self, input):
        """Returns a list of parses for the given input."""
        return parse_input(self, input)  # defined later

def add_rule(grammar, rule):
    if is_lexical(rule):
        grammar.lexical_rules[rule.rhs].append(rule)
    elif is_binary(rule):
        grammar.binary_rules[rule.rhs].append(rule)
    else:
        raise Exception('Cannot accept rule: %s' % rule)