changeset 15:104ee9208b17

add options -Olevel --time --debug --cc=compiler to jitgrep. add agrep to benchgrep.sh.
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Mon, 05 Jul 2010 03:27:38 +0900
parents 55684cb51347
children 100efeeb2ad9
files code/c/dfa.c code/c/nfa.c src/__init__.py src/benchgrep.sh src/benchgrep.sh~ src/jitgrep.py src/jitgrep.py~
diffstat 6 files changed, 110 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/code/c/dfa.c	Sun Jul 04 11:06:41 2010 +0900
+++ b/code/c/dfa.c	Mon Jul 05 03:27:38 2010 +0900
@@ -1,41 +1,76 @@
 #include <stdio.h>
-void state_1_3(char* s);
-void state_1_2(char* s);
+
+void state_0(char* s);
+void state_1(char* s);
+void state_2(char* s);
+void state_3(char* s);
 void accept(char* s);
 void reject(char* s);
 
 int main(int argc, char* argv[]) {
-	puts("regexp: A*");
-	puts("number of state: 2");
+	puts("regexp: (A|B)*C");
+	puts("number of state: 4");
 	printf("string: %s\n", argv[1]);
-	state_1_3(argv[1]);
+	state_1(argv[1]);
 
 	return 0;
 }
 
-void state_1_3(char* s) {
-	printf("state: 1_3, input: %s\n", s);
+void state_0(char* s) {
+	switch(*s++) {
+		case '\0': 
+			accept(s);
+			break;
+		default:
+			reject(NULL);
+	}
+}
+
+void state_1(char* s) {
 	switch(*s++) {
 		case 'A': 
-			state_1_2(s);
+			state_3(s);
+			break;
+		case 'C': 
+			state_0(s);
 			break;
-		case '\0':
-			accept(s);
+		case 'B': 
+			state_2(s);
 			break;
-		default: reject(s);
+		default:
+			reject(NULL);
 	}
 }
 
-void state_1_2(char* s) {
-	printf("state: 1_2, input: %s\n", s);
+void state_2(char* s) {
 	switch(*s++) {
 		case 'A': 
-			state_1_2(s);
+			state_3(s);
+			break;
+		case 'C': 
+			state_0(s);
+			break;
+		case 'B': 
+			state_2(s);
 			break;
-		case '\0':
-			accept(s);
+		default:
+			reject(NULL);
+	}
+}
+
+void state_3(char* s) {
+	switch(*s++) {
+		case 'A': 
+			state_3(s);
 			break;
-		default: reject(s);
+		case 'C': 
+			state_0(s);
+			break;
+		case 'B': 
+			state_2(s);
+			break;
+		default:
+			reject(NULL);
 	}
 }
 
@@ -46,4 +81,4 @@
 
 void reject(char* s) {
 	printf("\nstring does not match regexp. \n\n");
-}
+}
\ No newline at end of file
--- a/code/c/nfa.c	Sun Jul 04 11:06:41 2010 +0900
+++ b/code/c/nfa.c	Mon Jul 05 03:27:38 2010 +0900
@@ -1,4 +1,5 @@
 #include <stdio.h>
+
 void state_1(char* s);
 void state_3(char* s);
 void state_2(char* s);
@@ -21,7 +22,6 @@
 }
 
 void state_1(char* s) {
-	printf("state: 1, input: %s\n", s);
 	char* s_local = s;
 	switch(*s_local++) {
 		case 'A': 
@@ -31,7 +31,6 @@
 }
 
 void state_3(char* s) {
-	printf("state: 3, input: %s\n", s);
 	char* s_local = s;
 	switch(*s_local++) {
 		case 'B': 
@@ -41,34 +40,24 @@
 }
 
 void state_2(char* s) {
-	printf("state: 2, input: %s\n", s);
 	char* s_local = s;
 	state_5(s_local);
 	state_7(s_local);
-	switch(*s_local++) {
-	}
 }
 
 void state_5(char* s) {
-	printf("state: 5, input: %s\n", s);
 	char* s_local = s;
 	state_1(s_local);
 	state_3(s_local);
-	switch(*s_local++) {
-	}
 }
 
 void state_4(char* s) {
-	printf("state: 4, input: %s\n", s);
 	char* s_local = s;
 	state_5(s_local);
 	state_7(s_local);
-	switch(*s_local++) {
-	}
 }
 
 void state_7(char* s) {
-	printf("state: 7, input: %s\n", s);
 	char* s_local = s;
 	switch(*s_local++) {
 		case 'C': 
@@ -78,19 +67,15 @@
 }
 
 void state_6(char* s) {
-	printf("state: 6, input: %s\n", s);
 	char* s_local = s;
 	state_5(s_local);
 	state_7(s_local);
-	switch(*s_local++) {
-	}
 }
 
 void state_8(char* s) {
-	printf("state: 8, input: %s\n", s);
 	char* s_local = s;
 	switch(*s_local++) {
-		case '\0':
+		case '\0': 
 			accept(s_local);
 			break;
 	}
--- a/src/benchgrep.sh	Sun Jul 04 11:06:41 2010 +0900
+++ b/src/benchgrep.sh	Mon Jul 05 03:27:38 2010 +0900
@@ -2,14 +2,21 @@
 
 egrepout="/tmp/egrep.out"
 jitgrepout="/tmp/jitgrep.out"
+agrepout="/tmp/agrep.out"
 
 echo "[jitgrep]"
 time ./jitgrep.py $@ > $jitgrepout
 
+echo "\n[agrep]"
+time agrep $@ > $agrepout
+
 echo "\n[egrep]"
 time egrep    $@ > $egrepout
 
 echo "\n[diff egrep jitgrep]"
 diff $egrepout $jitgrepout
 
+echo "[diff agrep jitgrep]"
+diff $agrepout $jitgrepout
+
 #rm -f $egrepout $jitgrepout
--- a/src/benchgrep.sh~	Sun Jul 04 11:06:41 2010 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-#!/bin/sh
-
-egrepout="/tmp/egrep.out"
-jitgrepout="/tmp/jitgrep.out"
-
-echo "[egrep]"
-time egrep    $@ > $egrepout
-
-echo "\n[jitgrep]"
-time ./jitgrep.py $@ > $jitgrepout
-
-echo "\n[diff egrep jitgrep]"
-diff $egrepout $jitgrepout
-
-#rm -f $egrepout $jitgrepout
--- a/src/jitgrep.py	Sun Jul 04 11:06:41 2010 +0900
+++ b/src/jitgrep.py	Mon Jul 05 03:27:38 2010 +0900
@@ -3,6 +3,7 @@
 import sys
 import os
 import re
+import time
 from optparse import OptionParser
 from grep_translator import GREPTranslator
 from dfareg import Regexp, CallGraph
@@ -12,63 +13,85 @@
     psr = OptionParser(usage=myusage)
 
     optimize = ""
-    debug = False
-    time = False
+    redirect = ""
+    srcpath = "/tmp/jitgrep_emit.c"
+    binpath = "/tmp/jitgrep_emit"
+
     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")
+    psr.add_option("--cc", action="store", type="string", dest="compiler", default="gcc", help="string", metavar="FILE",
+                   help="Choose compiler (default is gcc).")
+    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("--out", action="store", type="string", dest="out", default="", help="string", 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].")
+
     (opts, args) = psr.parse_args(argv)
 
-    if debug: print("option", opts)
-    if debug: print("args", args)
+    if opts.compiler == "cbc":
+        cbc = True
+        opts.compiler = "gcc"
+    else:
+        cbc = False
 
-    string = argv[1]
+    if opts.debug: print("option", opts)
+    if opts.debug: print("args", args)
+
+    if (opts.time) : start_time = time.time()
+    string = args[1]
     reg = Regexp(string)
     dfacg = CallGraph(reg.dfa)
-    tje = GREPTranslator(string, dfacg)
-
-    srcpath = "/tmp/jitgrep_emit.c"
-    binpath = "/tmp/jitgrep_emit"
+    grept = GREPTranslator(string, dfacg)
 
     tmpsrc = open(srcpath, 'w')
-    tje.translate(tmpsrc)
+    grept.translate(tmpsrc)
     tmpsrc.close()
+    if (opts.time):
+        end_time = time.time()
+        print("Translation: " + str(end_time - start_time) + " Sec.")
 
     cmd = " ".join([opts.compiler, optimize, srcpath, "-o", binpath])
-    if debug:
+    if opts.debug:
         print("compile command", cmd)
     else:
+        if (opts.time): start_time = time.time()
         os.system(cmd)
+        if (opts.time):
+            end_time = time.time()
+            print("Compiling: " + str(end_time - start_time) + " Sec.")
 
-    # print("argc=" + str(len(argv)))
-    # print(argv)
+    if opts.debug:
+        print("argv=" + argv)
+        print("args=" + args)
+        print("opts=" + opts)
 
-    if len(argv) == 2:
+    if len(args) == 2 and not opts.debug:
         while True:
             try:
                 os.system(binpath + ' ' + raw_input())
             except (KeyboardInterrupt, EOFError):
                 break
     else:
-        cmd = binpath + ' dummy_option ' + ' '.join(argv[2:])
-        if debug:
+        if (opts.time): redirect = "> /dev/null"
+        if (opts.out):  redirect = ">" + opts.out
+        cmd = ' '.join([binpath, "dummy"] + args[2:] + [redirect])
+        if opts.debug:
             print("exec command", cmd)
         else:
+            if (opts.time): start_time = time.time()
             os.system(cmd)
+            if (opts.time):
+                end_time = time.time()
+                print("Matching: " + str(end_time - start_time) + " Sec.")
 
-    if not debug:
-        os.remove(srcpath)
+    if not opts.debug:
+        #os.remove(srcpath)
         os.remove(binpath)
 
 if __name__ == '__main__': main(sys.argv)
--- a/src/jitgrep.py~	Sun Jul 04 11:06:41 2010 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,14 +0,0 @@
-import sys
-from grep_translator import GREPTranslator
-from dfareg import Regexp, CallGraph
-
-def main(sys.argv):
-    string = "(gcc|fndecl|build)"
-    reg = Regexp(string)
-    dfacg = CallGraph(reg.dfa)
-    tje = GREPTranslator(string, dfacg)
-    tmpsrc = open("/tmp/jitgrep_emit.c", "w")
-    tje.translate(tmpsrc)
-    print(sys.argv)
-
-if __name__ == '__main__': main(sys.argv)