void testConstructNFA1(){ regexps = malloc(sizeof(struct REentry)); regexps->regexp = "1*|a"; regexps->action = "printf;"; constructNFA(); printGraph(); destroyNFA(); }
void testConstructNFA2(){ regexps = malloc(sizeof(struct REentry)); char *s = "(a|b)*abb(a|b)*"; regexps->regexp = malloc(strlen(s)); strcpy(regexps->regexp, s); regexps->action = "printf;"; preprocess(); constructNFA(); printGraph(); destroyNFA(); }
int main() { // test 1, for NFA.hpp and DFA.hpp string expression = "ab*(a|b)*abc"; string str1 = "abc"; string str2 = "aababababaabc"; NFA* nfa = constructNFA(expression); DFA *dfa = NFA2DFA(nfa); printf("\n"); printf("test 1 with NFA: %d\n", runNFA(nfa, str1)); printf("test 2 with NFA: %d\n", runNFA(nfa, str2)); printf("\n"); printf("test 1 with DFA: %d\n", runDFA(dfa, str1)); printf("test 2 with DFA: %d\n", runDFA(dfa, str2)); printf("\n"); // test2, for analyzer.hpp string input = "int ok = 1;\\nif name == 1, return ok;"; vector<NFA*> nfas; vector<string> regs; vector<string> tokenNames; regs.push_back("if"); tokenNames.push_back("if"); regs.push_back("int"); tokenNames.push_back("int"); regs.push_back("return"); tokenNames.push_back("return"); regs.push_back(","); tokenNames.push_back("comma"); regs.push_back(";"); tokenNames.push_back("semicolon"); regs.push_back("\\n"); tokenNames.push_back("newline"); regs.push_back("(i|f|o|k|n|a|m|e)*"); tokenNames.push_back("id"); regs.push_back(" "); tokenNames.push_back("space"); regs.push_back("=="); tokenNames.push_back("equals_sign"); regs.push_back("="); tokenNames.push_back("assignment_sign"); regs.push_back("(1|2|3|4|5|6|7|8|9|0)*"); tokenNames.push_back("number"); NFA_Analyzer *analyzer = build_NFA_Analyzer(regs, tokenNames); auto output = run_NFA_Analyzer(analyzer, input); for (auto p : output) printf("%15s -> %15s\n", p.first.c_str(), p.second.c_str()); // minDFA(); // double res = eval(vec); // cout << res << endl; return 0; }
void testConstructDFA3(){ regexps = malloc(sizeof(struct REentry)); char *s = "(a|b)*abb(a|b)*"; regexps->regexp = malloc(strlen(s)); strcpy(regexps->regexp, s); regexps->action = "printf;"; regexps->next = malloc(sizeof(struct REentry)); s = "(a|b)+abc(a|b|c)?"; regexps->next->regexp = malloc(strlen(s)); strcpy(regexps->next->regexp, s); regexps->next->action = "donothing"; regexps->next->next = NULL; preprocess(); constructNFA(); constructDFA(); printDFATransTable(); destroyNFA(); destroyDFA(); destroyGraph(); }