def main(): # parse an input string by parser with grammar loaded. # REDUCE event is a good starting point if you want to handle. g = pyauparser.Grammar.load_file("data/operator.egt") p = pyauparser.Parser(g) p.load_file("data/operator_sample_1.txt") #p.trim_reduction = True while True: ret = p.parse_step() if ret == pyauparser.ParseResultType.ACCEPT: print("[Accept]") break elif ret == pyauparser.ParseResultType.SHIFT: token = p.token print("[Shift] {0} '{1}' ({2}:{3})".format(token.symbol.name, token.lexeme, token.position[0], token.position[1])) elif ret == pyauparser.ParseResultType.REDUCE: print("[Reduce] {0}".format(p.reduction.production)) elif ret == pyauparser.ParseResultType.REDUCE_ELIMINATED: print("[ReduceEliminated]") elif ret == pyauparser.ParseResultType.ERROR: print("[Error] {0}".format(p.error_info)) return print("done", p.position)
def parse_and_write_dot_png(g, src_path, dot_path, png_path, trim_reduction=False, mark_id=False, simplified=False): p = pyauparser.Parser(g) p.trim_reduction = trim_reduction p.load_file(src_path) if simplified: builder = pyauparser.SimplifiedTreeBuilder() else: builder = pyauparser.TreeBuilder() if mark_id: reduce_id = [0] def mark_id_handler(handler): def sub_handler(ret, p): handler(ret, p) if ret == pyauparser.ParseResultType.REDUCE: reduce_id[0] += 1 p.reduction.head.data.id = reduce_id[0] return sub_handler builder_p = mark_id_handler(builder) else: builder_p = builder ret = p.parse_all(builder_p) if ret != pyauparser.ParseResultType.ACCEPT: print p.error_info return print "* SRC:", src_path result = builder.result result.dump() export_to_dot(result, r"dot.exe", dot_path, png_path) print
def c_parse(cmd_args): opts, args = getopt.getopt(cmd_args, "e:t") encoding = None trim_reduction = False for o, a in opts: if o == "-e": encoding = a elif o == "-t": trim_reduction = True egt_path = args[0] data_path = args[1] g = pyauparser.Grammar.load_file(egt_path) p = pyauparser.Parser(g) p.load_file(data_path, encoding=encoding) p.trim_reduction = trim_reduction while True: ret = p.parse_step() if ret == pyauparser.ParseResultType.ACCEPT: print("Accept") break elif ret == pyauparser.ParseResultType.SHIFT: token = p.token print("Shift\t{0} ({1}:{2})".format(token, p.line, p.column)) elif ret == pyauparser.ParseResultType.REDUCE: print("Reduce\t{0}".format(p.reduction.production)) elif ret == pyauparser.ParseResultType.REDUCE_ELIMINATED: print("ReduceEliminated") elif ret == pyauparser.ParseResultType.ERROR: print("Error\t'{0}' ({1}:{2})".format(p.error_info, p.line, p.column)) return