-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.cpp
More file actions
83 lines (70 loc) · 1.95 KB
/
State.cpp
File metadata and controls
83 lines (70 loc) · 1.95 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
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
#include "State.h"
#include <iostream>
#include <map>
#include <vector>
#include <stack>
void State::add_edge(char ch, State* next){
if(is_end_state())
set_normal_type();
if(ch == EPSILON)
epsilon_jump_vec.push_back(next);
else if(ch == ANY)
accept_any_char = next;
else
ch_state_map[ch] = next;
}
void State::print(int indent){
for (int i = 0; i<indent; ++i){
std::cout << " ";
}
if(is_end_state())
std::cout << (void*)this << " endstate" << std::endl;
else
std::cout << (void*)this << std::endl;
if(printed == 1)
return;
printed = 1;
for(std::map<char, State*>::iterator it=ch_state_map.begin(); it!=ch_state_map.end(); it++){
for (int i = 0; i<indent+2; ++i){
std::cout << " ";
}
if(it->first == ANY)
std::cout << ".";
else
std::cout << it->first;
std::cout << std::endl;
it->second->print(indent+2);
}
for(std::vector<State*>::iterator iter=epsilon_jump_vec.begin();
iter != epsilon_jump_vec.end();
iter++) {
for (int i = 0; i<indent+2; ++i){
std::cout << " ";
}
std::cout << "E" << std::endl;
(*iter)->print(indent+2);
}
}
int State::next_state(char ch, std::stack<State*> &s){
if(accept_any_char != NULL){
s.push(accept_any_char);
accept_any_char->next_epsilong_state(s);
return 0;
}
std::map<char, State*>::iterator it = ch_state_map.find(ch);
if(it == ch_state_map.end()){
return -1;
}
s.push(it->second);
it->second->next_epsilong_state(s);
return 0;
}
int State::next_epsilong_state(std::stack<State*> &s){
for(std::vector<State*>::iterator it = epsilon_jump_vec.begin();
it != epsilon_jump_vec.end();
it++) {
s.push(*it);
(*it)->next_epsilong_state(s);
}
return 0;
}