Mercurial > hg > Members > shinya > pyrect
changeset 14:55684cb51347
add LICENSE
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 04 Jul 2010 11:06:41 +0900 |
parents | fb7922f6d9ef |
children | 104ee9208b17 |
files | LICENSE src/cTranslator.py src/cTranslator.pyc src/cbcTranslator.py src/dfareg.py src/dfareg.pyc src/dotTranslator.py src/grep_translator.py src/grep_translator.pyc src/jitgrep.py |
diffstat | 10 files changed, 74 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LICENSE Sun Jul 04 11:06:41 2010 +0900 @@ -0,0 +1,15 @@ +Copyright (c) 2006, 2007, 2008, 2009, 2010 Ryoma SHINYA + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + a. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + b. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + c. Neither the name of the Pyrect nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission.
--- a/src/cTranslator.py Sun Jul 04 08:45:57 2010 +0900 +++ b/src/cTranslator.py Sun Jul 04 11:06:41 2010 +0900 @@ -21,13 +21,13 @@ self.callType = '' self.breakStatement = '\t\t\tbreak;' self.debug = False - if self.cg.type is "DFA": + if self.cg.type == "DFA": self.name_hash = self.create_name_hash() def modify_state_name(self, state_name): - if state_name is "accept" or state_name is "reject": + if state_name == "accept" or state_name == "reject": return state_name - if self.cg.type is "DFA": + if self.cg.type == "DFA": return "state_"+self.name_hash[state_name] else: return "state_"+state_name @@ -52,14 +52,14 @@ \tprintf(\"string: %%s\\n\", argv[1]); \t%s%s(argv[1]); """ % (self.regexp, len(self.cg.states), self.callType, self.modify_state_name(self.cg.start))) - if self.cg.type is "NFA": + if self.cg.type == "NFA": self.emit("\treject(argv[1]);\n") self.emit(""" \treturn 0; }\n\n""") def emit_switch(self, case, default=None): - if self.cg.type is "NFA": + if self.cg.type == "NFA": sLocal = "s_local" else: sLocal = "s" @@ -82,7 +82,7 @@ self.emit(self.funType + self.modify_state_name(cur_state) + "(char* s) {\n") if self.debug: self.emit("\tprintf(\"state: %s, input: %%s\\n\", s);\n" % (cur_state)) - if self.cg.type is "NFA": + if self.cg.type == "NFA": sLocal = "s_local" self.emit("\tchar* %s = s;\n" % (sLocal)) if '' in transition: @@ -96,7 +96,7 @@ transition['\\0'] = ["accept"] if transition: - if self.cg.type is "DFA": + if self.cg.type == "DFA": self.emit_switch(transition, default="reject") else: self.emit_switch(transition)
--- a/src/cbcTranslator.py Sun Jul 04 08:45:57 2010 +0900 +++ b/src/cbcTranslator.py Sun Jul 04 11:06:41 2010 +0900 @@ -18,7 +18,7 @@ >>> ct.translate() """ def __init__(self, regexp, cg): - if cg.type is "NFA": raise CbCTranslateExeption("can't translate CbC from NFA") + if cg.type == "NFA": raise CbCTranslateExeption("can't translate CbC from NFA") CTranslator.__init__(self, regexp, cg) self.funType = '__code ' self.callType = 'goto '
--- a/src/dfareg.py Sun Jul 04 08:45:57 2010 +0900 +++ b/src/dfareg.py Sun Jul 04 11:06:41 2010 +0900 @@ -362,7 +362,7 @@ def create_call_graph(self, fa): transitionCases = dict() - if self.type is "DFA": + if self.type == "DFA": transitionCases[fa.start] = set() else: transitionCases[frozenset([fa.start])] = set() @@ -377,14 +377,14 @@ # : 6 x '' -> set([5, 7]) for nextState in fa.map.itervalues(): - if self.type is "DFA": + if self.type == "DFA": transitionCases[frozenset(nextState)] = set() else: for nextState_ in nextState: transitionCases[frozenset([nextState_])] = set() for (state, input) in fa.map.iterkeys(): - if self.type is "NFA": + if self.type == "NFA": state = [state] slot = transitionCases.setdefault(frozenset(state), set()) slot.add(input) @@ -399,7 +399,7 @@ for (state, input) in transitionCases.iteritems(): caseMap = dict() self.states.add(self.set2name(state)) - if self.type is "DFA": + if self.type == "DFA": for case in input: self.inputs.add(case) caseMap[case] = [self.set2name(fa.map[frozenset(state), case])]
--- a/src/dotTranslator.py Sun Jul 04 08:45:57 2010 +0900 +++ b/src/dotTranslator.py Sun Jul 04 11:06:41 2010 +0900 @@ -19,11 +19,11 @@ """ def __init__(self, regexp, cg): Translator.__init__(self, regexp, cg) - if self.cg.type is "DFA": + if self.cg.type == "DFA": self.name_hash = self.create_name_hash() def modify_state_name(self, state_name): - if self.cg.type is "DFA": + if self.cg.type == "DFA": return "s"+self.name_hash[state_name] else: return "s"+state_name @@ -44,7 +44,7 @@ for cur_state, trans in self.cg.map.iteritems(): for input, next_states in trans.iteritems(): - if input is "" : input = "$\\varepsilon$" + if input == "" : input = "$\\varepsilon$" for next_state in next_states: self.emit("\t%s -> %s [texlbl=\"%s\"]\n" % (self.modify_state_name(cur_state), self.modify_state_name(next_state), input))
--- a/src/grep_translator.py Sun Jul 04 08:45:57 2010 +0900 +++ b/src/grep_translator.py Sun Jul 04 11:06:41 2010 +0900 @@ -1,6 +1,9 @@ from cTranslator import CTranslator from dfareg import Regexp, CallGraph +class GREPTranslateExeption(Exception): + pass + class GREPTranslator(CTranslator): """GREPTranslator >>> string = \"(A|B)*C\" @@ -9,7 +12,9 @@ >>> tje = GREPTranslator(string, dfacg) >>> tje.translate() """ + def __init__(self, regexp, cg): + if cg.type == "NFA": raise GREPTranslateExeption("can't translate grep from NFA") CTranslator.__init__(self, regexp, cg) self.funType = 'int ' self.callType = 'return ' @@ -55,7 +60,7 @@ self.emit("\treturn accept(s);\n") else: if transition: - if self.cg.type is "DFA": + if self.cg.type == "DFA": self.emit_switch(transition, default="reject") else: self.emit_switch(transition)
--- a/src/jitgrep.py Sun Jul 04 08:45:57 2010 +0900 +++ b/src/jitgrep.py Sun Jul 04 11:06:41 2010 +0900 @@ -3,13 +3,35 @@ import sys import os import re +from optparse import OptionParser from grep_translator import GREPTranslator from dfareg import Regexp, CallGraph def main(argv): - if len(argv) < 2: - print("usage: jitgrep regexp [file ..]") - return + myusage = "%prog [--time] [--debug] [--cc=compiler] [-Olevel] regexp [file ..]" + psr = OptionParser(usage=myusage) + + optimize = "" + debug = False + time = False + argv_iter = argv + for args in argv_iter: + matchp = re.match("^-O[0-9]?$", args) + if matchp: + optimize = matchp.group(0) + argv.remove(optimize) + elif args == "--debug": + argv.remove("--debug") + debug = True + elif args == "--time": + time = True; + argv.remove("--time") + + psr.add_option("--cc", action="store", type="string", dest="compiler", default="gcc", help="string", metavar="FILE") + (opts, args) = psr.parse_args(argv) + + if debug: print("option", opts) + if debug: print("args", args) string = argv[1] reg = Regexp(string) @@ -23,9 +45,11 @@ tje.translate(tmpsrc) tmpsrc.close() - cmd = 'gcc ' + srcpath + " -o " + binpath - # print(cmd) - os.system(cmd) + cmd = " ".join([opts.compiler, optimize, srcpath, "-o", binpath]) + if debug: + print("compile command", cmd) + else: + os.system(cmd) # print("argc=" + str(len(argv))) # print(argv) @@ -34,14 +58,17 @@ while True: try: os.system(binpath + ' ' + raw_input()) - except KeyboardInterrupt: + except (KeyboardInterrupt, EOFError): break else: cmd = binpath + ' dummy_option ' + ' '.join(argv[2:]) - # print(cmd) - os.system(cmd) + if debug: + print("exec command", cmd) + else: + os.system(cmd) - os.remove(srcpath) - os.remove(binpath) + if not debug: + os.remove(srcpath) + os.remove(binpath) if __name__ == '__main__': main(sys.argv)