Esempio n. 1
0
 def generate_target_program(self, tac):
     cg = CodeGenerator()
     for line in tac:
         print line
         cg.compile(line)
     with open(self.input_file[:-4] + ".asm", "w") as my_file:
         my_file.write(cg.program)
Esempio n. 2
0
    def compile(self, source, env=None, library=False):
        if env == None:
            env = Environment()

        # parse input
        (definitions, errors) = Parser().parse(source)

        # check for errors
        if len(errors) > 0:
            return ("", errors)

        # type checking
        for definition in definitions:
            TypeChecker().check(errors, env, definition) 

        # check for errors
        if len(errors) > 0:
            return ("", errors)

        # code generation
        code = ""
        generator = CodeGenerator()
        for node  in definitions:
            symbol = node[0].value()
            code += generator.generateDefinition(env, symbol, template)

        # generate library
        if library:
            code += generator.generateLibrary(template)

        return (code, errors)
Esempio n. 3
0
    def __init__(self, view=None, path=None):
        self.view = view
        self.image_path = path

        self.processor = CodeMagnetsImageProcessor()
        self.processor.train(readFromFile=True, path="data.csv")
        self.code_generator = CodeGenerator()
        self.code_runner = CodeRunner()
Esempio n. 4
0
    def printGeneratedCode(self, line):
        tokens = line.strip().split(" ")

        if len(tokens) < 2:
            print("No symbol specified")

        for symbol in tokens[1:]:
            if not symbol in self.env.elements:
                print("No symbol " + symbol)
                continue

            # code generation
            generator = CodeGenerator()
            code = generator.generateDefinition(self.env, symbol, template)
            if code == None:
                print("Native function")
            else:
                print(code.strip())
Esempio n. 5
0
class Controller:

    def __init__(self, view=None, path=None):
        self.view = view
        self.image_path = path

        self.processor = CodeMagnetsImageProcessor()
        self.processor.train(readFromFile=True, path="data.csv")
        self.code_generator = CodeGenerator()
        self.code_runner = CodeRunner()

    def set_view(self, v):
        self.view = v

    def set_image_path(self, path):
        self.image_path = path
        print "Controller.image_path set to: "+self.image_path

    def process(self):

        if not self._process_precondition():
            return None

        r = self.processor.process(self.image_path)

        print r

        self._send_output(r)

    def generate(self):
        x = self.do_code_generation()
        print "done generate()"
        return x

    def execute(self, code):
        print "Controller.execute(): "+code
        x = self.do_execute(code)
        print "done execute()"
        return x

    def _process_precondition(self):
        if not self.image_path:
            return False
        elif not self.processor:
            return False
        else:
            return True

    def _send_output(self, data):
        if self.view:
            self.view.show_code(data)

    def do_code_generation(self):
        return self.code_generator.get_code(self.view.get_output_text())

    def do_execute(self, c):
        return self.code_runner.execute(c)
Esempio n. 6
0
class Stitcher():

    def __init__(self, animation_name):
        self.code_gen = CodeGenerator(animation_name)
        self.animation_name = animation_name
        self.sheet = pil_image.new('RGBA', (1024,1024))
        self.frame_index = 0
        self.frame_count = 20

    def add_to_sheet(self, action, direction):
        start_frame = self.frame_index
        step = bpy.data.scenes[0].frame_step
        start, finish = bpy.data.actions[action].frame_range

        for frame_id in range(int(start), int(finish), int(step)):
            filepath = 'tmp/' + action + '_' + direction + '_' + str(frame_id).zfill(4) + '.png'
            frame = pil_image.open(filepath, "r")
            x = self.frame_index%16 * 64
            y = int(self.frame_index/16) * 64
            x2 = x + 64
            y2 = y + 64
            self.sheet.paste(frame, (x,y,x2,y2))
            self.frame_index += 1

        self.code_gen.add_frames(action, direction, start_frame, self.frame_index)

    def get_step_count(self, action, direction):
        max_steps = 10
        if os.path.exists('tmp/' + action + '_' + direction + '_0001.png'):
            step_count = 1
            while step_count < max_steps:
                if os.path.exists('tmp/' + action + '_' + direction + '_' + str(step_count).zfill(4) + '.png'):
                    break
                step_count += 1
            return step_count - 1

    def save(self):
        self.sheet.save('output/images/' + self.animation_name + '.png', 'PNG')
        self.code_gen.save()
Esempio n. 7
0
 def parse(self):
     instructions = self._program()
     # do not print any output if error occurs
     if self.total_error_flag == 0:
         if self.print_symbol_table == 0:
             self.observer.print_output()
         elif self.print_symbol_table == 1:
             self.visitor.visitScope(self.program_scope)
             self.visitor.end()
         elif self.print_symbol_table == 2:
             currinstruction = instructions
             self.visitor.start()
             # while(currinstruction is not None):
             currinstruction.visit(self.visitor)
             # currinstruction = currinstruction._next
         elif self.print_symbol_table == 3:
             environment = self.program_scope.make_environment()
             stack = []
             currinstruction = instructions
             #while curr is not None:
             #    self.interpret(curr, environment, stack)
             #    curr = curr._next
             #v = InterpreterVisitor(environment)
             #v.start()
             #currinstruction.int_visit(v)
             v = Interpreter(currinstruction, environment)
             v.start()
         elif self.print_symbol_table == 4:
             #environment = self.program_scope.make_code_generator_environment()
             #print environment
             self.program_scope.make_code_generator_environment()
             c = CodeGenerator(self.program_scope, 0, instructions)
             c.start()
             c.cgoutput()
         elif self.print_symbol_table == 5:
             self.program_scope.make_code_generator_environment()
             c = CodeGenerator(self.program_scope, 1, instructions, filename=self.filename+".s")
             c.start()
             c.cgoutput()
