示例#1
0
 def cfg_filter(self, arg=None):
     '''
     cfg筛选,获取候选函数存入self.candidate_func中
     '''
     if arg is None:
         # 默认获取下一对候选函数
         self.cur_func['origin'], self.cur_func[
             'patch'] = self.func_generator.next()
     elif isinstance(arg, int) or isinstance(arg, str):
         # 按地址或函数名获取指定候选函数
         self.cur_func['origin'], self.cur_func[
             'patch'] = self.binfile.get_func_graphs(arg)
     elif isinstance(arg, list) and isinstance(arg[0], FuncGraph):
         # 直接传递候选函数
         self.cur_func['origin'], self.cur_func['patch'] = arg[0], arg[1]
     self.cfg['origin'] = CFG(self.cur_func['origin'])
     self.cfg['patch'] = CFG(self.cur_func['patch'])
     funcname = self.cfg['origin'].funcname
     self.func_cfg_centroid[funcname] = {
         'origin': self.cfg['origin'].get_centroid(),
         'patch': self.cfg['patch'].get_centroid()
     }
     if not self.cfg['origin'].same_with(self.cfg['patch']):
         self.candidate_func[funcname] = {
             'origin': self.cfg['origin'].address,
             'patch': self.cfg['patch'].address
         }
示例#2
0
def intersect(cfg, nfa):
    ret = CFG()
    Nt = cfg.nonterms
    St = list(nfa.states)
    for _ in range(len(Nt) * len(St)**2  -  1):
        ret.addNonterm()
    NtInv = { Nt[i]:i for i in range(len(Nt)) }
    StInv = { St[i]:i for i in range(len(St)) }
    def getNterm(p, A, q):
        idx = (NtInv[A]*len(St) + StInv[p])*len(St) + StInv[q]
        return ret.nonterms[idx]
    ret.start = getNterm(nfa.start, cfg.start, nfa.final)
    for lhs, rhsSet in cfg.productions.items():
        for rhs in rhsSet:
            items = [(x, [], x) for x in St]
            for term in rhs:
                if term in Nt:
                    newItems = []
                    for z in St:
                        newItems.extend([(x, R + [getNterm(y, term, z)], z) for (x, R, y) in items])
                    items = newItems
                else:
                    newItems = []
                    for (x, R, y) in items:
                        newItems.extend([(x, R + [term], z) for z in nfa.transitions.get(x, {}).get(term,     [])])
                        newItems.extend([(x, R + [term], z) for z in nfa.transitions.get(x, {}).get(WILDCARD, [])])
                    items = newItems
            for (p, R, q) in items:
                ret.addProduction(getNterm(p, lhs, q), R)
    return ret
