Exemplo n.º 1
0
Arquivo: rxq.go Projeto: proebsting/re
func main() {
	var ifile *bufio.Scanner
	if len(os.Args) == 2 {
		ifile = rx.MkScanner("-")
	} else if len(os.Args) == 3 {
		ifile = rx.MkScanner(os.Args[2])
	} else {
		log.Fatal("usage: rxq \"rexpr\" [file]")
	}
	spec := os.Args[1]
	fmt.Printf("regexp: %s\n", spec)
	dfa, err := rx.Compile(spec)
	if err != nil {
		log.Fatal(err)
	}

	// load and process candidate strings
	for i := 0; ifile.Scan(); i++ {
		s := ifile.Text()
		if dfa.Accepts(s) != nil {
			fmt.Println("accept:", s)
		} else {
			fmt.Println("REJECT:", s)
		}
	}
	rx.CkErr(ifile.Err())
}
Exemplo n.º 2
0
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)
	}
}
Exemplo n.º 3
0
// setup performs global initialization actions
func setup() {
	rxsys.Interval() // initialize timestamps
	options()        // process command-line options
	if verbose {
		showopts() // show command options
	}

	// handle -P (profiling) option
	pfname := *val['P'] // profiling filename
	if pfname != "" {   // if set
		pfile, err := os.Create(pfname)
		rx.CkErr(err)
		pprof.StartCPUProfile(pfile)
	}

	// set up input scanning
	if *val['e'] != "" { // if -e specified
		if len(flag.Args()) > 0 {
			log.Fatal("-e option precludes reading " + flag.Arg(0))
		}
		input = bufio.NewScanner(bytes.NewReader([]byte(*val['e'])))
	} else {
		input = rx.MkScanner(rx.OneInputFile())
	}

	timestamp("init")
}
Exemplo n.º 4
0
//  CPUtime returns the current CPU usage (user time + system time).
func CPUtime() time.Duration {
	var ustruct syscall.Rusage
	rx.CkErr(syscall.Getrusage(0, &ustruct))
	user := time.Duration(syscall.TimevalToNsec(ustruct.Utime))
	sys := time.Duration(syscall.TimevalToNsec(ustruct.Stime))
	return user + sys
	return 0
}
Exemplo n.º 5
0
Arquivo: rxx.go Projeto: 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())
}
Exemplo n.º 6
0
//  options defines the command line options to be accepted.
func options() {

	vo('e', "single expression value (instead of reading input)")
	fo('i', "treat input expressions individually")
	fo('m', "merge all input expressions into one large DFA")
	fo('u', "don't minimize (optimize) any DFA produced")

	fo('l', "list input comments and metadata")
	fo('p', "show parse tree nodes with nullable, first, last")
	fo('n', "show positions and followsets of NFA")
	fo('d', "show states and transitions of DFA")
	fo('g', "show synthetic examples of matching strings from parse")
	fo('h', "show examples with reference to DFA states")
	fo('t', "output timing measurements")
	fo('v', "enable running commentary")
	fo('a', "enable all output options") // all of the above
	fo('x', "exit immediately on erroneous input")
	fo('y', "silently ignore errors")
	fo('z', "enable debug tracing")

	vo('I', "initial seed for randomization")
	fo('R', "reseed for every expression")
	fo('T', "set standard test options")

	// file format for N and D depend on extension of filename supplied
	// a filename of "@" generates a temporary SVG file and opens a viewer
	vo('N', "output file for NFA graph (*.dot/.gif/.pdf/.png/.svg/@)")
	vo('D', "output file for DFA graph (*.dot/.gif/.pdf/.png/.svg/@)")

	vo('C', "complexity limit")
	vo('P', "output file for profiling data (binary)")

	flag.Parse() // parse command args to set values

	imply("a", "lpndghtv")   // -a implies several others
	imply("NDX", "m")        // any of {-N -D -X} implies -m
	if imply("T", "lghvR") { // -T implies several plus -I 0
		*val['I'] = "0"
	}

	if !*opt['m'] {
		*opt['i'] = true // if not -m, then must have -i
	}
	if *val['I'] == "" {
		s := fmt.Sprintf("%d", 1+time.Now().Nanosecond()%9998)
		val['I'] = &s
	}

	// set global flag values based on options
	listopt = *opt['l']
	verbose = *opt['v']
	errsilent = *opt['y']
	rx.DBG_MIN = *opt['z'] && *opt['d']

	// set complexity limit
	if *val['C'] != "" { // if complexity option set
		maxc, err := strconv.Atoi(*val['C'])
		rx.CkErr(err)
		rx.MaxComplexity = maxc
	}

	// initialize random number generator
	seedvalue, err := strconv.Atoi(*val['I'])
	rx.CkErr(err)
	rand.Seed(int64(seedvalue))
}