Example #1
0
 def evaluate(self, program, *existing_declarations):
     existing_declarations_ast = [Parser(decl).parse() if isinstance(decl, str) else decl for decl in
                                  existing_declarations]
     if not existing_declarations_ast:
         # ensure we have at least one existing declaration or the parser will not run the lvalue-transformation pass
         existing_declarations_ast.append(NativeNoArgumentFunctionDeclaration('unused', None, lambda: None))
     assert all(isinstance(a, Program) for a in existing_declarations_ast)
     assert isinstance(program, str)
     program_ast = Parser(program).parse(existing_declarations_ast)
     return program_ast.evaluate(Environment.empty())
Example #2
0
def test_test():
    source = """a jest równe 2.
                Zwiększ a o 5.
                Wypisz na ekranie a.
                b jest równe 4+a-(2-a).
                Zmniejsz b o b+1.
                c jest równe "jestem sobie zmienna c".
                Wypisz na ekranie b+b.
                Wypisz na ekranie "ELO :P".
                Wypisz na ekranie "Wpisałem w puste miejsca  _, _, _!", w puste miejsce wpisz "siema",1,(2-5).
                Wypisz na ekranie "Wpisałem w puste miejsce _, _!", w puste miejsce wpisz "elo", "siemano".
                Wypisz na ekranie "zmienna a = _, zmienna b = _, zmienna c = _!", w puste miejsce wpisz a,b,c.
                Wypisz na ekranie b jest wieksze od b.
                Wypisz na ekranie b jest mniejsze od b.
                Wypisz na ekranie b równa się b.
                Wypisz na ekranie b jest różne od b.
                Jeżeli b jest mniejsze od b to wypisz na ekranie "b<b". Tyle.
                Jeżeli a jest mniejsze od b to wypisz na ekranie "a<b". Tyle.
                Jeżeli b jest mniejsze od a to wypisz na ekranie "b<a". Tyle.
                """

    lexer = Lexer().get_lexer()
    tokens = lexer.lex(source)

    pg = Parser()
    pg.parse()
    parser = pg.get_parser()
    context = {}
    parser.parse(tokens).eval(context)
def main(argv):
    """Parse and run any Tiger program"""

    # check for arguments
    try:
        file = argv[1]
    except IndexError:
        print(
            "Expected one file name argument to be passed, e.g. ./tiger-interpreter program.tig"
        )
        return 40

    program_contents = read_file(argv[1])

    # set up environment
    environment = create_empty_environment()

    # parse input program
    try:
        program = Parser(program_contents,
                         argv[1]).parse(create_native_functions())
    except ParseError as e:
        print("Parse failure: %s" % e.to_string())
        return 42

    # evaluate the program
    result = program.evaluate(environment)

    # print the result and exit
    if result:
        print(result.to_string())
    return 0
Example #4
0
    def second_pass(self, file_lines):
        memory_address = self.MEM_START_ADDR
        for line in file_lines:
            parser = Parser(instruction=line)
            encoder = Encoder(instruction_type=parser.instruction_type)

            if parser.instruction_type == InstructionType.c_instruction:
                hack_line = encoder.encode(dest=parser.dest,
                                           comp=parser.comp,
                                           jump=parser.jump)

            elif parser.instruction_type == InstructionType.a_instruction:
                try:
                    integer_address = int(parser.address)
                except ValueError:
                    if self.symbol_table.get(parser.address) is None:
                        self.symbol_table[parser.address] = memory_address
                        memory_address += 1

                    integer_address = self.symbol_table.get(parser.address)

                hack_line = encoder.encode(address=integer_address)

            else:
                continue

            self.hack_file.write(hack_line + '\r\n')
Example #5
0
def parse_vm_file_and_append_to_asm_file(vm_file, output_file_path):
    parser = Parser(vm_file)
    code_writer = CodeWriter(output_file_path)
    while parser.has_more_commands():
        parser.advance()
        code_writer.write(parser)
    code_writer.close()
        def test():
            program = Parser("""
            let
              var max : int := 50
              var s : int := 0
              var n : int := 2
            in
              while n <= max do
                 let
                    var p : int := 1
                    var d : int := 2
                  in
                    while d <= (n - 1) do
                       let
                         var m : int := d * (n / d)
                       in
                         if n <= m then
                           p := 0;
                         d := d + 1
                       end;
                     if p <> 0 then
                       s := s + n;
                     n := n + 1
                  end;
               s
            end
            """).parse()

            environment = create_environment_with_natives(
            )  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
            result = program.evaluate(environment)
            assert isinstance(result, IntegerValue)
            return result.integer