Esempio n. 8
0
    def process(self):
        ''' Process the assembly code '''

        lexicalAnalyzer = LexicalAnalyzer(self.args)
        syntaxAnalyzer = SyntaxAnalyzer(self.args)
        codeGenerator = CodeGenerator(self.args)

        # lexical analysis
        ok = lexicalAnalyzer.analyze()
        if not ok:
            return False

        # syntax analysis
        ok = syntaxAnalyzer.analyze(lexicalAnalyzer.tokens)
        if not ok:
            return False

        # code generation
        ok = codeGenerator.generate(syntaxAnalyzer.expressions)
        if not ok:
            return False

        return True
Esempio n. 9
0
def main():
    print '*** Parabench Module Preprocessor', VERSION, '***'
    
    #callPath = os.path.dirname(inspect.getfile(sys._getframe()))+'/'
    
    for module_file in glob.glob('modules/*.c'):
        module = Module(module_file)
        if VERBOSE:
            module.print_parameters()
        for source_file in hooks:
            cg = CodeGenerator(source_file)
            for hook in hooks[source_file]:
                print 'ppc: %s -> %s -> %s' % (module_file, source_file, hook)
                if generator[hook]:
                    g = generator[hook]()
                    template = Template(hook)
                    g.generate(template, module)
                else:
                    print 'No generator given for hook "%s"' % hook
            cg.write()
    
    
    # Copy production source files to gen dir:
    genfiles = hooks.keys()
    srcfiles = set(glob.glob('*.c') + glob.glob('*.h') + glob.glob('*.l') + glob.glob('*.y'))
    
    for g in genfiles:
        if g in srcfiles:
            srcfiles.remove(g)
    
    source = ' '.join(srcfiles)
    if source != '':
        os.system('cp %s gen/' % source)
    
    if VERBOSE:
        print '\n\nGenerated Code:'
        cg.print_code()
Esempio n. 10
0
class Parser:
    def __init__(self, input_code: str):  # TODO initialize
        self.symbol_table = SymbolTable()
        self.symbol_table.new_scope()
        self.pars_stack = Stack()
        self.scanner = Scanner(input_code, symbol_table=self.symbol_table)
        self.dfa = TransitionDiagram()  # TODO
        self.dfa.make_diagrams()
        self.look_ahead = ""
        self.current_token = None
        self.current_token_info = None
        self.pars_stack.push(self.dfa.start_state)
        self.code_generator = CodeGenerator(self.symbol_table)

    def run(self):
        token_info, self.look_ahead = self.scanner.look_ahead()
        ignored_string = ""
        while True:
            # print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
            top = self.pars_stack.top()
            # print("         Top : ", top.type, " ", top.name, " Look ahead=",self.look_ahead)
            if top.type == "state":
                state = top
                if state.is_final:
                    # print("         poped", top.name)
                    self.pars_stack.pop()
                    if state.is_omega_final:
                        print("Parsed successfully!")
                        break
                elif not state.is_final:
                    if self.look_ahead in state.goto_dict:  # key exists in dict
                        ignored_string = ""
                        pre_routines = Routine.string_to_routines(state.goto_dict[self.look_ahead][PRE_ROUTINES])
                        post_routines = Routine.string_to_routines(state.goto_dict[self.look_ahead][POST_ROUTINES])
                        self.code_generator.call_routines_list(pre_routines, self.current_token_info)
                        next_state = state.goto_dict[self.look_ahead][STATE]
                        if len(next_state) == 1:  # if edge is terminal
                            next_state = next_state[0]
                            # print("on a terminal edge")
                            # print("         poped ", self.pars_stack.top().name)
                            self.pars_stack.pop()
                            self.current_token = self.look_ahead
                            self.current_token_info = token_info
                            self.code_generator.call_routines_list(post_routines, current_token=self.current_token_info)
                            self.pars_stack.push(next_state)
                            # print("         pushed ", next_state.type, " ", next_state.name)
                            if not state.goto_dict[self.look_ahead][IS_EPSILON]:
                                token_info, self.look_ahead = self.scanner.look_ahead()
                        elif len(next_state) == 2:
                            # print("on a non-terminal edge")
                            # print("         poped ", self.pars_stack.top().name)
                            self.pars_stack.pop()
                            # print("         pushed ", next_state[1].name)
                            self.pars_stack.push(
                                next_state[1])  # next state in this diagram (not the nonterminal's diagram)
                            for routine in reversed(post_routines):
                                self.pars_stack.push(routine)
                            # print("         pushed ", next_state[0].name)
                            self.pars_stack.push(next_state[0])  # start state of the nonterminal
                        else:
                            print("ERROR in initializing states: next state with 0 or >2 elements")
                    else:  # key doesnt exist in dict
                        ignored_string += self.look_ahead
                        token_info, self.look_ahead = self.scanner.look_ahead()
                        if self.look_ahead == "$":
                            print("Error: reached end of file.")
                            break
                        print("Error in line:\n  Panic Mode: Error in input, ignored: ", ignored_string)
                        continue
                else:
                    print("!!?")
            elif top.type == "#":
                self.code_generator.call_routine(top, current_token=self.current_token_info)
                self.pars_stack.pop()
Esempio n. 11
0
 def __init__(self, animation_name):
     self.code_gen = CodeGenerator(animation_name)
     self.animation_name = animation_name
     self.sheet = pil_image.new('RGBA', (1024,1024))
     self.frame_index = 0
     self.frame_count = 20
