def _parse_if_needed(parser, name, bases, contents):
    """
	Parse the Omega grammar in the given class, if any.

	Returns the parsed AST if there's a grammar to be compiled, otherwise
	None.
	"""
    grammarAttribName = '_%s__grammar' % (name,)
    if grammarAttribName not in contents:
        return False
    if parser is None:
        raise RuntimeError(
            "Class %s should set the '_parser' attribute to a valid parser for the grammar it defines."
             % (name,))
    omegaAST = parser.match(contents[grammarAttribName])
    assert omegaAST[0
        ] == 'grammar', 'Expected an Omega grammar AST, not %r' % (omegaAST,)
    assert len(omegaAST) > 1, 'Omega grammar AST must have at least one rule.'
    lastRuleName = None
    for rule in omegaAST[1:]:
        assert rule[0
            ] == 'rule', 'Omega grammar AST must contain rules, not %r' % (rule
            ,)
        lastRuleName = rule[1]
    if '_start' in contents:
        pass
    else:
        for cls in bases:
            if getattr(cls, '_start', None) != None:
                break
        else:
            contents['_start'] = lastRuleName
    return omegaAST
