Skip to content

Commit

Permalink
Fixed a bug with final states in minimization
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammedAbdelbarry committed Mar 23, 2018
1 parent 93a6df1 commit 2034685
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
28 changes: 26 additions & 2 deletions dfa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,26 @@ machine build_dfa(machine &org_dfa, std::vector<std::vector<sid_t> > sets, sid_t
for (int i = 0 ; i < sets.size() ; i++) {
if (i != starting_set) {
token_type = get_token_type(sets[i], org_dfa, is_final);
dfa.add_new_state(token_type, is_final);
dfa.add_new_state(token_type, false, is_final);
}
}


for (int i = 0 ; i < sets.size() ; i++) {
std::vector<sid_t> set = sets[i];
for (char input : org_dfa.get_language()) {
for (sid_t state : set) {
std::vector<sid_t> transitions = org_dfa.get_transitions(state, input);
if (transitions.empty())
continue;
int to = find_set(sets, transitions[0]);
if (to == -1) {
std::cerr << "Internal Error: target can't be -1" << std::endl;
return dfa;
}
dfa.add_new_transition(i + 1, to + 1, input);
break;
}
}
}
return dfa;
Expand All @@ -253,6 +272,11 @@ machine dfa::minimize_dfa(machine& dfa) {
for (sid_t s = 1 ; s <= dfa.get_states_count() ; s++) {
std::string token_type = dfa.get_token_class(s);
// std::cout << s << ": " << token_type << std::endl;
// TODO: remove this;
if (!dfa.is_accepting(s)) {
dfa.set_token_class(s, "notacc");
token_type = "notacc";
}
if (tokenIdx.find(token_type) == tokenIdx.end()) {
tokenIdx[token_type] = count++;
std::vector<sid_t> temp;
Expand All @@ -273,6 +297,6 @@ machine dfa::minimize_dfa(machine& dfa) {
// std::cout << "267: \n";
// print_partitions(cur_set);
}
print_partitions(cur_set);
// print_partitions(cur_set);
return build_dfa(dfa, cur_set, starting_state);
}
11 changes: 11 additions & 0 deletions machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ void machine::state::set_key(std::string _key) {
key = _key;
}

void machine::state::set_token_class(std::string new_token_class) {
token_class = new_token_class;
}

std::vector<int> machine::state::get_transitions_for(char input) {
std::vector<int> t;
for (machine::state::transition &trans : transitions[input]) {
Expand Down Expand Up @@ -150,6 +154,13 @@ void machine::set_key_for(sid_t id, std::string new_key) {
states[id - 1].set_key(new_key);
}

bool machine::set_token_class(sid_t id, std::string new_token_class) {
if (id < 1 && id > states.size())
return false;
states[id - 1].set_token_class(new_token_class);
return true;
}

std::set<char> machine::get_language() const {
return language;
}
Expand Down
5 changes: 5 additions & 0 deletions machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class machine {

void set_key(std::string key);

void set_token_class(std::string token_class);

std::string get_key() const;

std::vector<sid_t> get_transitions_for(char input);
Expand Down Expand Up @@ -94,11 +96,14 @@ class machine {

void set_key_for(sid_t id, std::string new_key);


std::set<char> get_language() const;

sid_t get_states_count() const;

bool set_accepting(sid_t id);

bool set_token_class(sid_t id, std::string new_token_class);

bool is_accepting(sid_t id);

Expand Down
6 changes: 3 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ int main ()

rexparser rx;

// machine nfa = rx.rules2nfa ("letter = a-z | A-Z\ndigit = 0-9\nid: letter (letter|digit)*");
machine nfa = rx.rules2nfa("letter: a+");
machine nfa = rx.rules2nfa ("letter = a-z | A-Z\ndigit = 0-9\nid: letter (letter|digit)*");
// machine nfa = rx.rules2nfa("letter: a*");
// for (int s = 1 ; s <= nfa.get_states_count() ; s++) {
// cout << s << ": " << nfa.get_token_class(s) << std::endl;
// }
// nfa.print_machine();
machine dfa = dfa::to_dfa(nfa);
// dfa.print_machine();
machine min_dfa = dfa::minimize_dfa(dfa);
// min_dfa.print_machine();
min_dfa.print_machine();
//rx.rules2nfa ("id: a b*").print_machine ();

return 0;
Expand Down
2 changes: 1 addition & 1 deletion make.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
g++ -ggdb -std=c++11 *.cpp
g++ -ggdb -std=c++11 *.h *.cpp
./a.out > example.dot
dot -Tpng example.dot -o example.png
xdg-open example.png

0 comments on commit 2034685

Please sign in to comment.