Esempio n. 12
0
            if file.endswith('.vm'):
                f_name = file.split('.')[0]
                file_names.append(f_name)
        if len(file_names) == 0:
            raise ValueError("No files having '.vm' extension in the given directory.")
        if path[-1] == os.sep:          # in case path ends with an os separator (i.e. 'os.sep')
            dst_file = os.path.basename(path[:-1])
        else:
            dst_file = os.path.basename(path)
        dst_file += '.asm'
        bootstrap = True

    dst_file = open(os.path.join(path, dst_file), 'w+')

    parser = Parser()
    generator = CodeGenerator()

    bootstrap_code = ''
    if bootstrap:
        parsed_line = parser.parse('call Sys.init 0')
        bootstrap_code += '@256\n' + 'D=A\n' + '@SP\n' + 'M=D\n'
        bootstrap_code += generator.generate_code(parsed_line)
    dst_file.write(bootstrap_code)

    for f_name in file_names:
        generator.set_new_file_name(file_name=f_name)
        src_file = open(os.path.join(path, f_name+'.vm'), 'r')
        src_line = src_file.readline()
        while src_line:
            parsed_line = parser.parse(src_line)
            if parsed_line:
Esempio n. 13
0
if args.visualize:
    if not path.exists(args.visualize):
        os.makedirs(args.visualize)
    graph(parse_tree_root, path.join(args.visualize, 'original_ast'))

if parse_tree_root:
    static_semantic_analyzer = SemanticAnalyzer(parse_tree_root)
    static_semantic_analyzer.analyze()
    if args.visualize:
        static_semantic_analyzer.symbol_table.to_graph(
            path.join(args.visualize, 'symb_tab.png'))
    SemanticLogger.info(
        None, "Find {} warnings and {} errors".format(SemanticLogger.n_warn,
                                                      SemanticLogger.n_error))
    if SemanticLogger.n_error == 0:
        SemanticLogger.info(None, 'producing three address code')
        code_generator = CodeGenerator(parse_tree_root,
                                       static_semantic_analyzer.symbol_table)
        code_generator.gen_three_address_code()
        code_generator.write_file(out_file)
        SemanticLogger.info(None, 'done')

end = time.clock()

SemanticLogger.info(None, 'Time elapsed: {}s'.format(end - start))
print("=" * 20)

if args.visualize:
    graph(parse_tree_root, path.join(args.visualize, 'final_ast'))
Esempio n. 14
0
    path = argv[1]
    if not os.path.isfile(path):
        raise ValueError("Provided argument is not a file.")
    if not path.endswith('.asm'):
        raise ValueError("The file has to end with '.asm' extension.")

    path, src_file = os.path.split(path)
    f_name = src_file.split('.')[0]
    dst_file = f_name + '.hack'

    src_file = open(os.path.join(path, src_file), 'r')
    dst_file = open(os.path.join(path, dst_file), 'w+')

    parser = Parser()
    generator = CodeGenerator()

    parsed_lines = []
    src_line = src_file.readline()

    while src_line:
        parsed_line = parser.parse(src_line)
        if parsed_line[0]:
            if parsed_line[0] == 'JS':
                generator.add_jump_symb(key=parsed_line[1],
                                        value=parsed_line[2],
                                        file_line=parsed_line[6])
            else:
                parsed_lines.append(parsed_line)
        src_line = src_file.readline()
