示例#1
0
def infer_grammar(path, oldtable, tables, conf):
    productions = dict()
    if oldtable is not None:
        ## TODO: clean this jankyness up! we shouldn't have to rejoin oldtable
        ## so we can parse it.
        productions.update(
          lib.parse_grammar('\n'.join(''.join(row) for row in oldtable))
        )

    def callback(productions, node, depth):
        if not node.children: return
        p = productions.get(node.label, set())
        p.add(tuple(kid.label for kid in node.children))
        productions[node.label] = p
    walktrees(conf['trees'], functools.partial(callback, productions))

    table = tuple(
        tuple([nonterm] + [':'.join(p) for p in P])
        for nonterm, P in productions.iteritems()
    )
    gramfile = '\n'.join(
        ' : '.join((
            nonterm,
            ' '.join(prod)
        ))
        for nonterm, prods in productions.iteritems()
        for prod in prods
    ) + '\n'
    with open(path, 'w') as f: f.write(gramfile)
    return table
示例#2
0
    if not ((len(args) != 0) or (usetables) or (stdin)):
        log('You must provide a list of syntax trees to characterize.')
        usage(error_codes['no_args'])

    file_paths = sorted(assert_file_exists(arg) for arg in args)
    syntax_trees = [mktree(read_file_or_die(path)) for path in file_paths]
    if stdin: syntax_trees += [mktree(tree) for tree in split_stdin()]
    coverage = load_coverage(coverage, file_paths)

    if requested_artifacts:
        genimgs = False
        gentables = False

    if grammar is not None:
        grammar = lib.parse_grammar(read_file_or_die(grammar))


    conf = {'trees':syntax_trees,
            'grammar': grammar,
            'outdir':assert_dir_exists(outdir),
            'loadtables':usetables,
            'loadpath':loadpath,
            'genimgs':genimgs,
            'gentables':gentables,
            'requested_artifacts':requested_artifacts,
            'excluded_artifacts':excluded,
            'coverage':coverage,
            'list_artifacts':list_artifacts,
    }