#!/usr/bin/python import sys import re import pickle from table import * #Swap operator SWAP = Manipulation("**SWAP**", 2,(2,1)) LIT = Manipulation("%s", 0, (5,)) def output(table, stack, literal, current, commutative): if table.has_key(stack): if literal != "": opt = table[stack] % literal else: opt = table[stack] else: # print "Can't match ", stack opt = current if commutative: stack = SWAP.change(stack) if table.has_key(stack): if literal != "": opt2 = table[stack] % literal else: opt2 = table[stack] else: opt2 = current if len(opt) > len(opt2): opt = opt2 # print stack if opt != "": print opt, f = open(sys.argv[1]); unpick = pickle.Unpickler(f) table = unpick.load() manipulators = unpick.load() base_stack = unpick.load() stack_re = unpick.load() commute_re = unpick.load() literal_re = unpick.load() f.close(); print >> sys.stderr, "Matching against %d possible sequences" % len(table.keys()) args = 0 stack = base_stack has_lit = 0 changed = 0 current = "" literal = "" line = sys.stdin.readline() while line: if stack_re.match(line): changed = 1 for op in manipulators: if op.name == line: try: stack = op.change(stack) except: output(table, stack, literal, current, 0) current = line literal = "" stack = op.change(base_stack) break current += line elif literal_re.match(line): if literal != "": output(table, stack, literal, current, 0) current = "" stack = base_stack stack = LIT.change(stack) current += line literal = line elif commute_re.match(line) and base_stack != stack: output(table, stack, literal, current, 1) current = "" literal = "" stack = base_stack print line, else: if base_stack != stack: output(table, stack, literal, current, 0) current = "" literal = "" stack = base_stack print line, line = sys.stdin.readline()