Esempio n. 15
0
class Parser:
    def __init__(self, file_name):
        self.symbol_table = SymbolTable()
        self.pc = 2
        self.symbol_table.entries.append(
            SymbolTableEntry('output', 'ID', None, 1, None, None))
        self.scope_stack: List[int] = [0]
        self.memory_manager = MemoryManager()
        self.scanner = Scanner(file_name, self.symbol_table)
        self.semantic_analyser = SemanticAnalyser(self.symbol_table,
                                                  self.scope_stack, self.pc,
                                                  self.memory_manager)
        self.code_generator = CodeGenerator(self.symbol_table,
                                            self.memory_manager, self.pc,
                                            self.scope_stack)

    def parse(self):
        def handle_program():
            handle_declaration_list()
            if not match('EOF'):
                raise Exception("expected EOF, instead got " + token[1])
            # todo: self.code_generator.code_gen_token(None, 'main_return_addr')
            self.code_generator.code_gen_token(None, 'main_return_addr')
            return

        def handle_declaration_list():
            if token[1] in [
                    'continue', 'break', ';', 'ID', '(', 'NUM', 'if', 'return',
                    '{', 'switch', 'while', 'EOF', '-'
            ]:
                return
            elif token[1] in ['int', 'void']:
                handle_declaration()
                handle_declaration_list()
            else:
                raise Exception("illegal " + token[1])

        def handle_declaration():
            handle_type_specifier()
            self.semantic_analyser.analyse_token(prev_token, 'save_type')
            if not match('ID'):
                raise Exception("Expected ID, instead got " + token[1])
            self.semantic_analyser.analyse_token(prev_token, 'save_token')
            self.semantic_analyser.analyse_token(prev_token,
                                                 'check_if_dec_before')
            handle_declaration_prime()
            self.semantic_analyser.analyse_token(None,
                                                 'pop_token_and_saved_type')
            return

        def handle_declaration_prime():
            if token[1] in [';', '[']:
                self.semantic_analyser.analyse_token(None, 'check_saved_type')
                handle_var_declaration_prime()
                self.semantic_analyser.analyse_token(prev_token,
                                                     'allocate_memory')
                return
            elif token[1] in ['(']:
                self.semantic_analyser.analyse_token(
                    None, 'determine_start_address_return_address')
                self.code_generator.code_gen_token(prev_token, 'p_return')
                if not match('('):
                    raise Exception("Expected (, instead got " + token[1])
                handle_params()
                self.semantic_analyser.analyse_token(None, 'assign_dim')
                if not match(')'):
                    raise Exception("Expected ), instead got " + token[1])
                handle_compound_stmt()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_var_declaration_prime():
            if token[1] in [';']:
                match(';')
                return
            elif token[1] in ['[']:
                match('[')
                if not match('NUM'):
                    raise Exception("Expected NUM instead got " + token[1])
                self.semantic_analyser.analyse_token(None, 'update_dim')
                if not match(']'):
                    raise Exception("Expected ] instead got " + token[1])
                if not match(';'):
                    raise Exception("Expected ;, instead got " + token[1])
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_type_specifier():
            if token[1] in ['int']:
                self.semantic_analyser.analyse_token(None, 'add_dim')
                match('int')
                return
            if token[1] in ['void']:
                match('void')
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_params_prime():
            if token[1] == 'ID':
                match('ID')
                self.semantic_analyser.analyse_token(None,
                                                     'illegal_ID_after_void')
                handle_param_prime()
                handle_param_list_prime()
                self.semantic_analyser.analyse_token(None, 'add_dim')
                return
            if token[1] == ')':
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_params():
            if token[1] == 'void':
                match('void')
                handle_params_prime()
                return
            if token[1] == 'int':
                match('int')
                if not match('ID'):
                    raise Exception("Expected ID, instead got " + token[1])
                self.semantic_analyser.analyse_token(None, 'add_dim')
                self.semantic_analyser.analyse_token(prev_token,
                                                     'allocate_memory_param')
                handle_param_prime()
                handle_param_list_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_param_list():
            handle_param()
            handle_param_list_prime()

        def handle_param_list_prime():
            if token[1] == ')':
                return
            if token[1] == ',':
                match(',')
                handle_param()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_param():
            handle_type_specifier()
            self.semantic_analyser.analyse_token(prev_token, 'check_type')
            match('ID')
            self.semantic_analyser.analyse_token(prev_token, 'add_dim')
            self.semantic_analyser.analyse_token(prev_token,
                                                 'allocate_memory_param')
            handle_param_prime()

        def handle_param_prime():
            if token[1] == '[':
                match('[')
                if not match(']'):
                    raise Exception("Expected ] instead got " + token[1])
                return
            if token[1] in [',', ')']:
                return
            else:
                raise Exception("illegal ", token[1])

        def handle_compound_stmt():
            if not match('{'):
                raise Exception("Expected {, instead got " + token[1])
            self.semantic_analyser.analyse_token(None, 'update_scope')
            handle_declaration_list()
            handle_statement_list()
            self.semantic_analyser.analyse_token(None, 'remove_prev_scope')
            if not match('}'):
                raise Exception("Expected }, instead got " + token[1])

        def handle_statement_list():
            if token[1] in [
                    'continue', 'break', ';', 'ID', '(', 'NUM', 'if', 'return',
                    '{', 'switch', 'while', '-'
            ]:
                handle_statement()
                handle_statement_list()
                return
            if token[1] in ['}', 'default', 'case']:
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_statement():
            if token[1] in ['continue', 'break', ';', 'ID', '(', 'NUM', '-']:
                handle_expression_stmt()
                return
            if token[1] == '{':
                handle_compound_stmt()
                return
            if token[1] == 'if':
                handle_selection_stmt()
                return
            if token[1] == 'while':
                handle_iteration_stmt()
                return
            if token[1] == 'return':
                handle_return_stmt()
                return
            if token[1] == 'switch':
                handle_switch_stmt()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_expression_stmt():
            if token[1] in ['ID', '(', 'NUM', '-']:
                handle_Nsign()
                handle_expression()
                if not match(';'):
                    raise Exception('Expected ;, instead got ' + token[1])
                return
            if token[1] == 'continue':
                match('continue')
                self.code_generator.code_gen_token(None, 'continue')
                if not match(';'):
                    raise Exception('Expected ;, instead got ' + token[1])
                return
            if token[1] == 'break':
                match('break')
                self.code_generator.code_gen_token(None, 'break')
                if not match(';'):
                    raise Exception('Expected ;, instead got ' + token[1])
                return
            if token[1] == ';':
                match(';')
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_selection_stmt():
            if not match('if'):
                raise Exception("Expected if, instead got " + token[1])
            if not match('('):
                raise Exception("Expected (, instead got " + token[1])
            handle_expression()
            if not match(')'):
                raise Exception("Expected ), instead got " + token[1])
            self.code_generator.code_gen_token(None, 'save')
            handle_statement()
            if not match('else'):
                raise Exception("Expected else, instead got " + token[1])
            self.code_generator.code_gen_token(None, 'jpf_save')
            handle_statement()
            self.code_generator.code_gen_token(None, 'jp')
            return

        def handle_iteration_stmt():
            self.code_generator.code_gen_token(None, 'save')
            if not match('while'):
                raise Exception("Expected while, instead got " + token[1])
            self.code_generator.code_gen_token(None, 'label')
            if not match('('):
                raise Exception("Expected (, instead got " + token[1])
            handle_expression()
            if not match(')'):
                raise Exception("Expected ), instead got " + token[1])
            self.code_generator.code_gen_token(None, 'save')
            handle_statement()
            self.code_generator.code_gen_token(None, 'while')
            return

        def handle_return_stmt():
            if not match('return'):
                raise Exception("Expected return, instead got " + token[1])
            handle_return_stmt_prime()
            self.code_generator.code_gen_token(None, 'handle_return')
            return

        def handle_return_stmt_prime():
            if token[1] == ';':
                match(';')
                return
            if token[1] in ['ID', 'NUM', '(']:
                handle_expression()
                if not match(';'):
                    raise Exception("Expected ;, instead got " + token[1])
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_switch_stmt():
            self.code_generator.code_gen_token(None, 'save')
            if not match('switch'):
                raise Exception("Expected switch, instead got " + token[1])
            if not match('('):
                raise Exception("Expected (, instead got " + token[1])
            handle_expression()
            if not match(')'):
                raise Exception("Expected ), instead got " + token[1])
            if not match('{'):
                raise Exception("Expected {, instead got " + token[1])
            handle_case_stmts()
            handle_default_stmt()
            self.code_generator.code_gen_token(None, 'switch')
            if not match('}'):
                raise Exception("Expected }, instead got " + token[1])
            return

        def handle_case_stmts():
            if token[1] in ['ID', '(', 'NUM', 'default']:
                return
            if token[1] == 'case':
                handle_case_stmt()
                self.code_generator.code_gen_token(None, 'case')
                handle_case_stmts()

        def handle_case_stmt():
            if not match('case'):
                raise Exception("Expected case, instead got " + token[1])
            if not match('NUM'):
                raise Exception("Expected NUM, instead got " + token[1])
            self.code_generator.code_gen_token(prev_token, 'pnum')
            self.code_generator.code_gen_token(None, 'cmp_save')
            if not match(':'):
                raise Exception("Expected :, instead got " + token[1])
            handle_statement_list()
            return

        def handle_default_stmt():
            if token[1] == 'default':
                match('default')
                if not match(':'):
                    raise Exception('Expected :, instead got ' + token[1])
                handle_statement_list()
                return
            if token[1] == '}':
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_expression():
            if token[1] == 'NUM':
                match('NUM')
                self.code_generator.code_gen_token(prev_token, 'pnum')
                handle_term_prime()
                handle_additive_expression_prime()
                handle_simple_expression_prime()
                return
            if token[1] == 'ID':
                match('ID')
                self.semantic_analyser.analyse_token(prev_token,
                                                     'check_id_save')
                self.code_generator.code_gen_token(prev_token, 'pid')
                handle_expression_prime()
                self.semantic_analyser.analyse_token(None, 'remove_id')
                return
            if token[1] == '(':
                match('(')
                handle_expression()
                if not match(')'):
                    raise Exception("Expected ), instead got " + token[1])
                handle_term_prime()
                handle_additive_expression_prime()
                handle_simple_expression_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_expression_prime():
            if token[1] in ['[', '=', '*', '+', '-', '==', '<', ')', ';', ']']:
                handle_var_prime()
                handle_expression_zegond()
                return
            if token[1] == '(':
                self.code_generator.code_gen_token(prev_token, 'p_return')
                match('(')
                handle_args()
                self.semantic_analyser.analyse_token(None, 'check_dim')
                if not match(')'):
                    raise Exception("Expected ), instead got " + token[1])
                self.code_generator.code_gen_token(None, 'call_function')
                handle_term_prime()
                handle_additive_expression_prime()
                handle_simple_expression_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_expression_zegond():
            if token[1] == '=':
                match('=')
                handle_expression()
                self.code_generator.code_gen_token(None, 'assign')
                return
            if token[1] in ['*', '+', '-', '==', '<', ')', ';', ']']:
                handle_term_prime()
                handle_additive_expression_prime()
                handle_simple_expression_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_var():
            pass

        def handle_var_prime():
            if token[1] in ['=', '*', '+', '-', '==', '<', ',', ')', ']', ';']:
                return
            if token[1] == '[':
                match('[')
                handle_expression()
                self.code_generator.code_gen_token(None, 'address_update')
                if not match(']'):
                    raise Exception("Expected ], instead got " + token[1])
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_simple_expression():
            pass

        def handle_simple_expression_prime():
            if token[1] in ['==', '<']:
                handle_relop()
                handle_additive_expression()
                self.code_generator.code_gen_token(None, 'handle_relop')
                return
            if token[1] in [',', ')', ']', ';']:
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_relop():
            if token[1] == '==':
                match('==')
                self.code_generator.code_gen_token(prev_token, 'prelop')
                return
            if token[1] == '<':
                match('<')
                self.code_generator.code_gen_token(prev_token, 'prelop')
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_additive_expression():
            if token[1] in ['(', 'NUM', 'ID']:
                handle_term()
                handle_additive_expression_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_additive_expression_prime():
            if token[1] in ['==', '<', ',', ')', ']', ';']:
                return
            if token[1] in ['+', '-']:
                handle_addop()
                handle_term()
                self.code_generator.code_gen_token(None, 'handle_addop')
                handle_additive_expression_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_addop():
            if token[1] == '+':
                match('+')
                self.code_generator.code_gen_token(prev_token, 'addop')
                return
            if token[1] == '-':
                match('-')
                self.code_generator.code_gen_token(prev_token, 'addop')
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_term():
            if token[1] in ['(', 'NUM', 'ID']:
                handle_factor()
                handle_term_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_term_prime():
            if token[1] == '*':
                match('*')
                handle_factor()
                self.code_generator.code_gen_token(None, 'mult')
                handle_term_prime()
                return
            if token[1] in ['==', '<', '+', '-', ')', ',', ']', ';']:
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_factor():
            if token[1] == 'NUM':
                match('NUM')
                self.code_generator.code_gen_token(prev_token, 'pnum')
                return
            if token[1] == 'ID':
                match('ID')
                self.semantic_analyser.analyse_token(prev_token,
                                                     'check_id_save')
                self.code_generator.code_gen_token(prev_token, 'pid')
                handle_factor_prime()
                self.semantic_analyser.analyse_token(None, 'remove_id')
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_factor_prime():
            if token[1] == '[':
                handle_var_prime()
                return
            if token[1] == '(':
                match('(')
                handle_args()
                self.semantic_analyser.analyse_token(None, 'check_dim')
                if not match(')'):
                    raise Exception("Expected ), instead got " + token[1])
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_call():
            pass

        def handle_args():
            if token[1] in ['ID', '(', 'NUM']:
                handle_arg_list()
                return
            if token[1] == ')':
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_arg_list():
            if token[1] in ['ID', '(', 'NUM']:
                self.semantic_analyser.analyse_token(None, 'add_dim')
                handle_expression()
                self.code_generator.code_gen_token(prev_token, 'put_input')
                handle_arg_list_prime()
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_arg_list_prime():
            if token[1] == ',':
                self.semantic_analyser.analyse_token(None, 'add_dim')
                match(',')
                handle_expression()
                handle_arg_list_prime()
                return
            if token[1] == ')':
                return
            else:
                raise Exception("illegal " + token[1])

        def handle_Nsign():
            if token[1] == '-':
                match('-')
                self.code_generator.code_gen_token(None, 'handle_sign')

        def match(terminal: str):
            nonlocal token
            nonlocal prev_token
            if token[1] == terminal:
                prev_token = (token[0], token[1])
                token = self.scanner.get_token()
                return True
            return False

        # todo: implement panic mode
        # todo: comment handler
        # todo: implement output function
        prev_token: (str, str) = (None, None)
        token: (str, str)
        token = self.scanner.get_token()
        handle_program()
        out = open('output.txt', 'w')
        counter = 0
        for line in self.code_generator.PB:
            if line is None:
                break
            out.write(str(counter) + '\t' + str(line) + '\n')
            counter += 1
        out.close()