Example #7
0
    def add_new_belief_to_belief_base(self, sentence: str):

        """
        Contains functionality for adding a new belief to the belief base.

        Parameters:
            sentence (str): Takes a string input containing propositional logic in symbolic form.

        Step 1: The sentence is parsed and an abstract syntax tree is created.
        Step 2: The abstract syntax tree is converted to CNF
        Step 3: The sentence in the abstract syntax tree is negated
        Step 4: The negated sentence is added to a copy of the belief base
        Step 5: The temp belief base where the negated sentence was added is check
                for logical entailment with the resolution method.
        Step 6: A revision of the belief base is preformed, if its required
        """

        # Step 1: The sentence is parsed and an abstract syntax tree is created.
        sentence = Parser(sentence).parse()
        print(f"Before: {sentence}")

        # Step 2: The abstract syntax tree is converted to CNF
        CNF = convert_to_cnf(sentence)
        print(f"After: {CNF}")

        # Step 3: The sentence in the abstract syntax tree is negated
        negation = Not(CNF)
        print(f"Negated: {negation}")

        self.belief_base.add_belief(negation)
        print(f"Temp belief base: {self.belief_base}")
Example #8
0
def main():
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('read_path')
    arg_parser.add_argument('write_path')
    args = arg_parser.parse_args()

    with open(args.read_path, 'r') as source:
        text = source.read()

        lexer = Lexer(text)
        tokens = lexer.lex()

        parser = Parser(tokens)
        ast = parser.parse()

        symbolizer = Symbolizer(ast)
        symbolizer.symbolize()

        optimizer = Optimizer(ast)
        optimizer.optimize()

        grapher = Grapher(ast)
        grapher.graph()

        generator = Generator(ast)
        generator.generate(args.write_path)

        runner = Runner(ast)
        runner.run()
Example #9
0
 def to_program(self, text, absolutize_lvalues=True):
     print_declaration = NativeOneArgumentFunctionDeclaration(
         'print', [FunctionParameter('message', TypeId('str'))],
         TypeId('nil'), lambda s: None)
     program = Parser(text).parse(
         [print_declaration] if absolutize_lvalues else None)
     return program
Example #10
0
    def instrument(self, contract_path, contract_name, predicates, **kargs):
        instrument_for_echidna = kargs.get('for_echidna', False)
        reuse_pre_process = kargs.get('reuse_pre_process', False)

        if reuse_pre_process:
            self.contract_path = contract_path
        else:
            self.pre_process_contract(contract_path, contract_name)

        with open(self.contract_path) as contract_file:
            self.contract_lines = contract_file.readlines()

        parser = Parser()

        for index, predicate_string in enumerate(predicates):
            predicate = parser.parse(predicate_string)

            functions_to_instrument = self.__get_functions_to_instrument(predicate)

            self.__instrument_new_variables(predicate, functions_to_instrument, instrument_for_echidna)

            if instrument_for_echidna:
                echidna_function = f'function echidna_VERIMAN_predicate_no_{str(index + 1)}() public returns(bool){{\nreturn {predicate.solidity_repr};\n}}'
                self.__insert_in_contract(echidna_function)
            else:
                assert_string = f'assert({predicate.solidity_repr}); // VERIMAN ASSERT FOR PREDICATE NO. {index + 1}'
                self.__insert_at_functions_end(functions_to_instrument, assert_string)

        with open(self.contract_path, 'w') as contract_file:
            contract_file.writelines(self.contract_lines)
 def test():
     create_environment_with_natives()
     program = Parser("let var a := 0 in (for i := 1 to 9 do a := a - i; a) end").parse()
     environment = SimpleEnvironment()
     result = program.evaluate(environment)
     assert isinstance(result, IntegerValue)
     return result.integer
Example #12
0
    def scraping(self) -> List[ParseResult]:
        response = requests.get(self.url_fqdn + self.url_path)

        parser = Parser(self.url_fqdn, response.content)
        result = parser.parse_all()

        return result
        def test():
            # adding this line creates more jitcodes in /tmp/usession-exploration-abrown/jitcodes which reduces the number of operations
            unused = Parser(
                'let var a := 0 in (while a < 100 do a := a + 1) end').parse()

            program = Let(
                declarations=[
                    VariableDeclaration(name='a',
                                        type_id=None,
                                        expression=IntegerValue(0))
                ],
                expressions=[
                    Sequence(expressions=[
                        ModifiedWhile(condition=LessThan(
                            left=LValue('a'), right=IntegerValue(100)),
                                      body=Assign(lvalue=LValue(name='a'),
                                                  expression=Add(
                                                      left=LValue(name='a'),
                                                      right=IntegerValue(1)))),
                        LValue(name='a', next_lvalue=None)
                    ])
                ])

            environment = create_environment_with_natives(
            )  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
            result = program.evaluate(environment)
            assert isinstance(result, IntegerValue)
            return result.integer
