예제 #1
0
def build_parser(gramfile, parser=None):
    """reads a (EBNF) grammar definition and builds a parser for it"""
    if parser is None:
        parser = Parser()
    setup_tokens(parser)
    # XXX: clean up object dependencies
    from pypy.rlib.streamio import open_file_as_stream
    stream = open_file_as_stream(gramfile)
    grammardef = stream.readall()
    stream.close()
    assert isinstance(grammardef, str)
    source = GrammarSource(GRAMMAR_GRAMMAR, grammardef)
    builder = EBNFBuilder(GRAMMAR_GRAMMAR, dest_parser=parser)
    GRAMMAR_GRAMMAR.root_rules['grammar'].match(source, builder)
    builder.resolve_rules()
    parser.build_first_sets()
    parser.keywords = builder.keywords
    return parser
예제 #2
0
def build_parser(gramfile, parser=None):
    """reads a (EBNF) grammar definition and builds a parser for it"""
    if parser is None:
        parser = Parser()
    setup_tokens(parser)
    # XXX: clean up object dependencies
    from pypy.rlib.streamio import open_file_as_stream
    stream = open_file_as_stream(gramfile)
    grammardef = stream.readall()
    stream.close()
    assert isinstance(grammardef, str)
    source = GrammarSource(GRAMMAR_GRAMMAR, grammardef)
    builder = EBNFBuilder(GRAMMAR_GRAMMAR, dest_parser=parser)
    GRAMMAR_GRAMMAR.root_rules['grammar'].match(source, builder)
    builder.resolve_rules()
    parser.build_first_sets()
    parser.keywords = builder.keywords
    return parser