예제 #1
0
파일: rxpick.go 프로젝트: proebsting/re
func main() {
	// get command line options
	if len(os.Args) < 3 {
		log.Fatal("usage: rxpick exprfile i ...")
	}
	filename := os.Args[1]
	xset := &rx.BitSet{}
	for i := 2; i < len(os.Args); i++ {
		n, err := strconv.Atoi(os.Args[i])
		rx.CkErr(err)
		xset.Set(n)
	}

	// load expressions from file
	exprs := rx.LoadExpressions(filename, nil)

	// print desired entries
	for _, i := range xset.Members() {
		fmt.Printf("\n# { %d }\n", i)
		if i >= len(exprs) {
			fmt.Printf("# OUT OF RANGE\n")
			continue
		}
		exprs[i].ShowMeta(os.Stdout, "")
		fmt.Println(exprs[i].Expr)
	}
}
예제 #2
0
파일: rxcluster.go 프로젝트: proebsting/re
//  load expressions, returning list and augmented parse tree list
func load(filename string) []*RegEx {
	elist := make([]*RegEx, 0) // expression structure list
	rx.LoadExpressions(filename, func(l *rx.RegExParsed) {
		if l.Tree != nil { // if a real expression
			e := &RegEx{}
			e.index = len(elist)
			e.cnum = len(elist)
			e.RegExParsed = *l         // save parse tree
			e.examples = genex(l.Tree) // gen examples unaugmented
			t := rx.Augment(l.Tree, e.index)
			e.dfa = rx.BuildDFA(t) // build DFA
			if verbose {
				fmt.Println()
				showexpr(e)     // show details of expr
				showexamples(e) // show its examples
			}
			elist = append(elist, e)
		}
	})
	if verbose {
		fmt.Printf("\nloaded %d expression(s), ignored %d more\n",
			rx.InputRegExCount, rx.InputErrorCount)
	}
	return elist
}
예제 #3
0
파일: rxtime.go 프로젝트: proebsting/re
func main() {
	// get command line options
	nways := 1
	if len(os.Args) == 3 {
		nways, _ = strconv.Atoi(os.Args[2])
	}
	if len(os.Args) < 2 || len(os.Args) > 3 || nways < 1 {
		log.Fatal("usage: rxtime exprfile [n]")
	}
	filename := os.Args[1]

	// load expressions from file
	exprs := rx.LoadExpressions(filename, nil)
	nexprs := len(exprs)
	if nways < 1 || nways > nexprs {
		log.Fatal(fmt.Sprintf(
			"cannot combine %d expressions(s) in %d way(s)",
			nexprs, nways))
	}

	// record individual complexity scores and make augmented parse trees
	cx := make([]int, nexprs)
	for i, t := range exprs {
		cx[i] = rx.ComplexityScore(t.Tree)
		t.Tree = rx.Augment(t.Tree, i)
	}

	// initialize index list for first combination {0,1,2...}
	xlist := make([]int, nways)
	for i := range xlist {
		xlist[i] = i
	}

	// try all possible n-way combinations by varying the index list
	tlist := make([]rx.Node, nways)
	for xlist != nil {
		for i, x := range xlist {
			tlist[i] = exprs[x].Tree
		}
		_ = rxsys.Interval()             // reset timer
		dfa1 := rx.MultiDFA(tlist)       // make DFA
		t1 := rxsys.Interval().Seconds() // measure time
		dfa2 := dfa1.Minimize()          // minimize DFA
		t2 := rxsys.Interval().Seconds() // measure time
		fmt.Printf("%6d %6d %8.3f %8.3f",
			len(dfa1.Dstates), len(dfa2.Dstates), t1, t2)
		if nways == 1 {
			fmt.Printf(" %6d", cx[xlist[0]])
		}
		fmt.Print("   {")
		for _, x := range xlist {
			fmt.Printf(" %d", x)
		}
		fmt.Print(" }\n")
		xlist = advance(xlist, nexprs) // get next combination
	}
}
예제 #4
0
파일: rxg.go 프로젝트: proebsting/re
func main() {

	rflag := flag.Bool("R", false, "reproducible output")
	flag.Parse()
	if *rflag {
		rand.Seed(0)
	} else {
		rand.Seed(int64(time.Now().Nanosecond()))
	}

	// load and process regexps
	exprs := make([]*RegEx, 0)
	tlist := make([]rx.Node, 0)
	rx.LoadExpressions(rx.OneInputFile(), func(l *rx.RegExParsed) {
		if l.Err != nil {
			fmt.Fprintln(os.Stderr, l.Err)
		}
		if l.Tree != nil {
			atree := rx.Augment(l.Tree, len(tlist))
			tlist = append(tlist, atree)
			exprs = append(exprs, &RegEx{len(exprs), l.Expr})
		}
	})

	// echo the input with index numbers
	fmt.Print(`{"Expressions":`)
	rx.Jlist(os.Stdout, exprs)
	fmt.Println(",")

	// build the DFA and produce examples
	synthx := rx.MultiDFA(tlist).Synthesize()

	// convert into expected form with int array replacing BitSet
	results := make([]*Example, 0, len(synthx))
	for _, x := range synthx {
		results = append(results,
			&Example{x.State, x.RXset.Members(), x.Example})
	}

	// output the array of synthesized examples
	fmt.Print(`"Examples":`)
	rx.Jlist(os.Stdout, results)
	fmt.Println("}")
}
예제 #5
0
파일: rxquest.go 프로젝트: proebsting/re
// load expressions, returning list and augmented parse tree list
func load(filename string) ([]*rx.RegExParsed, []rx.Node) {
	errcount := 0                       // error count
	exprs := make([]*rx.RegExParsed, 0) // expression structure list
	trees := make([]rx.Node, 0)         // augmented parse tree list

	rx.LoadExpressions(filename, func(l *rx.RegExParsed) {
		if l.Tree != nil { // if a real expression
			augtree := rx.Augment(l.Tree, len(trees))
			trees = append(trees, augtree)
			exprs = append(exprs, l)
		} else if l.Err != nil { // if erroneous
			errcount++
		} // else is just a comment
	})
	fmt.Printf("%d expressions loaded\n", len(exprs))
	if errcount != 0 {
		fmt.Printf("(%d improper expressions ignored)\n", errcount)
	}
	return exprs, trees
}
예제 #6
0
파일: rxx.go 프로젝트: proebsting/re
func main() {
	args := os.Args
	if len(args) != 3 {
		log.Fatal("usage: rxx efile sfile")
	}
	ename := args[1]
	sfile := rx.MkScanner(args[2])

	labels :=
		"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	elist := make([]tester, 0, len(labels))

	// load and compile regexps
	fmt.Println()
	tlist := make([]rx.Node, 0) // list of valid parse trees
	rx.LoadExpressions(ename, func(x *rx.RegExParsed) {
		spec := x.Expr
		ptree := x.Tree
		err := x.Err
		if err != nil {
			fmt.Printf("ERR %s\n", spec)
			elist = append(elist, tester{" ", spec, nil, 0})
		} else if ptree == nil {
			fmt.Printf("    %s\n", spec)
			elist = append(elist, tester{" ", spec, nil, 0})
		} else {
			i := len(tlist)
			if i >= len(labels) {
				log.Fatal("too many regular expressions")
			}
			label := string(labels[i : i+1])
			fmt.Printf("%s:  %s\n", label, spec)
			atree := rx.Augment(ptree, len(tlist))
			tlist = append(tlist, atree)
			elist = append(elist, tester{label, spec, ptree, i})
		}
	})

	dfa := rx.MultiDFA(tlist)
	_ = dfa.Minimize()   // should have no effect
	_ = dfa.Minimize()   // should again have no effect
	dfa = dfa.Minimize() // not necessary, but a good stress test
	dfa = dfa.Minimize() // especially if done more than once

	// read and test candidate strings
	fmt.Println()
	for sfile.Scan() {
		s := string(sfile.Bytes())
		results := dfa.Accepts(s)
		if results == nil {
			results = &rx.BitSet{}
		}
		for _, e := range elist {
			if e.tree == nil {
				fmt.Print(" ")
			} else {
				if results.Test(e.index) {
					fmt.Print(e.label)
				} else {
					fmt.Print("-")
				}
			}
		}
		fmt.Printf("  %s\n", s)
	}
	rx.CkErr(sfile.Err())
}