Esempio n. 16
0
from CodeGenerator import CodeGenerator
import telebot
bot = telebot.TeleBot("1310630322:AAHk7aNFpZc4g2u0h_OC5J34XznoeTFJY88")

CodeGen = CodeGenerator()


@bot.message_handler(commands=['start'])
def start_handler(message):
    print(message)
    bot.send_message(
        message.chat.id,
        'Привет, я пока умею просто проверять коды. Попробуешь? Введи свой код.'
    )


@bot.message_handler(content_types=["text"])
def text_handler(message):
    print(message)
    if CodeGen.code_use(message.text) is True:
        bot.send_message(message.chat.id, "Ты великолепен, твой код работает!")
    else:
        bot.send_message(message.chat.id,
                         "Упс, ошибочка, код использован или не существует")


bot.polling()
Esempio n. 17
0
class ParserGen(object):
    maxTerm = 3  # sets of size < maxTerm are enumerated
    ls = "\n"

    tErr = 0  # error codes
    altErr = 1
    syncErr = 2

    usingPos = None  # usingPos: Position   "using" definitions from the attributed grammar

    errorNr = 0  # highest parser error number
    curSy = None  # symbol whose production is currently generated
    err = None  # generated parser error messages
    srcName = ''  # name of attributed grammar file
    srcDir = ''  # directory of attributed grammar file
    symSet = []

    codeGen = CodeGenerator()

    @staticmethod
    def Overlaps(s1, s2):
        assert isinstance(s1, set)
        assert isinstance(s2, set)
        ln = len(s1)
        for i in xrange(0, ln):
            if (i in s1) and (i in s2):
                return True
        return False

    @staticmethod
    def GenErrorMsg(errTyp, sym):
        assert isinstance(errTyp, int)
        assert isinstance(sym, Symbol)
        ParserGen.errorNr += 1
        ParserGen.err.write(ParserGen.ls + '      ' + str(ParserGen.errorNr) +
                            ' : "')
        if errTyp == ParserGen.tErr:
            if sym.name[0] == '"':
                ParserGen.err.write(str(DFA.Escape(sym.name)) + ' expected')
            else:
                ParserGen.err.write(str(sym.name) + ' expected')
        elif errTyp == ParserGen.altErr:
            ParserGen.err.write('invalid ' + str(sym.name))
        elif errTyp == ParserGen.syncErr:
            ParserGen.err.write('this symbol not expected in ' + str(sym.name))
        ParserGen.err.write('",')

    @staticmethod
    def NewCondSet(s):
        assert isinstance(s, set)
        for i in xrange(1, len(ParserGen.symSet)):
            # skip symSet[0] (reserved for union of SYNC sets)
            if s == ParserGen.symSet[i]:  #s.equals( ParserGen.symSet[i] ):
                return i
        ParserGen.symSet.append(copy.copy(s))
        return len(ParserGen.symSet) - 1

    @staticmethod
    def GenCond(s, p):
        assert isinstance(s, set)
        assert isinstance(p, Node)
        if p.typ == Node.rslv:
            ParserGen.codeGen.CopySourcePart(p.pos, 0)
        else:
            n = len(s)
            if n == 0:
                ParserGen.codeGen.write('False')  # should never happen
            elif n <= ParserGen.maxTerm:
                for i in xrange(0, len(Symbol.terminals)):
                    sym = Symbol.terminals[i]
                    assert isinstance(sym, Symbol)
                    if sym.n in s:
                        ParserGen.codeGen.write('self.la.kind == ')
                        ParserGen.PrintTermName(sym)
                        n -= 1
                        if n > 0:
                            ParserGen.codeGen.write(' or ')
            else:
                ParserGen.codeGen.write('self.StartOf(' +
                                        str(ParserGen.NewCondSet(s)) + ')')

    @staticmethod
    def GenCode(p, indent, isChecked):
        #assert isinstance( p, Node )
        assert isinstance(indent, int)
        assert isinstance(isChecked, set)
        while p is not None:
            if p.typ == Node.nt:  # Non-Terminals
                ParserGen.codeGen.Indent(indent)
                if p.retVar is not None:
                    ParserGen.codeGen.write(p.retVar + ' = ')
                ParserGen.codeGen.write('self.' + p.sym.name + '(')
                ParserGen.codeGen.CopySourcePart(p.pos, 0)
                ParserGen.codeGen.write(')\n')
            elif p.typ == Node.t:  # Terminals
                ParserGen.codeGen.Indent(indent)
                if p.sym.n in isChecked:
                    ParserGen.codeGen.write('self.Get( )\n')
                else:
                    ParserGen.codeGen.write('self.Expect(')
                    ParserGen.PrintTermName(p.sym)
                    ParserGen.codeGen.write(')\n')
            elif p.typ == Node.wt:
                ParserGen.codeGen.Indent(indent)
                s1 = Tab.Expected(p.next, ParserGen.curSy)
                s1 |= Tab.allSyncSets
                ParserGen.codeGen.write('self.ExpectWeak(')
                ParserGen.PrintTermName(p.sym)
                ParserGen.codeGen.write(', ' + str(ParserGen.NewCondSet(s1)) +
                                        ')\n')
            elif p.typ == Node.any:
                ParserGen.codeGen.Indent(indent)
                ParserGen.codeGen.write('self.Get()\n')
            elif p.typ == Node.eps:
                ParserGen.codeGen.Indent(indent)
                ParserGen.codeGen.write('pass\n')
            elif p.typ == Node.rslv:
                #ParserGen.codeGen.Indent( indent )
                #ParserGen.codeGen.write( 'pass\n' )
                pass  # Nothing to do
            elif p.typ == Node.sem:
                ParserGen.codeGen.CopySourcePart(p.pos, indent)
            elif p.typ == Node.sync:
                ParserGen.codeGen.Indent(indent)
                ParserGen.GenErrorMsg(ParserGen.syncErr, ParserGen.curSy)
                s1 = copy.copy(p.set)
                ParserGen.codeGen.write('while not (')
                ParserGen.GenCond(s1, p)
                ParserGen.codeGen.write('):\n')
                ParserGen.codeGen.Indent(indent + 1)
                ParserGen.codeGen.write('self.SynErr(' +
                                        str(ParserGen.errorNr) + ')\n')
                ParserGen.codeGen.Indent(indent + 1)
                ParserGen.codeGen.write('self.Get()\n')
            elif p.typ == Node.alt:
                s1 = Tab.First(p)
                p2 = p
                equal = (s1 == isChecked)
                while p2 is not None:
                    s1 = Tab.Expected(p2.sub, ParserGen.curSy)
                    ParserGen.codeGen.Indent(indent)
                    if p2 == p:
                        ParserGen.codeGen.write('if ')
                        ParserGen.GenCond(s1, p2.sub)
                        ParserGen.codeGen.write(':\n')
                    elif p2.down is None and equal:
                        ParserGen.codeGen.write('else:\n')
                    else:
                        ParserGen.codeGen.write('elif ')
                        ParserGen.GenCond(s1, p2.sub)
                        ParserGen.codeGen.write(':\n')
                    s1 |= isChecked
                    ParserGen.GenCode(p2.sub, indent + 1, s1)
                    p2 = p2.down
                if not equal:
                    ParserGen.codeGen.Indent(indent)
                    ParserGen.GenErrorMsg(ParserGen.altErr, ParserGen.curSy)
                    ParserGen.codeGen.write('else:\n')
                    ParserGen.codeGen.Indent(indent + 1)
                    ParserGen.codeGen.write('self.SynErr(' +
                                            str(ParserGen.errorNr) + ')\n')
            elif p.typ == Node.iter:
                ParserGen.codeGen.Indent(indent)
                p2 = p.sub
                ParserGen.codeGen.write('while ')
                if p2.typ == Node.wt:
                    s1 = Tab.Expected(p2.next, ParserGen.curSy)
                    s2 = Tab.Expected(p.next, ParserGen.curSy)
                    ParserGen.codeGen.write('self.WeakSeparator(')
                    ParserGen.PrintTermName(p2.sym)
                    ParserGen.codeGen.write(', ' +
                                            str(ParserGen.NewCondSet(s1)) +
                                            ', ' +
                                            str(ParserGen.NewCondSet(s2)) +
                                            ')')
                    s1 = set()
                    if p2.up or p2.next is None:
                        p2 = None
                    else:
                        p2 = p2.next
                else:
                    s1 = Tab.First(p2)
                    ParserGen.GenCond(s1, p2)
                ParserGen.codeGen.write(':\n')
                ParserGen.GenCode(p2, indent + 1, s1)
                ParserGen.codeGen.write('\n')
            elif p.typ == Node.opt:
                s1 = Tab.First(p.sub)
                ParserGen.codeGen.Indent(indent)
                ParserGen.codeGen.write('if (')
                ParserGen.GenCond(s1, p.sub)
                ParserGen.codeGen.write('):\n')
                ParserGen.GenCode(p.sub, indent + 1, s1)

            if p.typ != Node.eps and p.typ != Node.sem and p.typ != Node.sync:
                for val in xrange(0, len(isChecked)):
                    isChecked.discard(val)

            if p.up:
                break

            p = p.next

    @staticmethod
    def GenTokens(withNames):
        assert isinstance(withNames, bool)
        for sym in Symbol.terminals:
            if sym.name[0].isalpha():
                ParserGen.codeGen.write('   _' + sym.name + ' = ' +
                                        str(sym.n) + '\n')
        if withNames:
            ParserGen.codeGen.write('   # terminals\n')
            for sym in Symbol.terminals:
                ParserGen.codeGen.write('   ' + sym.symName + ' = ' +
                                        str(sym.n) + '\n')
            ParserGen.codeGen.write('   # pragmas\n')
            for sym in Symbol.pragmas:
                ParserGen.codeGen.write('   ' + sym.symName + ' = ' +
                                        str(sym.n) + '\n')
            ParserGen.codeGen.write('\n')

    @staticmethod
    def GenPragmas():
        for sym in Symbol.pragmas:
            ParserGen.codeGen.write('   _' + str(sym.name) + ' = ' +
                                    str(sym.n) + '\n')

    @staticmethod
    def GenCodePragmas():
        for sym in Symbol.pragmas:
            ParserGen.codeGen.write('if self.la.kind == ')
            ParserGen.PrintTermName(sym)
            ParserGen.codeGen.write(':\n')
            ParserGen.codeGen.CopySourcePart(sym.semPos, 4, True)

    @staticmethod
    def GenProductions():
        for sym in Symbol.nonterminals:
            ParserGen.curSy = sym

            # Generate the function header
            ParserGen.codeGen.write('   def ' + sym.name + '( self')
            if sym.attrPos is not None:
                ParserGen.codeGen.write(', ')
            ParserGen.codeGen.CopySourcePart(sym.attrPos, 0)
            ParserGen.codeGen.write(' ):\n')

            # Generate the function body
            ParserGen.codeGen.CopySourcePart(sym.semPos, 2)
            ParserGen.GenCode(sym.graph, 2, set())

            # Generate the function close
            if sym.retVar is not None:
                ParserGen.codeGen.write('      return ' + sym.retVar + '\n')
            ParserGen.codeGen.write('\n')

    @staticmethod
    def InitSets():
        for i in xrange(0, len(ParserGen.symSet)):
            s = ParserGen.symSet[i]
            ParserGen.codeGen.write('      [')
            j = 0
            for sym in Symbol.terminals:
                if sym.n in s:
                    ParserGen.codeGen.write('T,')
                else:
                    ParserGen.codeGen.write('x,')
                j += 1
                if j % 4 == 0:
                    ParserGen.codeGen.write(' ')
            if i == (len(ParserGen.symSet) - 1):
                ParserGen.codeGen.write('x]\n')
            else:
                ParserGen.codeGen.write('x],\n')

    @staticmethod
    def Init(fn, dir):
        assert isinstance(fn, str)
        assert isinstance(dir, str)
        ParserGen.srcName = fn
        ParserGen.srcDir = dir
        ParserGen.errorNr = -1
        ParserGen.usingPos = None

    @staticmethod
    def WriteParser(withNames):
        assert isinstance(withNames, bool)
        assert isinstance(Tab.allSyncSets, set)
        ParserGen.symSet.append(Tab.allSyncSets)

        ParserGen.codeGen.openFiles('Parser.frame', ParserGen.srcName,
                                    'Parser.py', True)

        if withNames:
            Tab.AssignNames()

        ParserGen.err = StringIO.StringIO()
        for sym in Symbol.terminals:
            ParserGen.GenErrorMsg(ParserGen.tErr, sym)

        ParserGen.codeGen.CopyFramePart('-->begin')
        if ParserGen.usingPos != None:
            ParserGen.codeGen.write('\n')
            ParserGen.codeGen.CopySourcePart(ParserGen.usingPos, 0)
        ParserGen.codeGen.CopyFramePart('-->constants')
        ParserGen.GenTokens(withNames)
        ParserGen.codeGen.write('   maxT = ' + str(len(Symbol.terminals) - 1) +
                                '\n')
        ParserGen.GenPragmas()
        ParserGen.codeGen.CopyFramePart('-->declarations')
        ParserGen.codeGen.CopySourcePart(Tab.semDeclPos, 0)
        ParserGen.codeGen.CopyFramePart('-->pragmas')
        ParserGen.GenCodePragmas()
        ParserGen.codeGen.CopyFramePart('-->productions')
        ParserGen.GenProductions()
        ParserGen.codeGen.CopyFramePart('-->parseRoot')
        ParserGen.codeGen.write(Tab.gramSy.name + '()\n')
        ParserGen.codeGen.write('      self.Expect(')
        ParserGen.PrintTermName(Tab.eofSy)
        ParserGen.codeGen.write(')\n')
        ParserGen.codeGen.CopyFramePart('-->initialization')
        ParserGen.InitSets()
        ParserGen.codeGen.CopyFramePart('-->errors')
        ParserGen.codeGen.write(str(ParserGen.err.getvalue()))
        ParserGen.codeGen.CopyFramePart('$$$')
        ParserGen.codeGen.close()

    @staticmethod
    def WriteStatistics():
        Trace.WriteLine()
        Trace.WriteLine('Statistics:')
        Trace.WriteLine('-----------')
        Trace.WriteLine()
        Trace.WriteLine(str(len(Symbol.terminals)) + ' terminals')
        Trace.WriteLine(
            str(
                len(Symbol.terminals) + len(Symbol.pragmas) +
                len(Symbol.nonterminals)) + ' symbols')
        Trace.WriteLine(str(len(Node.nodes)) + ' nodes')
        Trace.WriteLine(str(len(ParserGen.symSet)) + ' sets')
        Trace.WriteLine()

    @staticmethod
    def PrintTermName(sym):
        assert isinstance(sym, Symbol)
        assert isinstance(sym.symName, (str, unicode)) or (sym.symName is None)
        if sym.symName is None:
            ParserGen.codeGen.write(str(sym.n))
        else:
            ParserGen.codeGen.write('Scanner.')
            ParserGen.codeGen.write(str(sym.symName))
