-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.cpp
123 lines (93 loc) · 3.32 KB
/
state.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// ****************************************************************************
// ****************************************************************************
// state.cpp
// ****************************************************************************
//
// ****************************************************************************
// ****************************************************************************
// ****************************************************************************
// Includes
// ****************************************************************************
#include "common.h"
// ****************************************************************************
// Static Member Initialization
// ****************************************************************************
UINT State::m_nID = 0;
comVector<State*> State::m_states;
// ****************************************************************************
// State()
// ****************************************************************************
State::State(const comString& name)
: m_finalization(Token::NONE),
m_num(m_nID++),
m_name(name)
{
m_states.append(this);
}
State::State(const comString& name,
Token::Type type)
: m_finalization(type),
m_num(m_nID++),
m_name(name)
{
m_states.append(this);
}
// ****************************************************************************
// hasFinalization()
// ****************************************************************************
bool
State::hasFinalization() const
{
return m_finalization != Token::NONE;
}
// ****************************************************************************
// addTransition()
// ****************************************************************************
void
State::addTransition(char chr,
State* state)
{
m_transitions.append(new Transition(chr, state));
}
// ****************************************************************************
// transition()
// ****************************************************************************
const State*
State::transition(char chr) const
{
for (UINT i = 0; i < m_transitions.getNumEntries(); i++) {
if (m_transitions[i]->m_char == chr)
return m_transitions[i]->m_state;
}
return NULL;
}
// ****************************************************************************
// Transition()
// ****************************************************************************
Transition::Transition(char chr,
State* state)
: m_char(chr),
m_state(state)
{
}
// ****************************************************************************
// State::dumpFSA()
//
// This method prints out the states and their transition which compose the FSA
// constructed for the lexer.
// ****************************************************************************
void
State::dumpFSA()
{
FILE* file = fopen("fsa.out", "w");
for (UINT i = 0; i < m_states.getNumEntries(); i++) {
State* state = m_states[i];
fprintf(file, "\"%s\" (#%d):\n", (const char*) state->m_name, state->m_num);
if (state->hasFinalization())
fprintf(file, " F: %s\n", (const char*) Token::getTypeName(state->m_finalization));
for (UINT j = 0; j < state->m_transitions.getNumEntries(); j++) {
Transition* t = state->m_transitions[j];
fprintf(file, "\t-'%c'-> \"%s\" (%d)\n", t->m_char, (const char*) t->m_state->m_name, t->m_state->m_num);
}
}
}