Example #1
0
//  combos responds to a comparison request for multiple expressions
func combos(w http.ResponseWriter, r *http.Request) {

	// must read all input before writing anything
	// first get the expressions, trimming leading/trailing blanks
	exprlist := getexprs(r)
	nx := len(exprlist)

	// then get the test strings; these are not trimmed
	testlist := make([]string, 0)
	for i := 0; i < maxTest; i++ {
		arg := r.FormValue(fmt.Sprintf("v%d", baseTest+i))
		if arg != "" {
			testlist = append(testlist, arg)
		}
	}

	// parse and echo the input
	putheader(w, r, "Compare Expressions")
	fmt.Fprintf(w, "<P class=xleading>%d expressions:\n", nx)
	treelist := lpxc(w, exprlist)

	if nx > 0 && len(treelist) == nx { // if no errors
		dfa := rx.MultiDFA(treelist) // build combined DFA
		trylist := make([]string, 0) // list of strings to try
		// tests from form submission
		for _, x := range testlist {
			trylist = append(trylist, x)
		}
		trylist = append(trylist, DRAWLINE)
		// examples from DFA
		synthx := dfa.Synthesize() // synthesize from DFA
		for _, x := range synthx { // put results on list
			trylist = append(trylist, x.Example)
		}
		// DON'T separate parse tree examples
		// (confusing in the the absence of a published explanation)
		// trylist = append(trylist, DRAWLINE)
		// examples from parse tree
		for i := 0; i < nx; i++ {
			trylist = append(trylist, rx.Specimen(treelist[i], 1))
			trylist = append(trylist, rx.Specimen(treelist[i], 2))
			trylist = append(trylist, rx.Specimen(treelist[i], 3))
			trylist = append(trylist, rx.Specimen(treelist[i], 5))
		}
		fmt.Fprintf(w, "<H2>Results</H2>\n")
		showgrid(w, dfa, nx, trylist) // show examples
	}

	fmt.Fprintln(w, "<P>&nbsp;")
	formlink(w, "/multaut", exprlist, "Show automata states")
	formlink(w, "/drawNFA", exprlist, "Draw the NFA")
	formlink(w, "/drawDFA", exprlist, "Draw the DFA")

	fmt.Fprint(w, "<h2>Try again?</h2>")
	putform(w, "/combos", "Enter regular expressions:",
		nExpr, exprlist, nTest, testlist)
	putfooter(w, r)
}
Example #2
0
//  generate examples from a parse tree
func genex(tree rx.Node) []string {
	x := make([]string, 0)
	for i := 0; i < nexamples; i++ {
		r := maxrepl - i%maxrepl
		x = append(x, rx.Specimen(tree, r))
	}
	return x
}
Example #3
0
//  genexamples writes a line of specimen strings matching the expression
func genexamples(w http.ResponseWriter, tree rx.Node, maxrepl int) {
	nprinted := 0
	ncolm := 0
	for {
		s := rx.Specimen(tree, maxrepl)
		t := rx.Protect(s)
		ncolm += 2 + len(t)
		if nprinted > 0 && ncolm > linemax {
			break
		}
		fmt.Fprintf(w, " %s &nbsp; ", hx(t))
		nprinted++
	}
	fmt.Fprint(w, "<BR>\n")
}
Example #4
0
//   examples generates a line's worth of examples with max replication n.
//   Each example is also tested against the DFA.
func examples(dfa *rx.DFA, tree rx.Node, n int) {
	s := fmt.Sprintf("ex(%d):", n)
	nprinted := 0
	fmt.Print(s)
	ncolm := utf8.RuneCountInString(s)
	for {
		s := rx.Specimen(tree, n)
		t := rx.Protect(s)
		ncolm += 2 + utf8.RuneCountInString(t)
		if nprinted > 0 && ncolm > linemax {
			break
		}
		fmt.Printf("  %s", t)
		if dfa.Accepts(s) == nil {
			fmt.Print(" [FAIL]")
			ncolm += 7
		}
		nprinted++
	}
	fmt.Println()
}