def run_benchmarks(): # type: () -> None n = 200000 result = 0 i = 0 while i < n: lex = Lexer('a*b*3*4') p = Parser(lex) tree = p.Parse() i += 1 log('result = %d', result) log('iterations = %d', n)
def run_tests(): # type: () -> None lex = Lexer('abc') while True: tok_type, tok_val = lex.Read() if tok_type == tok_e.Eof: break #print('%s %s' % (tok_type, tok_val)) log('tok_val %s', tok_val) CASES = [ '1+2', '1+2*3', '1*2+3', '(1+2)*3', 'a+b+c+d', 'a*b*3*4', # expect errors here: '(', ')', '(a+b', ' ', ' $$ ', ] for expr in CASES: lex = Lexer(expr) p = Parser(lex) log('') log('--') log('%s =>', expr) tree = None # type: Optional[expr_t] try: tree = p.Parse() except ParseError as e: log('Parse error: %s', e.msg) continue #log('%s', tree) htree = tree.AbbreviatedTree() ast_f = fmt.AnsiOutput(mylib.Stdout()) fmt.PrintTree(htree, ast_f) ast_f.write('\n')
def ParseDemo(oil_grammar): # type: (Grammar) -> None arena = alloc.Arena() arena.PushSource(source__Stdin('')) parse_ctx = None # type: ParseContext e_parser = expr_parse.ExprParser(parse_ctx, oil_grammar) line_lexer = lexer.LineLexer('', arena) line_reader = reader.StringLineReader('1 + 2*3', arena) lex = lexer.Lexer(line_lexer, line_reader) try: pnode, _ = e_parser.Parse(lex, arith_nt.arith_expr) except error.Parse as e: #ui.PrettyPrintError(e, arena) log("Parse Error (TODO: print it)") return # TODO: Fill this in. Oil uses parse_lib.MakeGrammarNames() # # terminals: _Id_str? Doesn't work in mycpp # nonterminals: gr.number2symbol. Is this ever used at runtime? # # Dict[int,str] should really be a List[str] then? if 0: names = {} # type: Dict[int, str] printer = expr_parse.ParseTreePrinter(names) printer.Print(pnode) # NOTE: Could also transform # This only works for Oil if 0: tr = expr_to_ast.Transformer(oil_grammar) node = tr.Expr(pnode) assert node is not None tree = node.AbbreviatedTree() fmt.PrintTree(tree, mylib.Stdout())
def run_tests(): # type: () -> None lex = Lexer('abc') while True: tok_type, tok_val = lex.Read() if tok_type == tok_e.Eof: break #print('%s %s' % (tok_type, tok_val)) log('tok_val %s', tok_val) CASES = [ '1+2', '1+2*3', '1*2+3', '(1+2)*3', 'a+b+c+d', 'a*b*3*4', '1', 'a', # expect errors here: '(', ')', '(a+b', ' ', ' $$ ', ] for expr in CASES: lex = Lexer(expr) p = Parser(lex) log('') log('--') log('%s =>', expr) node = None # type: Optional[expr_t] try: node = p.Parse() except ParseError as e: log('Parse error: %s', e.msg) continue #log('%s', tree) htree = node.AbbreviatedTree() ast_f = fmt.AnsiOutput(mylib.Stdout()) fmt.PrintTree(htree, ast_f) ast_f.write('\n') UP_node = node with tagswitch(UP_node) as case: if case(expr_e.Const): node = cast(expr__Const, UP_node) log('Const %d', node.i) elif case(expr_e.Var): node = cast(expr__Var, UP_node) log('Var %s', node.name) else: log('Other')
node = cast(expr__Var, UP_node) log('Var %s', node.name) else: log('Other') def run_benchmarks(): # type: () -> None n = 200000 result = 0 i = 0 while i < n: lex = Lexer('a*b*3*4') p = Parser(lex) tree = p.Parse() i += 1 log('result = %d', result) log('iterations = %d', n) if __name__ == '__main__': if os.getenv('BENCHMARK'): log('Benchmarking...') run_benchmarks() else: run_tests()