Example #14
0
    def tok_lem_stm(self):
        """ Using tokenize, lemmatize and stemming
        """
        print(sent_tokenize(Parser().final_text()))
        print(
            f'* Sum of sentences in given .txt file: {len(sent_tokenize(Parser().final_text()))}'
        )
        print(word_tokenize(Final_str().full_str))
        print(
            f'* Sum of words in given .txt file: {len(word_tokenize(Final_str().full_str))}'
        )

        lemmatizer = WordNetLemmatizer()
        lemmatized_output = ' '.join([
            lemmatizer.lemmatize(w)
            for w in word_tokenize(Final_str().full_str)
        ])
        print(f'* Lemmatization result: {lemmatized_output}')

        ps = PorterStemmer()
        words = word_tokenize(Final_str().full_str)
        stem_list = []
        for w in words:
            stem_list.append(ps.stem(w))
        print(f'* Stemming result: {stem_list}')
Example #15
0
def create_app_test():

    file_hndlrs = configure_logger()

    mediator_q = Queue()

    clients = [
        Parser(Queue(), mediator_q, config),
        BotProtocol(Queue(), mediator_q, config),
        Crawler(Queue(), mediator_q, config, 10),
        CommandMessageHandler(Queue(), mediator_q, config),
    ]

    mediator = AppMediator(mediator_q, clients)
    mediator.start()

    for client in clients:
        client.start()
    reglament_thread = Thread(target=reglament_work, args=[mediator])
    reglament_thread.start()
    try:
        while True:
            time.sleep(30)
            if not mediator.is_alive():
                logger.error('Медиатор умер, пеерзапускаю...')
                mediator = AppMediator(mediator_q, mediator.clients)
                reglament_thread = Thread(target=reglament_work,
                                          args=[mediator])
                reglament_thread.start()
                mediator.start()
            mediator.check_clients()
    finally:
        for file_hndl in file_hndlrs:
            file_hndl.close()
Example #16
0
def main():
    working_dir = get_working_directory()
    py_files = find_all_py_files_in(working_dir)

    ex = {
        f.module: Parser(f.module, f.lines()).extract()
        for f in py_files if not f.module.startswith('tests')
    }
    modules = list(ex.keys())

    # "ROUNDED" GRAPH
    discards = set([])
    for module, imports in ex.items():
        cleaned = set([])
        for imp in imports:
            if imp in modules:
                cleaned.add(imp)
            else:
                pre = longest_common_prefix(imp, modules)
                if pre and pre not in modules:
                    pre = pre + '.__init__'
                if pre not in modules:
                    discards.add(imp)
                else:
                    cleaned.add(pre)
        ex[module] = cleaned

    with open('asdf.gv', 'w') as f:
        f.write('digraph {\n')
        for module, imports in ex.items():
            for imp in imports:
                f.write('    "{}" -> "{}";\n'.format(module, imp))
        f.write('}\n')

    pprint.pprint(discards)
 def test_assignment(self):
     # x := 5\ny := not b + 7\ny := 'asd' # '123'
     tokens = [
         MyToken(TokenType.IDENT, 'x', 'x', 1),
         MyToken(TokenType.ASSIGN, ':=', None, 1),
         MyToken(TokenType.NUMBER, '5', 5, 1),
         MyToken(TokenType.EOL, 'None', None, 1),
         MyToken(TokenType.IDENT, 'y', 'y', 2),
         MyToken(TokenType.ASSIGN, ':=', None, 2),
         MyToken(TokenType.NOT, 'not', None, 2),
         MyToken(TokenType.IDENT, 'b', 'b', 2),
         MyToken(TokenType.PLUS, '+', None, 2),
         MyToken(TokenType.NUMBER, '7', 7, 2),
         MyToken(TokenType.EOL, 'None', None, 2),
         MyToken(TokenType.IDENT, 'y', 'y', 3),
         MyToken(TokenType.ASSIGN, ':=', None, 3),
         MyToken(TokenType.STRING, 'asd', 'asd', 3),
         MyToken(TokenType.HASH, '#', None, 3),
         MyToken(TokenType.STRING, '123', '123', 3)
     ]
     parser = Parser(tokens)
     tree = \
     Program([Assign(Identifier('x'), Literal(5)),
      Assign(Identifier('y'), Binary(LogicalUnary(TokenType.NOT, Identifier('b')), TokenType.PLUS, Literal(7))),
      Assign(Identifier('y'), StringBinary(Literal('asd'), TokenType.HASH, Literal('123')))])
     self.assertEqual(tree, parser.parse())
