def calc_values(): print "calc_values()" ''' pool = Pool() pool.map(exec_bash, g_queue) pool.close() pool.join() ''' global g_queue i = g_queue.popleft() try: while i is not None: cmd = "value=%s" % i res = parser.parse(cmd) cmd = judge res = parser.parse(cmd) #cmd = "value=%s\n%s" % (i, judge) #res = parser.parse(cmd) #cmd = "bash -c 'export value=%s;%s'" % (i, judge) #print cmd #status, output = commands.getstatusoutput(cmd) #print "i=%d, status=%s" % (i, res) i = g_queue.popleft() except IndexError: print time.time() gevent.sleep(0)
def feed(sentence, response, weight): parser = stat_parser.Parser() sent_tree = parser.parse(sentence) resp_tree = parser.parse(response) # Index nodes of sentence. feedSent_recursive(sent_tree)
def main(argv): filename = sys.argv[1] file = open(filename) data = file.read() file.close() print(data) parser.parse(data, tracking = True)
def test_parse(): from parser import parser, Token print parser.parse([ Token('-'), Token('parsedexpr', Expression('Pi', Symbol('Hey'))), Token('+'), Token('int', 2) ])
def compile(file): '''Parses input file and compiles data''' success = True f = open(file, 'r') s = f.read() parser.parse(s) if success == True: print('Compiling successful!') gen_obj() test_log(file) else: print('Compile error')
def main(): if len(sys.argv) != 2: print 'Invalid number of arguments: <mongoose> <mongoose src code>' exit(1) src = open(sys.argv[1]) backend.walk_ast(parser.parse(src.read())) backend.run()
def generate(input_str): ''' Parses an input string and returns another one containing the generated program skeleton. ''' HEADER, L, ENDCODE = parser.parse(input_str) result = 'from skel import Grammar\n' if HEADER is not None: result += HEADER + '\n' result = result + """ def generate(self): """ result = '' grammar = Grammar(Parser.START_SYMBOL) if L: for T in L: grammar.addRule(T) result += grammar.generate(Parser.START_SYMBOL) if ENDCODE is not None: result += ENDCODE + '\n' return result
def main(): args = get_args() formula = input() syntax_tree = parser.parse(formula) cnf = transform_to_cnf(syntax_tree, debug=args.debug) answer = 'Yes' if resolution_method(cnf) else 'No' print(f'Q: Is the given formula satisfiable?\nA: {answer}')
def run(fname): with open(fname, "r") as f: source_code = f.read() # Remove shebang line if it exists. if source_code[0:2] == "#!": source_code = "\n".join(source_code.split("\n")[1:]) ctx = Context() parser.parse(lexer.lex(source_code)).compile(ctx) bytecode = ctx.create_bytecode() frame = Frame(bytecode) interpreter = Interpreter() interpreter.interpret(bytecode, frame)
def main(): if len(argv) > 1: #get command line arguements switches = {'-o':'a.xml'} setswitches,args = gnu_getopt(argv[1:],':o:') setswitches = dict(setswitches) for i in setswitches.keys(): switches[i] = setswitches[i] filename = args[0] outfile = switches['-o'] f = open(filename, "r") #for line in f: data = f.read() f.close() y = parser.parse(data) ASTprint(stdout,y) ter = print_errors(terrors,data,filename) per = print_errors(perrors,data,filename) if ter and per: f = open(outfile,"w") f.write(repr(y)) #f.write(sreplace(repr(y),'None','')) f.close() else: print "No input given" exit(-1)
def __init__(self, _rule_string): self.string_representation = _rule_string self.parsed = parser.parse(_rule_string) self.bodyRetSttt = SubstituteTable() self.body = self.parsed["body"] self.head = self.parsed["head"] self.predicate_to_pos_node_idx, self.predicate_to_neg_node_idx = self.index_leafnodes() self.negatedAtoms = self.getNegatedAtoms() self.hasNegatedAtom = (len(self.negatedAtoms) > 0) self.predicate_to_node_idx = dict() self.lastSuccessfulJoin = -1 self.var2atoms = self.createVarToAtomMap() # ----------------------------------------- #self.joinFunc = self.hashJoin # Join function self.joinFunc = self.fastHashJoin # Join function # ----------------------------------------- for pred, leaves in self.predicate_to_pos_node_idx.items(): if pred not in self.predicate_to_node_idx: self.predicate_to_node_idx[pred] = leaves else: self.predicate_to_node_idx[pred] += leaves for pred, leaves in self.predicate_to_neg_node_idx.items(): if pred not in self.predicate_to_node_idx: self.predicate_to_node_idx[pred] = leaves else: self.predicate_to_node_idx[pred] += leaves
def begin(argv): if len(argv) > 1: with open(argv[1], 'r') as f: result = parser.parse(lexer.lex(f.read())) os.write(1, result.eval()) else: os.write(1, "Please provide a filename.")
def parse_from_file(self): """ Read todos from file. return the tasks of "todo.txt". """ content = open(self.file_path()).read() return parser.parse(content)
def build(code, functions = {}): if not isinstance(functions, dict): functions = dict([(f.__name__, f) for f in functions]) strategy = parser.parse(code) standard = { "print": _print } strategy.dfs(linker(dict(standard.items() + functions.items()))) strategy.dfs(triggers_creator_factory(strategy)) return strategy
def build(code, functions={}): if not isinstance(functions, dict): functions = dict([(f.__name__, f) for f in functions]) strategy = parser.parse(code) standard = {"print": _print} strategy.dfs(linker(dict(standard.items() + functions.items()))) strategy.dfs(triggers_creator_factory(strategy)) return strategy
def from_file(cls, file): config = None env = {} with open(file) as f: inp = f.read() cn = parser.parse(inp, lexer=lexer) config = cn.evaluate(env) return config
def parse_range(start_id, increments): for id in range(start_id, start_id + increments): data = parser.parse(id) responce = api.put(id, float(data['money']['amount']), data['money']['currency'], data['company'], data['store_page']) print(responce) time.sleep(10)
def execute_program(filename): try: file = open('./test_cases/' + filename, 'r') code = file.read() tree = parser.parse(code) tree.visit() except (VariableTypeError, VariableNotDefinedError, RedeclarationError, DivisionByZeroError) as error: print(error)
def test_np_chunk(self): sentence_3 = 'Holmes lit a pipe.' proc_sent = preprocess(sentence_3) trees = list(parser.parse(proc_sent)) chunks = [] for tree in trees: for np in np_chunk(tree): chunk = " ".join(np.flatten()) chunks.append(chunk) self.assertEqual(chunks, ['holmes', 'a pipe'])
def testParser(parser, read_data): logging.basicConfig( level=logging.DEBUG, filename="parselog.txt", filemode="w", format="%(filename)10s:%(lineno)4d:%(message)s" ) log = logging.getLogger() result = parser.parse(read_data, tracking=True, debug=log) return result
def build_sequence(pattern): root = parser.parse(pattern, debug=log, lexer=lexer) traversal = postorder(root) paths = build_expression(traversal) sequence = phrase(paths) conjunctions = ['and', 'plus', 'followed by'] sent = '' for token in sequence[:-1]: sent += token + ' ' + choice(conjunctions) + ' ' sent += ' ' + sequence[-1] return sent
def main(): fileName = sys.argv[1] f = open(fileName, 'rb') # line = f.read() # result = parser.parse(line) # print(result) lines = f.readlines() for line in lines: if line[0] != '#' and line[0] != '\n': result = parser.parse(line) walk_tree(result, env)
def __init__(self, image_type, count, verbose=False): self.image_type = image_type self.count = count self.verbose = verbose self.run_dict, self.directive_dict = parser.parse(self.image_type, self.count, verbose) self.software = set() self.software_list = [] self.software_map = {} self.idf_map = {} self.score_map = {}
def test_parser(url, status_code, elapsed, text, patterns, result): mock_context = MagicMock() mock_response = Mock(spec=Response) mock_response.elapsed = timedelta(microseconds=elapsed) mock_response.text = text mock_response.status_code = status_code mock_context.__enter__.return_value = mock_response with patch("requests.get", return_value=mock_context): result_status_code, result_elapsed, result_text = parse(url, patterns) assert result_status_code == status_code assert result_elapsed == elapsed assert result_text == result
def repl(): while True: source = read_input() try: ast = parser.parse(source) if ast is None: continue value = ast.execute(global_env) except Exception: print traceback.format_exc() else: if value is not None: print value
def get_game_by_id(id): try: #Validates the game id id = int(id) if id < 0: raise ValueError game_key = parser.build_game_key(id) response_data = parser.parse(app.config[GAME_PATH_CONFIG])[game_key] except (ValueError): raise exception.BadParam(exception_messages.BAD_PARAMETER) except (KeyError): raise exception.ResourceNotFound( exception_messages.GAME_NOT_FOUND_MESSAGE) return jsonify(response_data)
def main(): print 'Enter a tiny expression.' try: s = raw_input('-> ').strip() except EOFError: sys.exit() if s: exp = parser.parse(s) try: print 'Evaluating ' + exp.to_s() print exp.eval() except ZeroDivisionError, e: print e
def main() : print 'Enter a tiny expression.' try : s = raw_input('-> ').strip() except EOFError : sys.exit() if s : exp = parser.parse(s) try : print 'Evaluating ' + exp.to_s() print exp.eval() except ZeroDivisionError, e : print e
def checker(url: str, patterns: List[str]): """ periodic task :param url: target url :param patterns: list of regex """ logging.info('start checker') status_code, elapsed, results = parse(url, patterns) logging.info( f'checker results for {url} ({patterns}) status_code: {status_code}, elapsed: {elapsed}, results: {results}.' .format(url=url, patterns=patterns, status_code=status_code, elapsed=elapsed, results=results)) producer = CheckerProducer(kafka_producer_config()) logging.info('send message') producer.send_message(status_code, elapsed, results) logging.info('message have been sent')
def test_square(self): actual = re.sub(r'\s', '', parse(sqr), flags=re.MULTILINE) exp = re.sub(r'\s', '', sqr_tree, flags=re.MULTILINE) from lxml import etree actual = etree.fromstring(actual) exp = etree.fromstring(exp) actual = etree.tostring(actual, pretty_print=True).decode() exp = etree.tostring(exp, pretty_print=True).decode() import pdb pdb.set_trace() ad = actual.split('\n') ed = exp.split('\n') from difflib import unified_diff as ud diff = '\n'.join(ud(ad, ed)) import pdb pdb.set_trace() self.assertEqual(exp, actual)
def main(): try: input_file = sys.argv[1] output_file = sys.argv[2] except Exception: print('Nieprawidłowa liczba parametrów wejściowych.') exit() with open(input_file, 'r') as f: input_data = f.read() try: result = parser.parse(input_data, tracking=True) result = process(result, memory_manager) except Exception as e: print(e) exit() with open(output_file, 'w') as f: f.write('\n'.join(result))
def run_interpreter(): while True: code = input('yapl >>> ') if (code == 'quit' or code == 'exit'): break try: tree = parser.parse(code) print(tree.visit()) except VariableTypeError as error: print(error) except VariableNotDefinedError as error: print(error) except RedeclarationError as error: print(error) except DivisionByZeroError as error: print(error) except KeyboardInterrupt: print("Quitting...") exit(1) except Exception: print()
def main(): if(len(sys.argv) == 2): try: f = open(sys.argv[1], "r") except: print("File not found: " + sys.argv[1]) return -1 if f.mode == 'r': data = f.read() f.close() else: print("Error: File " + sys.argv[1] + " is not redable") return -1 else: print("Usage: " + sys.argv[0] + " file") return -1 # Dar el input al lexer lexer.input(data) # Tokenize # while True: # tok = lexer.token() # if not tok: # break # No more input # print(tok.type, tok.value, tok.lineno, tok.lexpos) result = parser.parse(data) debugging(result) if p_error: sys.exit(1) else: print("Succesful compilation, passing to execution.") sys.exit(0)
def main(filename, opt_flag, code_gen, o): # open bf code print(opt_flag) with open(filename, 'r') as f: bf = f.read() # parser parsed = parser.parse(bf) # optimizer print("## 4PASS ###") opt = K4.lift_convert(parsed) print(opt) if opt_flag == 'hard': print("### 6PASS ###") opt = K6.lift_convert(opt) print(opt) # code generation print("### CODEGEN ###") if code_gen == 'py': py_code = "\n".join(py_gen(opt)) with open(f"{o}.py", "w+") as f: f.write(py_code) elif code_gen == 'c': c_code = '\n'.join(c_gen(opt)) with open(f"{o}.c", "w+") as f: f.write(c_code) run_cmd(f'gcc -O2 {o}.c -o {o}') else: Exception("Illegal codegen type")
def test_parse(): from parser import parser, Token print parser.parse([Token('-'), Token('parsedexpr', Expression('Pi', Symbol('Hey'))), Token('+'), Token('int', 2)])
if args.out is None: ofile = args.input if args.input is not None else "myAST" ofile = ofile.split('/')[-1].split('.')[0] args.out = ofile + '.dot' png_file = args.out.split('.')[-2] + '.png' with open(args.input, 'r') as f: arg_file = f.read() ifile = stdlib + arg_file lexer.filename = args.input lexer.lines = ifile.split("\n") syntax_tree = parser.parse(ifile) if syntax_tree is None or parser.compilation_err: exit(1) graph = pydot.Dot('gcc_lite: Abstract Syntax Tree', graph_type='digraph') syntax_tree.gen() symtable.dump_csv(args.out.split('.')[-2] + '.csv') tac.dump_code(args.out.split('.')[-2] + '.out') asm = AssemblyGen(tac.func_code) asm.gen_assembly() asm.dump_code(args.out.split('.')[-2] + '.s') AST = syntax_tree.gen_dot(graph)
def parse_check(self, str, expected): self.assertEqual(parse(str).accept(Evaluator()), expected)
rules.append("delete(and(%s), atom(X)) :- rule(and(%s), atom(X))." % (",".join(["atom(X)" for j in range(1, i + 1)]), ",".join( ["atom(X)" for j in range(1, i + 1)]))) for conn in ["min", "max", "and", "or"]: for j in range(1, i + 1): rules.append("expression(X%d) :- expression(%s(X%s))." % (j, conn, vars)) rules.append("#show rule(X,Y) : rule(X,Y), not delete(X,Y).") rules.append("#show integer/1.") rules.append("\n%%%%%%%%%%%%%%%%\n% user program\n") for file in args.files: with open(file, encoding='utf-8') as f: for line in f: rules.append(parser.parse(line)) if args.print_grounder_input: print("<grounder-input>") print("\n".join(rules)) print("</grounder-input>") sys.stdout.flush() tmpFile = tempfile.NamedTemporaryFile() tmpFile.write("\n".join(rules).encode()) tmpFile.flush() cmd = [args.grounder] cmd.extend(args.grounder_args) cmd.append(tmpFile.name) gringo = subprocess.Popen(cmd, stdout=subprocess.PIPE,
from parser import parser import testcases from environment import Environment import builtin for case, src in { k: v for k, v in testcases.__dict__.items() if not k.startswith('__') }.items(): print '===', case, '===' ast = parser.parse(src) #print ast ast.execute(Environment(builtin.built_ins)) print
rules.append("integer(atom(X)) :- rule(atom(X), or(%s))." % (",".join(["atom(X)" for j in range(1,i+1)]),)) rules.append("delete(atom(X), or(%s)) :- rule(atom(X), or(%s))." % (",".join(["atom(X)" for j in range(1,i+1)]), ",".join(["atom(X)" for j in range(1,i+1)]))) rules.append("integer(atom(X)) :- rule(and(%s), atom(X))." % (",".join(["atom(X)" for j in range(1,i+1)]),)) rules.append("delete(and(%s), atom(X)) :- rule(and(%s), atom(X))." % (",".join(["atom(X)" for j in range(1,i+1)]), ",".join(["atom(X)" for j in range(1,i+1)]))) for conn in ["min", "max", "and", "or"]: for j in range(1,i+1): rules.append("expression(X%d) :- expression(%s(X%s))." % (j, conn, vars)) rules.append("#show rule(X,Y) : rule(X,Y), not delete(X,Y).") rules.append("#show integer/1.") rules.append("\n%%%%%%%%%%%%%%%%\n% user program\n") for file in args.files: with open(file, encoding='utf-8') as f: for line in f: rules.append(parser.parse(line)) if args.print_grounder_input: print("<grounder-input>") print("\n".join(rules)) print("</grounder-input>") sys.stdout.flush() tmpFile = tempfile.NamedTemporaryFile() tmpFile.write("\n".join(rules).encode()) tmpFile.flush() cmd = [args.grounder] cmd.extend(args.grounder_args) cmd.append(tmpFile.name) gringo = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) [stdout, stderr] = gringo.communicate()
def __init__(self): self.todo_txt = t = TodoTxt() # <TodoTxt instance> self.todo_content = c = t.read() # todo.txt's content self.todo = parser.parse(c) # <Todo instance>
def parse_dump(self, text): tree = parse(text) return tree.accept(Dumper(semantics=False))
#!/usr/bin/env python # ----------------------------------------------------------------------------- # File: $file: kwstyle.py$ # Author: $author: adrien.bailly$ # Date: $date: 3 dec 2009$ # Revision: $revision: 1$ # Description: PHP coding style validator # ----------------------------------------------------------------------------- import sys sys.path.insert(0,"../..") if sys.version_info[0] >= 3: raw_input = input import ply.lex as lex import ply.yacc as yacc import os from parser import parser f = open("../../tests/php/parser/test.php", 'r') result = parser.parse(f.read()) print result
#!/usr/bin/env python # ----------------------------------------------------------------------------- # File: $file: kwstyle.py$ # Author: $author: adrien.bailly$ # Date: $date: 3 dec 2009$ # Revision: $revision: 1$ # Description: PHP coding style validator # ----------------------------------------------------------------------------- import sys sys.path.insert(0,"../..") if sys.version_info[0] >= 3: raw_input = input import ply.lex as lex import ply.yacc as yacc import os from parser import parser f = open("test/expression", 'r') parser.parse(f.read())
import argparse import sys import os import importlib from parser import parser parser.parse(sys.argv[1], sys.argv[2])
contenido = archivo_actual.read() regex = r'(\{%(?:=)?(?:.)*?%\})' regex = re.compile(regex,re.DOTALL) objeto = re.findall(regex,contenido) if objeto: shiny = objeto archivo_actual.close() return shiny #-------------------------------------Main------------------------------------------# if(len(sys.argv) > 1): if os.path.exists(sys.argv[1]): print "Archivo de Entrada.." representacion(sys.argv[1]) print "Tags" marcasShiny = tagsForParse(sys.argv[1]) print marcasShiny print "# Marcas ShinyEL = " + str(len(marcasShiny)) + "\n" i = 0 for element in marcasShiny: i = i + 1 result = parser.parse(element) #print result if result: nx.draw_graphviz(result) plt.savefig("grafo.png") else: print "El archivo de entrada NO existe" else: print "File Missing ... Exit"
# /bin/env python import sys from lexer import lexer from parser import parser import textools as masters from pdb import set_trace md_name = 'test.md' #sys.argv[-2] #templates = sys.argv[-1] #templates = templates.strip('.py') #module = __import__(templates) if __name__ == '__main__': md_file = open(md_name).read() lexer.input(md_file) parse_tree = parser.parse(lexer=lexer) tex_name = md_name.replace('md', 'tex') with open(tex_name,'w') as output: output.write(masters.header) for slide in parse_tree: if hasattr(masters, slide.master): int_slide = hasattr(masters, slide.master)(slide) output.write(int_slide) else: raise AttributeError('No master slide named %s available in the theme collection' % slide.master)
def test_invalid_programs(self): for invalid_program in listdir(INVALID_PROGRAMS_PATH): with self.assertRaises(ParserException): with open(INVALID_PROGRAMS_PATH + invalid_program) as file: parser.parse(file.read())
print " " * indent + root.__str__() traversePost(root.expression, indent + 1) traversePost(root.statements, indent + 1) traversePost(root.next_conditional, indent + 1) elif root.vtype == v.AGENT: print " " * indent + root.__str__() traversePost(root.create, indent + 1) traversePost(root.action, indent + 1) traversePost(root.destroy, indent + 1) for st in root.statements: traversePost(st, indent + 1) else: # regular Node print " " * indent + root.__str__() # vtype,':',root.syn_value,':',root.symbol,':',root.inh_value,':',root.params if root.children is not None: for n in root.children: traversePost(n, indent + 1) if __name__ == "__main__": # while True: # try: # s = raw_input('y>> ') s = open(sys.argv[1], "r") # except EOFError: # break # if not s: continue result = parser.parse(s.read()) print "result is of type", type(result) traverse(result) print "done"
def get(self, answer=None): # add answer to figure which approach to go """ call this method to get a gist_id:: gist_id = GistId().get() what the get() dose: 1) read the gist_id from file "~/.todo/gist_id" 2) check if the gist_id if empty (yes)-> 1) if the gist_id is empty, ask user to input it or new a gist directly. 2) save the new gist_id 3) return the gist_id """ if self.is_empty: print "Tell me the gist's id you want to push to:" print " 1. New a gist right now." print " 2. Let me input a gist's id." if answer is None: answer = ask_input.text("Input your answer(1/2):") if answer == '2': self.save(ask_input.text("Gist id:")) elif answer == '1': # new a gist todo_content = TodoTxt().read() todo = parser.parse(todo_content) if not todo.name: name = "Todo" else: name = todo.name files = { name: { "content": todo_content } } resp = None github = Github() token = GithubToken().get() github.login(token) # need to login log.info("Create a new gist..") resp = github.create_gist(files=files, description="Todo") if resp.status_code == 201: dct = resp.json() html_url = dct["html_url"].encode("utf8") log.ok("Create success:%s ," "pushed at file '%s'" % (html_url, name)) self.save(dct["id"]) sys.exit() elif resp.status_code == 401: log.warning("Github access denied, empty the old token") GithubToken().save('') # empty the token! # and re create self.get(answer='1') else: log.error("Create gist failed. %d" % resp.status_code) else: # exit if else input log.error("Invalid answer.") return self.content
def parse_binder(self, text): tree = parse(text) return tree.accept(Binder())
def assemble(source): '''Assemble a infasm source file''' asmfile = parser.parse(source, lexer = lexer) gen = zmachine(asmfile) return gen