示例#3
0
 def testOut(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     n5 = Numeral(5)
     n5.type = int_type
     n1 = Numeral(1)
     n1.type = int_type
     stmt = Operation(FunctionCall(Name('__out__'), [n5, n1]))
     cfg.connect(cfg.entry, stmt, cfg.exit)
     function.cfg = cfg
     function.symbol_table = SymbolTable()
     cfg.symbol_table = SymbolTable(function.symbol_table)
     program = Program([function])
     self.assertSuccess(program)
     self.assertTrue(
         function.cfg.has_path(
             function.cfg.entry,
             Operation(BinaryOperation([Name('$t0'), '=',
                                        Numeral(5)])),
             Operation(BinaryOperation([Name('$t1'), '=',
                                        Numeral(1)])),
             Operation(
                 FunctionCall(Name('__out__'),
                              [Name('$t0'), Name('$t1')])),
             function.cfg.exit))
示例#4
0
    def execute_test(self, file, filepath, capsys):
        lexer = APLLexer()
        lexer.build()

        parser = APLYacc(output=YaccOutput.AST)
        parser.build(lexer)

        f = open(filepath)
        ast = parser.parse(f.read())

        ast_str = ''
        if ast:
            ast_str = str(ast)

        output = open(os.path.join(TestSymtab.tests_out, file + ".ast")).read()
        errout = capsys.readouterr().err
        assert output.strip() == (errout + ast_str).strip()

        cfg_str = ''
        if ast:
            cfg = CFG(ast)
            cfg_str = str(cfg)
        output = open(os.path.join(TestSymtab.tests_out, file + ".cfg")).read()
        assert output.strip() == (errout + cfg_str).strip()

        symtab_str = ''
        if ast:
            symtab_str = symtab_from_ast(parser, ast)
        output = open(os.path.join(TestSymtab.tests_out, file + ".sym")).read()
        assert output.strip() == (errout + symtab_str).strip()
示例#5
0
文件: test_ssa.py 项目: SamL98/MyDeco
    def test_ssa_complex_2(self):
        cfg = CFG(complex2_cfg_blocks)
        cfg.convert_to_ssa()

        for blk in cfg.blocks[::-1]:
            print(blk.name)
            print(blk)
            print()
示例#6
0
 def testEmpty(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     cfg.connect(cfg.entry, cfg.exit)
     function.cfg = cfg
     program = Program([function])
     linearise = self.assertSuccess(program)
     self.assertEquals(
         linearise.lines,
         [Label('f', public=True),
          Label('f$exit', public=True)])
示例#7
0
    def p_code(self, p):
        'code : VOID MAIN LPAREN RPAREN block'
        logging.debug('body: %s' % (p[5]))

        for node in p[5]:
            self.ast_file.write(str(node))

        self.ast_file.close()

        cfg = CFG(p[5])
        self.cfg_file.write(str(cfg))
        self.cfg_file.close()
示例#8
0
文件: test_cfg.py 项目: SamL98/MyDeco
    def test_insert_phis_complex_1(self):
        cfg = CFG(complex1_cfg_blocks)
        phis_inserted = cfg.insert_phis()

        self.assertEqual(phis_inserted, 1)

        for blk in [c1_block1, c1_block2, c1_block3]:
            for insn in blk.insns:
                self.assertEqual(insn.num_phis(), 0)

        insn = c1_block4.insns[0]
        self.assertEqual(insn.num_phis(), 1)
        self.assertEqual(insn.pcode[0].output, r3)
示例#9
0
 def testOneStatement(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     stmt = cfg.add(Operation(42))
     cfg.connect(cfg.entry, stmt)
     cfg.connect(stmt, cfg.exit)
     function.cfg = cfg
     program = Program([function])
     linearise = self.assertSuccess(program)
     self.assertEquals(linearise.lines, [
         Label('f', public=True),
         Instruction(42),
         Label('f$exit', public=True)
     ])
示例#10
0
文件: test_cfg.py 项目: SamL98/MyDeco
    def test_insert_phis_complex_2(self):
        cfg = CFG(complex2_cfg_blocks)
        phis_inserted = cfg.insert_phis()

        self.assertEqual(phis_inserted, 2)

        for blk in [c2_block1, c2_block2, c2_block3, c2_block5]:
            for insn in blk.insns:
                self.assertEqual(insn.num_phis(), 0)

        insn = c2_block4.insns[0]
        self.assertEqual(insn.num_phis(), 2)

        outputs = set([pcop.output for pcop in insn.pcode if pcop.is_phi()])
        self.assertEqual(outputs, set({r3, r1}))
示例#11
0
 def testInfiniteLoop(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     node = cfg.add(Operation(42))
     cfg.connect(cfg.entry, node, node)
     function.cfg = cfg
     program = Program([function])
     linearise = self.assertSuccess(program)
     self.assertEquals(linearise.lines, [
         Label('f', public=True),
         Label(2),
         Instruction(42),
         Jump(2),
         Label('f$exit', public=True)
     ])
示例#12
0
    def visit_FunctionDecl(self, func):
        cfg = CFG(func.name)
        func.cfg = cfg

        if hasattr(func, 'symbol_table'):
            cfg.symbol_table = SymbolTable(func.symbol_table.parent)
            cfg.symbol_table.embed(func.symbol_table)
        else:
            cfg.symbol_table = SymbolTable()

        prev_node = self.visit(func.body,
                               cfg=cfg,
                               entry=cfg.entry,
                               exit=cfg.exit)
        if prev_node is not None:
            cfg.connect(prev_node, cfg.exit)
示例#13
0
    def p_code(self, p):
        'code : global_statement_list'
        logging.debug('code: %s' % (p[1]))

        for node in p[1]:
            self.ast_file.write(str(node))

        self.ast_file.close()

        cfg = CFG(p[1])
        self.cfg_file.write(str(cfg))
        self.cfg_file.close()

        # print(self.tableptr.top())
        print_procedures(self.tableptr.top(), self.sym_file)
        print_variables(self.tableptr.top(), self.sym_file)
示例#14
0
 def parse_cfg(self):
     """Read from the stream, return a CFG object.
     This CFG object will be used to run the tasks.
     """
     lines = self.read_section()
     it = iter(lines)
     # variables and terminals are comma separated, with no whitespace
     vas = re.sub('\s', '', next(it)).split(',')
     tes = re.sub('\s', '', next(it)).split(',')
     start = next(it)
     # the remaining lines are rules V -> production
     rules = list()
     for line in it:
         v, production = line.split('->')
         v = v.strip()
         # separate on whitespace (excluding leading or trailing whitespace)
         production = re.sub('\s', ' ', production.strip())
         production = production.split(' ')
         rules.append((v, production))
     return CFG(vas, tes, start, rules)
示例#15
0
 def testTest(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     test_node = cfg.add(Test(42))
     cfg.connect(cfg.entry, test_node)
     true_node = cfg.add(Operation(100))
     cfg.connect(test_node, TrueEdge(), true_node, cfg.exit)
     false_node = cfg.add(Operation(200))
     cfg.connect(test_node, FalseEdge(), false_node, cfg.exit)
     function.cfg = cfg
     program = Program([function])
     linearise = self.assertSuccess(program)
     self.assertEquals(linearise.lines, [
         Label('f', public=True),
         Branch(42, 3),
         Label(4),
         Instruction(200),
         Jump('f$exit'),
         Label(3),
         Instruction(100),
         Label('f$exit', public=True)
     ])
示例#16
0
import SATA_PRETREAT
import os
import shutil
import scipy
import argparse
import matplotlib.pyplot as plt
import datetime
import GETPERDATA
from pyfaidx import Fasta
from cfg import CFG

parser = argparse.ArgumentParser()
parser.add_argument('-f', help="the folder input and ouput.")
args = parser.parse_args()

cfg = CFG()

# The below items should be check, modify if nessary.
OUTPUT = cfg.output
CLINICAL_ITEM = cfg.clinical_item
MIN_ALIVE = cfg.min_alive
VOTE_CLASS = 'Class_4'
VOTE_LINE = 1
CALC_NUM = 10
SATA_LIST = [  # 'ICD_O3_pathology',
    # 'ICD_O3_site',
    # 'ajcc_stage',
    'patient_age',
    # 'patient_gender',
    # 'patient_race',
    'patient_weight'
示例#17
0
        right_sides = []
        for rule in self.rules:
            if (node_name in rule):
                right_side = rule.split(" -> ")
                right_sides.append(right_side)
        return node(node_name, right_sides)

    def _initNodes(self):
        for i in self.rules:
            sides = i.split(" -> ")
            if (sides[0] not in self.nodes.keys()):
                self.nodes[sides[0]] = self._initNode(sides[0])


if __name__ == "__main__":
    in_file = open("CFG.txt", 'r')
    rules = in_file.read().split('\n')
    root_name = rules.pop()
    tracker = Tracker(rules, root_name)

    from cfg import CFG

    g = CFG(set(tracker.nodes.keys()), set(tracker.alphabet + ['_']),
            set(tracker.Readable_rules), tracker.root.name, '_')

    string = input("Enter a string: ")

    if g.cyk(string):
        print("Grammar can generate the string!")
    else:
        print("Grammar cannot generate the string!")
示例#18
0
            table = []
            for i in range(0, len(words) + 1):
                table.append([])
            self.cky_table.append(table)

        # self.display()
        for j in range(1, len(words) + 1):
            self.cky_table[j - 1][j] = list(
                set([x for x in grammar.return_rule([words[j - 1]])]))
            for i in range(j - 1, -1, -1):
                for k in range(i, j):
                    a = self.cky_table[i][k]
                    b = self.cky_table[k][j]

                    c = [
                        grammar.return_rule(r)[0]
                        for r in itertools.product(a, b)
                        if grammar.return_rule(r) != []
                    ]

                    self.cky_table[i][j] = self.cky_table[i][j] + c

        self.display()


if __name__ == '__main__':
    cfg = CFG('midterm.cfg')
    cky = CKY()

    cky.cky_parse(['flies', 'like', 'arrows'], cfg)
        path = None
        out = None
        for i in [1, 3]:
            if args[i] in ['-P', '--path']:
                path = args[i + 1]
            if args[i] in ['-O', '--output']:
                out = args[i + 1]
        if path is None or out is None:
            print_help()
    else:
        print_help()
    return path, out


if __name__ == "__main__":

    path, out = get_op()

    _, file = os.path.split(path)
    name, _ = os.path.splitext(file)
    sym_tab = build_symtab(path)
    with open(path, 'r') as f:
        lines = f.readlines()
    cfg = {}
    for key in sym_tab.keys():
        cfg[key] = CFG(
            lines[sym_tab[key]["lines"][1]:sym_tab[key]["lines"][2]], key)
    cg = FCG(cfg, "foo", sym_tab)

    generate_jpg(cg, name, out)
    generate_cfg(cg, name, out)
示例#20
0

if __name__ == '__main__':
    class Num:
        def __init__(self):
            pass
        def __str__(self):
            return 'Num'
        def __repr__(self):
            return 'Num'
        def __hash__(self):
            return hash(0)
        def __eq__(self, other):
            return isinstance(other, Num)

    gram = CFG()
    expr = gram.start
    term = gram.addNonterm()
    fact = gram.addNonterm()
    WS = gram.addNonterm()
    gram.addProduction(fact, [WS, Num(), WS])
    gram.addProduction(fact, [WS, '-', Num(), WS])
    gram.addProduction(fact, [WS, '(', expr, ')', WS])
    gram.addProduction(term, [term, WS, '*', WS, fact])
    gram.addProduction(term, [term, WS, '/', WS, fact])
    gram.addProduction(term, [WS, fact, WS])
    gram.addProduction(expr, [expr, '+', term])
    gram.addProduction(expr, [expr, '-', term])
    gram.addProduction(expr, [WS, term, WS])
    gram.addProduction(WS, [WS, '\t'])
    gram.addProduction(WS, [WS, ' '])
示例#21
0
文件: test_ssa.py 项目: SamL98/MyDeco
 def test_ssa_simple(self):
     cfg = CFG(simple_cfg_blocks)
     cfg.convert_to_ssa()
def buildCFG(cfile, function=None):
    """This function will allow us to create the CFG for a specific function
	or for the entire translation Unit.

	Parameters
	----------
	tu_cursor : `obj`:Cursor
		Cursor of the Translation Unit.
	function : str
		Name of the function from which we want to obtain the CFG.

	Return
	------
	`obj`:CFG
		A CFG object.

	"""
    # First we need to build de AST
    tu_cursor = buildAST(cfile)
    if function:
        # Retrieving the children from the translation unit
        tu_child = tu_cursor.get_children()
        func = None
        for e in tu_child:
            if str(e.spelling) == str(function):
                func = e
        # The function is not in the translation unit
        if not func:
            return None
        else:
            f_childs = func.get_children()
            # Avoiding ParamDecl and looking for the CompoundStmt
            elm = [c for c in f_childs]
            # The compound stmt is always at the end of the children of a func_decl
            c_stmt = elm[-1]
            # Transform the cursors of the function and the compound stmt to CFG decorators of
            # function and compound stmt
            f_decorator = Decl(func)
            c_stmt_decorator = CompoundStmt(c_stmt)
            # instance of a cfg object
            cfg = CFG()
            return cfg.buildCFG(f_decorator, c_stmt_decorator)

    else:
        # Retrieving the children from the translation Unit
        tu_childs = tu_cursor.get_children()
        cfgs = []
        # for each children of the translation unit, we check if it is a func_decl
        # by looking for the compound stmt
        cfg = CFGBuilder()
        for child in tu_childs:
            # If the element is a Funct Decl Cursor

            if child.kind is CursorKind.FUNCTION_DECL:
                childs = [c for c in child.get_children()]
                # Check if the last element is a compound statement
                if len(childs) > 0:
                    if childs[-1].kind is CursorKind.COMPOUND_STMT:
                        # FIXIT: accessing to a 'static' variable,
                        # find another way to access to the CursorKind
                        c_stmt = childs[-1]
                        # Transform the cursors of the function and the compound stmt to CFG decorators of
                        # function and compound stmt
                        f_decorator = FunctionDecl(child)
                        c_stmt_decorator = CompoundStmt(c_stmt)
                        # instance of a cfg object

                        # build the cfg
                        # print cfg
                        cfg_b = cfg.buildCFG(f_decorator, c_stmt_decorator)
                        # Save the cfg
                        cfgs.append([child.spelling, cfg_b])
        return cfgs
示例#23
0
            table = []
            for i in range(0, len(words) + 1):
                table.append([])
            self.cky_table.append(table)

        # self.display()
        for j in range(1, len(words) + 1):
            self.cky_table[j - 1][j] = list(
                set([x for x in grammar.return_rule([words[j - 1]])]))
            for i in range(j - 1, -1, -1):
                for k in range(i, j):
                    a = self.cky_table[i][k]
                    b = self.cky_table[k][j]

                    c = [
                        grammar.return_rule(r)[0]
                        for r in itertools.product(a, b)
                        if grammar.return_rule(r) != []
                    ]

                    self.cky_table[i][j] = self.cky_table[i][j] + c

        self.display()


if __name__ == '__main__':
    cfg = CFG('main.cfg')
    cky = CKY()

    cky.cky_parse(['the', 'large', 'can', 'can', 'hold', 'the', 'water'], cfg)
示例#24
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Main file of pyCFG2Chomsky. """

import sys

from cfg import ContextFreeGrammar as CFG

if __name__ == '__main__':
    myCFG = CFG(sys.stdin)
    print "End of input."
    myCFG.transform_to_Chomsky()
    # print "\nChomsky Normal Form Grammar\nRules:"
    myCFG.print_rules()
    # print "Terminals:"
    # for term in myCFG.terminals:
    # print term
    # print "Nonterminals:"
    # print myCFG.nonterminals
示例#25
0
import argparse
import torch
import torch.utils.data as data
from loader_voxforge import *
from cfg import CFG

import csv

config = CFG()
"""
data_path_predict = "data/presentation"
vx = VOXFORGE(data_path_predict, langs=config.vx.langs,
              ratios=[0., 0., 1.],
              label_type="lang", use_cache=False,
              use_precompute=False)
vx.transform = config.vx.transform
vx.target_transform = config.vx.target_transform
vx = config.vx
vx.set_split("train")
config.vx = vx
dl = data.DataLoader(vx, batch_size=1, shuffle=False)
"""

config.vx.set_split("test")
RLENC = {v: k for (k, v) in config.vx.target_transform.vocab.items()}

model = config.model
model.eval()
correct = 0

for i, (mb, tgt) in enumerate(config.dl):
示例#26
0
 def __init__(self, function: MCFunction):
     # self.regs = get_regs_from_instructions(instructions)
     # self.bbs = get_bbs_from_instructions(instructions)
     self.function = function
     self.cfg = CFG(function.body)
     self.reg_maps = self.get_reg_maps()
示例#27
0
文件: test_ssa.py 项目: SamL98/MyDeco
 def test_ssa_complex_1(self):
     cfg = CFG(complex1_cfg_blocks)
     cfg.convert_to_ssa()
示例#28
0
 def __init__(self, function: MCFunction, use_saved=False):
     self.cfg = CFG(function.body)
     self.use_saved = use_saved
     self.function = function
     self.reg_maps = self.get_reg_maps()
示例#29
0
import csv
import random
import sys
from cfg import CFG
cfg1 = CFG()
cfg1.add_prod('S', 'x|y|( S + S )|( S if B else S )')
cfg1.add_prod('B', ' ( S < S )|( S == S )|( S > S )|True|False')

if __name__ == '__main__':
    f = open(sys.argv[1])
    reader = csv.reader(f, delimiter=',')
    eg = [(r[0], r[1], r[2]) for r in reader]
    progs_done = 0
    OUTPUT = False
    while True:
        p = cfg1.get_next_prog()
        if (progs_done % 1000) == 0:
            OUTPUT = True
        if (OUTPUT):
            print("{} Programs evaluated".format(progs_done))
            print("Evaluating program {}".format(p))
        correct = 0
        for e in eg:
            if (OUTPUT):
                print("  Evaluating example {}".format(e))
            p_inst = p.replace('x', e[0])
            p_inst = p_inst.replace('y', e[1])
            if (OUTPUT):
                print("  Evaluation = %d" % eval(p_inst))
            if (eval(p_inst) == int(e[2])):
                correct += 1
示例#30
0
    def _evaluate(self):
        self._change_widgets_state(tkinter.DISABLED)

        try:
            rules = set()
            for line in self.rules_text.get("1.0", 'end-1c').strip().split('\n'):
                line = line.strip()
                if not line:
                    continue

                line_parts = line.split('->')
                if len(line_parts) != 2:
                    raise ValueError("Rule syntax error : {}".format(line))

                line_parts = [line_part.strip() for line_part in line_parts]

                if line_parts[1].count('|') != 0:
                    second_parts = line_parts[1].split('|')
                    for second_part in second_parts:
                        second_part = second_part.strip()
                        if not second_part:
                            raise ValueError("Rule syntax error : {}".format(line))

                        rules.add((line_parts[0], second_part))

                else:
                    rules.add((line_parts[0], line_parts[1]))

            variables = set()
            for variable in self.variables_entry.get().strip().split(','):
                variable = variable.strip()
                if not variable:
                    continue

                variables.add(variable)

            terminals = set()
            for terminal in self.terminals_entry.get().strip().split(','):
                terminal = terminal.strip()
                if not terminal:
                    continue

                terminals.add(terminal)

            _cfg = CFG(variables, terminals, rules, self.start_variable_entry.get(), self.null_character_entry.get())

            cfg = [copy(_cfg) for _ in range(5)]
            cfg[1].remove_null_rules()
            cfg[2].remove_null_rules()
            cfg[2].remove_unit_rules()
            cfg[3].simplify()
            cfg[4].chamsky()

        except ValueError as e:
            messagebox.showerror("Input CFG Error", e.args[0])
            self.cfg = [None for _ in range(5)]
            return

        self.cfg = cfg
        self._change_widgets_state(tkinter.NORMAL)
        self._change_grammar_mode()