changeset 33:e9e90c006760

simplify grep.c, correnspod syntax '^'.
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Sat, 10 Jul 2010 15:37:41 +0900
parents 8d5ab8036fac
children 50b10929be29
files pyrect/grep_translator.py pyrect/jitgrep.py pyrect/template/grep.c
diffstat 3 files changed, 36 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/pyrect/grep_translator.py	Sat Jul 10 02:50:03 2010 +0900
+++ b/pyrect/grep_translator.py	Sat Jul 10 15:37:41 2010 +0900
@@ -24,6 +24,7 @@
         self.funType = 'int '
         self.callType = 'return '
         self.breakStatement = ''
+        self.begline = False
 
     def emit_accept_state(self):
         self.emit ("""
@@ -38,9 +39,7 @@
 }\n""" % self.funType)
 
     def emit_initialization(self):
-        self.emit("%sFILTER(char* s);\n" % (self.funType))
         self.emit("%sDFA(char* s);\n" % (self.funType))
-        self.emit("extern int bmhfilter(char* text, char *key);\n")
         self.emit("extern int grepmain(int argc, char* argv[]);\n")
         for state in self.cg.map.iterkeys():
             self.emit(self.funType + self.modify_state_name(state) + "(char* s);\n")
@@ -48,18 +47,28 @@
         self.emit(self.funType + 'reject(char* s);\n')
 
     def emit_filter(self):
-        self.emit("""
-int FILTER(char *text) {
+        pass
+
+    def emit_matcher(self):
+        self.emit("int match(char *regexp, char *text) {")
+        if self.begline:
+            self.emit("\n\t\treturn DFA(text);\n")
+        else:
+            self.emit("""
+\tdo {
+\t\tif  (DFA(text))
+\t\t\treturn 1;
+\t} while (*text++ != '\\0');
+\treturn 0;
 """)
-        self.emit("\treturn 1;\n}\n")
+        self.emit("}\n\n")
 
     def emit_driver(self):
         self.emit("""
 int main(int argc, char* argv[]) {
 \treturn grepmain(argc, argv);
-}
-""")
-        self.emit_filter()
+}\n\n""")
+        self.emit_matcher()
         self.emit("""
 %sDFA(char *s) {
   return %s(s);
--- a/pyrect/jitgrep.py	Sat Jul 10 02:50:03 2010 +0900
+++ b/pyrect/jitgrep.py	Sat Jul 10 15:37:41 2010 +0900
@@ -9,51 +9,50 @@
 from dfareg import Regexp, CallGraph
 
 def main(argv):
-    myusage = "%prog [--time] [--debug] [--cc=compiler] [-Olevel] regexp [file ..]"
+    myusage = "%prog [--build-lib] [--time] [--debug] [--cc=compiler] [-Olevel] regexp [file ..]"
     psr = OptionParser(usage=myusage)
 
     redirect = ""
-    optimize = "-O3"
     srcpath = "/tmp/jitgrep_dfa.c"
     binpath = "/tmp/jitgrep"
     libgrep = os.path.dirname(__file__) + "/template/libgrep.so"
 
-    argv_iter = argv
-    for args in argv_iter:
-       matchp = re.match("^-O[0-9]?$", args)
-       if matchp:
-           optimize = matchp.group(0)
-           argv.remove(optimize)
-
-    psr.add_option("--cc", action="store", type="string", dest="compiler", default="gcc", metavar="FILE",
+    psr.add_option("--cc", action="store", type="string", dest="cc", default="gcc", metavar="FILE",
                    help="Choose compiler (default is gcc).")
-    psr.add_option("--CFLAGS", action="store", type="string", dest="cflags", default="-O3", help="Print compile/matching time.")
+    psr.add_option("--CFLAGS", action="store", type="string", dest="cflags", default="-O3 -fomit-frame-pointer", help="Print compile/matching time.")
     psr.add_option("--time", action="store_true", dest="time", default=False, help="Print compile/matching time.")
     psr.add_option("--debug", action="store_true", dest="debug", default=False, help="Dump commands, not evalute matching (except interactive mode).")
+    psr.add_option("--build-lib", action="store_true", dest="rebuild", default=False, help="Building libgrep.so, although which exists.")
     psr.add_option("--out", action="store", type="string", dest="out", default="", metavar="FILE", help="Output file.")
-    psr.add_option("-O", action="store_true", dest="_", help="Optimizing compilation takes somewhat more time, level be recognized a digits [0-9]. (default: -O3)")
 
     (opts, args) = psr.parse_args(argv)
-    libgrep = ".".join([libgrep, opts.compiler])
+    libgrep = ".".join([libgrep, opts.cc])
 
     if len(args) < 2:
         psr.print_usage()
         return
 
-    if opts.compiler == "cbc":
+    if opts.cc == "cbc":
         cbc = True
-        opts.compiler = "gcc"
+        opts.cc = "gcc"
     else:
         cbc = False
 
     if opts.debug: print("option", opts)
     if opts.debug: print("args", args)
 
+    string = args[1]
+    if string[0] == "^":
+        string = string[1:]
+        begline = True
+    else:
+        begline = False
+
     if (opts.time) : start_time = time.time()
-    string = args[1]
     reg = Regexp(string)
     dfacg = CallGraph(reg.dfa)
     grept = GREPTranslator(string, dfacg)
+    grept.begline = begline
 
     tmpsrc = open(srcpath, 'w')
     grept.translate(tmpsrc)
@@ -62,14 +61,14 @@
         end_time = time.time()
         print("Translation: " + str(end_time - start_time) + " Sec.")
 
-    if not os.path.exists(libgrep):
-        cmd = " ".join([opts.compiler, optimize, opts.cflags, "-c -fPIC -shared template/grep.c -o", libgrep])
+    if opts.rebuild or not os.path.exists(libgrep):
+        cmd = " ".join([opts.cc, opts.cflags, "-c -fPIC -shared template/grep.c -o", libgrep])
         if opts.debug:
             print cmd
         else:
             os.system(cmd)
 
-    cmd = " ".join([opts.compiler, optimize, opts.cflags, srcpath, libgrep, "-o", binpath])
+    cmd = " ".join([opts.cc, opts.cflags, srcpath, libgrep, "-o", binpath])
     if opts.debug:
         print("compile command", cmd)
     else:
--- a/pyrect/template/grep.c	Sat Jul 10 02:50:03 2010 +0900
+++ b/pyrect/template/grep.c	Sat Jul 10 15:37:41 2010 +0900
@@ -7,31 +7,7 @@
 
 #define BUFSIZE 1024
 
-extern int DFA(char *text);
-extern int FILTER(char *text);
-int bmhfilter(char *text, char *key);
-
-int match(char *regexp, char *text) {
-  if (regexp[0] == '^')
-    return DFA(text);
-  do {
-    if  (FILTER(text) && DFA(text))
-      return 1;
-  } while (*text++ != '\0');
-  return 0;
-}
-
-int bmhfilter(char *text, char *key) {
-  int i, j;
-  i = j = 0;
-  while (text[i] != '\0' && key[j] != '\0') {
-    if (text[i] == key[j]) { ++i; ++j; }
-    else { i = i - j + 1; j = 0; }
-  }
-  if (key[j] == '\0')
-    return 1; /* position = k - j; */
-  return 0;
-}
+extern int match(char *regexp, char *text);
 
 int grep(char * regexp, FILE *f, char *name) {
   int n, nmatch;