Example #18
0
def main():
    """
    A method which is the main entry point for the CLI.

    This method calls the Parser() class to parse command line arguments and \
    then instantiates the SyncEnd() class with input arguments.
    It then calls the start() method which is the interface of \
    the CLI with Postman and Slack.
    """

    # get the arguments from command line
    parser = Parser()
    (
        collection_name,
        api_key,
        trigger_interval,
        slack_channel,
        slack_token,
    ) = parser.get_arguments()
    sync_end = SyncEnd(api_key, collection_name, trigger_interval,
                       slack_channel, slack_token)

    try:
        sync_end.start()
    except Exception as e:
        print(e)
Example #19
0
class TestParser:
    PARSER = Parser()

    # TEST UNITAIRE

    def test_remove_case_sensitive(self):
        assert (self.PARSER.remove_case_sensitive(
            "Emmène-moi à Moomin World ?") == "emmène-moi à moomin world ?")

    def test_remove_accents(self):
        assert (self.PARSER.remove_accents("Emmène-moi à Moomin World ?") ==
                "Emmene-moi a Moomin World ?")

    def test_remove_ponctuation(self):
        assert (self.PARSER.remove_ponctuation(
            "Bonjour, emmène-moi à Moomin World ?") ==
                "Bonjour emmène moi à Moomin World")

    def test_remove_stop_words(self):
        assert (self.PARSER.remove_stop_words("aux Pays des Moomins") ==
                "Pays Moomins")

    # TEST D'INTEGRATION

    def test_process_question(self):
        sentence = "Où est Moomin World ? Hein? Alors, je t'écoute !"
        assert self.PARSER.process_question(sentence) == "moomin world tecoute"
Example #20
0
def main(argv):
    """Parse and print any Tiger program"""

    # check for arguments
    try:
        file = argv[1]
    except IndexError:
        print(
            "Expected one file name argument to be passed, e.g. ./tiger-parser program.tig"
        )
        return 40

    program_contents = read_file(argv[1])

    # parse input program
    try:
        program = Parser(program_contents, argv[1]).parse()
    except ParseError as e:
        print("Parse failure: %s" % e.to_string())
        return 42

    # print the program
    print(program.to_string())

    return 0
Example #21
0
    def test_generator(self):
        for path in glob.glob("test/grader/*/src.pas"):
            dir = os.path.dirname(path)
            should_fail = not dir.endswith('16')
            with open(path, 'r') as source:
                print(f"testing {path}")
                text = source.read()
                lexer = Lexer(text)
                tokens = lexer.lex()
                parser = Parser(tokens)
                ast = parser.parse()
                symbolizer = Symbolizer(ast)
                symbolizer.symbolize()
                grapher = Generator(ast, symbolizer)
                grapher.generate()
                sol = os.path.join(dir, 'src.c')
                out = os.path.join(dir, 'out')
                if os.path.exists(sol):
                    os.remove(sol)
                if os.path.exists(out):
                    os.remove(out)
                grapher.write(sol)
                p = None
                try:
                    p = sp.Popen(['gcc', sol, '-o', out], stdout=sp.PIPE)
                    retCode = p.wait()
                    self.assertTrue(retCode == 0)
                    p.stdout.close()
                    #s = str(p.stdout.read())
                    #self.assertTrue(s == '')
                except Exception:
                    self.assertFalse(should_fail)
                for i in range(1, 5):
                    inFile = os.path.join(dir, str(i) + '.in')
                    outFile = os.path.join(dir, str(i) + '.out')
                    with open(inFile, 'r') as inText:
                        with open(outFile, 'r') as outText:
                            inText = inText.read()
                            outText = outText.read()
                            try:
                                of = sp.Popen([out],
                                              stdin=sp.PIPE,
                                              stdout=sp.PIPE)
                                of.stdin.write(inText.encode('utf-8'))
                                of.stdin.close()
                                rc = of.wait()
                                self.assertTrue(rc == 0)
                                b = of.stdout.read()
                                s = b.decode('utf-8')
                                of.stdout.close()
                                if (not should_fail):
                                    self.assertEqual(s, str(outText))
                            except Exception:
                                self.assertFalse(should_fail)

        self.assertTrue(True)


