-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThompsonConstructor.cpp
More file actions
47 lines (43 loc) · 1.02 KB
/
ThompsonConstructor.cpp
File metadata and controls
47 lines (43 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "ThompsonConstructor.h"
using namespace std;
NFA *ThompsonConstructor::constructChar(char c)
{
NFA *nfa = new NFA("constructed nfa");
State *startState = new State(false);
State *finishState = new State(true);
nfa->addState(startState);
nfa->addState(finishState);
nfa->setStartState(startState);
startState->addTransition(c, finishState);
return nfa;
}
NFA *ThompsonConstructor::construct(RegexNode *ast)
{
NFA *L;
NFA *R;
if (!ast)
return nullptr;
switch (ast->type)
{
case 'c':
return constructChar(ast->value);
break;
case '*':
L = construct(ast->left);
return NFA::kleeneStar(L);
break;
case '|':
L = construct(ast->left);
R = construct(ast->right);
return NFA::alternation(L, R);
break;
case '.':
L = construct(ast->left);
R = construct(ast->right);
return NFA::concatenation(L, R);
break;
default:
return nullptr;
break;
}
}