'Helper function for __init__: read the grammar if it is a
string.'

def _read_grammar(self, grammar, root_label, trace):

    rules = []
    lhs = None
    for line in grammar.split(u'\n'):
        line = line.strip()
        m = re.match(u'(?P<nonterminal>(\\.|[^:])*)(:(?P<rule>.*))', line)
        if m:
            self._add_stage(rules, lhs, root_label, trace)
            lhs = m.group(u'nonterminal').strip()
            rules = []
            line = m.group(u'rule').strip()
        if ((line == u'') or line.startswith(u'#')):
            continue
        rules.append(RegexpChunkRule.fromstring(line))
    self._add_stage(rules, lhs, root_label, trace)
