Пример #1
0
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;
}
Пример #2
0
int verifySubstring(char* str, State* state)
{
    int i,j, size = strlen(str), ok=0;
    for(i=0;i<size;i++)
    {
        for(j=0;j<size;j++)
        {
            ok = ok || runNFA(state,getSubstring(str,i,j),state->last);
        }
    }
    return ok;
}