#Tests().test_grapher()
Example #22
0
 def run(self):
     """
     Запускает парсер и музыку
     """
     parser = Parser(self.file)
     self.track_name = os.path.basename(self.file)
     self.play(self.file)
     self.words_with_timings, self.sentences = parser.parse()
Example #23
0
    def __init__(self, path):
        archive = open(path, 'r').read()

        lex = Lexer(archive)
        tokens = lex.tokenize()
        parser = Parser(tokens)

        parser.parse()
Example #24
0
 def __init__(self, text):
     """
     :param text: client input
     """
     self.text = text
     self.tokenizer = Tokenizer(self.text)
     self.parser = Parser(self.tokenizer.create_tokens())
     self.GLOBAL_VARS = dict()
Example #25
0
    def run(self, parser=2, downloader=2):
        self._logger.info('이미지 다운로드 작업 시작')
        start = time.time()

        # 멀티 프로세싱 처리를 위한 매니저
        with Manager() as manager:
            # 프로세스 목록
            processes = []

            # 공유 메모리 변수
            content_list = manager.list()
            image_list = manager.list()
            count = manager.Value('i', 0)
            lock = manager.Lock()
            feeder_running = manager.Value('i', 1)
            parser_running = manager.Value('i', 1)

            parser_logger = Logger('cybot_parser.log')
            downloader_logger = Logger('cybot_downloader.log')
            main_cookies = self._driver.get_cookies()
            cookie = []

            for c in main_cookies:
                cookie.append({'name': c['name'], 'value': c['value']})

            # 파서 프로세스 생성 및 시작
            for idx in range(parser):
                parser_instance = Parser(self._chromedriver, cookie,
                                         parser_logger, self._delay)
                parser_process = Process(target=parser_instance.parse, \
                    args=(content_list, image_list, feeder_running, parser_running)
                )
                parser_process.name = 'Parser::' + str(idx)
                parser_process.start()
                processes.append(parser_process)
                self._logger.info('Parser', str(idx), '프로세스 시작')

            # 다운로더 프로세스 생성 및 시작
            for idx in range(downloader):
                downloader_instance = Downloader(downloader_logger)
                downloader_process = Process(target=downloader_instance.downloader, \
                    args=(image_list, count, lock, parser_running))
                downloader_process.name = 'Downloader::' + str(idx)
                downloader_process.start()
                processes.append(downloader_process)
                self._logger.info('Downloader', str(idx), '프로세스 시작')

            # 피더 프로세스 시작
            self._logger.info('Feeder 시작')
            self.feeder(content_list, feeder_running)

            # 파서, 다운로더 프로세스가 종료되지않은 경우 대기
            for p in processes:
                p.join()

            self._logger.info('작업 소요시간: {}초'.format(
                round(time.time() - start, 2)))
            self._logger.info('전체 이미지 수: {}'.format(count.value))
 def test():
     program = Parser(
         'let var a := 0 in (while a < 100 do a := a + 1; a) end'
     ).parse(create_native_functions())
     environment = create_environment_with_natives(
     )  # apparently RPython barfs if we just use Environment() here because NativeFunctionDeclaration.__init__ is never called so the flowspace does not know about the 'function' field
     result = program.evaluate(environment)
     assert isinstance(result, IntegerValue)
     return result.integer
Example #27
0
 def test_make_first_set_01(self):
     # example from slide
     parser = Parser('data/book_glc_01', True)
     right_result = {
         'S': {'a', 'b', 'c', 'd'},
         'B': {'a', 'b', 'd', '&'},
         'A': {'a', '&'}
     }
     self.assertDictEqual(parser.first_sets, right_result)
Example #28
0
 def __init__(self, path, file):
     info = Parser(path, file)
     self.book_name = info.book_name()
     self.number_of_paragraphs = info.number_of_paragraph()
     self.number_of_words = info.number_of_words()
     self.number_of_letters = info.number_of_letters()
     self.words_with_capital_letters = info.words_with_capital_letters()
     self.words_in_lowercase = info.words_in_lowercase()
     self.second_table = info.second_table()
Example #29
0
 def test_handles_no_matches(self):
     p = Parser()
     m = Meme("all your * -base")
     s = [
         Source("", "foo", ""),
         Source("", "bar", ""),
         Source("", "baz", "")
     ]
     self.assertEqual(p.collate_words(m, s).get_list(), [])
Example #30
0
 def test_ast_from_tokens(self):
     expect(
         Parser([
             ("list_start", "("),
             ("symbol", "list"),
             ("integer", "1"),
             ("integer", "9"),
             ("list_end", ")"),
         ]).run()[0]) == ["list", 1, 9]