Esempio n. 18
0
     else:
         output = Output()
         factory = AstFactory(output)
         parser = Parser(token_array, output, factory)
         #print 'parse start'
         parser.parse()
         print 'parse success'
         if parser.get_error_message() != '':
             print parser.get_error_message()
             exit()
             
         symbol_table = factory.get_symbol_table()
         ast_root = factory.get_ast_root()
         #print 'code generation start'
         if symbol_table:
             code_generation = CodeGenerator(symbol_table, ast_root)
             code_generation.generate_code()
             code_generation.print_code()
     
 else:
     #if the option is -s, it will run the scanner and produce a list of tokens
     if  sys.argv[1] == '-s':
         input_string = '';
         #if the second argument exist, it means read from file
         if len(sys.argv) == 3:
             #Read the file
             if os.path.exists(sys.argv[2]):
                 f = open(sys.argv[2])
                 input_string = f.read()
             #if the directory of the file does not exist
             else:
Esempio n. 19
0
#!/bin/env/python
# coding=utf-8
"""
程序运行入口
"""
from LogTool import logger
from CodeGenerator import CodeGenerator
from ErrorUtil import ErrorUtil
import time
import Target
from ExcelConf import excel_conf

logger.debug("gen start")

# package_name = raw_input("Please input java package name(like com.xxx.xxx):")
CodeGenerator.set_package_name(excel_conf.get("package_name"))
CodeGenerator.set_protocol_file(excel_conf.get("protocol_file"))
CodeGenerator.set_service_name(excel_conf.get("service_name"))
CodeGenerator.set_option_comment(True)
CodeGenerator.set_option_json_serialize(False)
module_list = [
]
CodeGenerator.extend_import_module(module_list)

start_time = time.clock()
CodeGenerator.run(Target.Target_pmbank)
CodeGenerator.run(Target.Target_openapi)
CodeGenerator.run(Target.Target_normal)
stop_time = time.clock()
use_time = stop_time - start_time