Skip to content

Instantly share code, notes, and snippets.

@frakw
Last active December 9, 2020 11:51
Show Gist options
  • Select an option

  • Save frakw/e077d17e3342dc84d0b165b71088171a to your computer and use it in GitHub Desktop.

Select an option

Save frakw/e077d17e3342dc84d0b165b71088171a to your computer and use it in GitHub Desktop.
digraph STG {
rankdir=LR;
INIT [shape=point];
a [label="a"];
b [label="b"];
c [label="c"];
d [label="d"];
e [label="e"];
f [label="f"];
INIT -> a;
a -> c [label="00/0,01/1"];
a -> b [label="10/1"];
a -> e [label="11/0"];
b -> b [label="00/1"];
b -> f [label="01/1"];
b -> d [label="10/1"];
b -> e [label="11/1"];
c -> d [label="00/0,10/1,11/0"];
c -> a [label="01/0"];
d -> e [label="00/1"];
d -> b [label="01/1"];
d -> a [label="10/0"];
d -> c [label="11/0"];
e -> b [label="00/0,10/1"];
e -> a [label="01/1"];
e -> c [label="11/1"];
f -> b [label="00/0,10/1"];
f -> a [label="01/1"];
f -> c [label="11/1"];
}
.start_kiss
.i 2
.o 1
.p 24
.s 6
.r a
00 a c 0
01 a c 1
10 a b 1
11 a e 0
00 b b 1
01 b f 1
10 b d 1
11 b e 1
00 c d 0
01 c a 0
10 c d 1
11 c d 0
00 d e 1
01 d b 1
10 d a 0
11 d c 0
00 e b 0
01 e a 1
10 e b 1
11 e c 1
00 f b 0
01 f a 1
10 f b 1
11 f c 1
.end_kiss
digraph STG {
rankdir=LR;
INIT [shape=point];
a [label="a"];
b [label="b"];
c [label="c"];
d [label="d"];
e [label="e"];
INIT -> a;
a -> c [label="00/0,01/1"];
a -> b [label="10/1"];
a -> e [label="11/0"];
b -> b [label="00/1"];
b -> e [label="01/1,11/1"];
b -> d [label="10/1"];
c -> d [label="00/0,10/1,11/0"];
c -> a [label="01/0"];
d -> e [label="00/1"];
d -> b [label="01/1"];
d -> a [label="10/0"];
d -> c [label="11/0"];
e -> b [label="00/0,10/1"];
e -> a [label="01/1"];
e -> c [label="11/1"];
}
.start_kiss
.i 2
.o 1
.p 20
.s 5
.r a
00 a c 0
01 a c 1
10 a b 1
11 a e 0
00 b b 1
01 b e 1
10 b d 1
11 b e 1
00 c d 0
01 c a 0
10 c d 1
11 c d 0
00 d e 1
01 d b 1
10 d a 0
11 d c 0
00 e b 0
01 e a 1
10 e b 1
11 e c 1
.end_kiss
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <utility>
#define CMD
using namespace std;
struct TERM {
string input;
string current_state;
string next_state;
string output;
bool operator==(const TERM& i) const{
//return (input == i.input) && (output == i.output) && (next_state == i.next_state);//wrong compare!!!
return (input == i.input) && (output == i.output) && ((next_state == i.next_state) || (next_state == i.current_state && i.next_state == current_state));
}
bool operator!=(const TERM& i) const{
return !(*this == i);
}
};
typedef vector<TERM> STATE;
bool operator==(const STATE& A, const STATE& B) {
for (int i = 0; i < A.size(); i++) {
bool flag = false;
for (int j = 0; j < B.size(); j++) {
if (A[i] == B[j]) {
flag = true;
break;
}
}
if (!flag) return false;
}
return true;
}
class KISS {
public:
int input_num,output_num,term_num,state_num;
string start_state;
map<string,STATE> states;
void Minimization() {
for (auto i = states.begin();i != states.end();++i) {
for (auto j = next(i);j != states.end(); ++j) {
if (i->second == j->second){
if (start_state == j->first) start_state = i->first;
for (auto& k : states){
for (auto& m : k.second){
if (m.next_state == j->first) m.next_state = i->first;
}
}
states.erase(j);
Minimization();
return;
}
}
}
}
void output_kiss(ofstream& output) {
output << ".start_kiss\n";
output << ".i " << input_num << '\n';
output << ".o " << output_num << '\n';
output << ".p " << states.size() * (1 << input_num) << '\n';
output << ".s " << states.size() << '\n';
output << ".r " << start_state << '\n';
for (auto& i : states) {
for (auto& j : i.second) {
output << j.input << ' ' << i.first << ' ' << j.next_state << ' ' << j.output << '\n';
}
}
output << ".end_kiss";
}
void output_dot(ofstream& dot) {
dot << "digraph STG {\n\trankdir=LR;\n\n\tINIT [shape=point];\n";
for (auto& i : states) {
dot << '\t' << i.first << " [label=\"" << i.first << "\"];\n";
}
dot << "\n\n\tINIT -> " << start_state << ";\n";
for (auto& i : states) {
for (auto j = i.second.begin();j != i.second.end();j++) {
dot << '\t' << i.first << " -> " << j->next_state << " [label=\"" << j->input << '/' << j->output;
for (auto k = next(j);k != i.second.end();k++) {
if (j->next_state == k->next_state) {
dot << "," << k->input << '/' << k->output;
i.second.erase(k--);
}
}
dot << "\"];\n";
}
}
dot << '}';
}
};
int main(int argc, char* argv[]) {
#ifdef CMD
if (argc != 4) {
cout << "command error!" << endl;
return 0;
}
ifstream input(argv[1]);//argv[0] 是本程式名稱
ofstream output(argv[2]);
ofstream dot(argv[3]);
#else
ifstream input("input.kiss");
ofstream output("output.kiss");
ofstream dot("output.dot");
#endif // CMD
KISS kiss;
string word;
bool start = false;
while (input >> word) {
if (word == ".start_kiss") start = true;
else if (word == ".end_kiss") {
kiss.Minimization();
break;
}
if (start) {
int tmpi;
if (word == ".i") {
input >> kiss.input_num;
}
else if (word == ".o") {
input >> kiss.output_num;
}
else if (word == ".p") {
input >> kiss.term_num;
}
else if (word == ".s") {
input >> kiss.state_num;
}
else if (word == ".r") {
input >> kiss.start_state;
TERM tmp;
for (int i = 0;i < kiss.term_num;i++) {
input >> tmp.input >> tmp.current_state >> tmp.next_state >> tmp.output;
kiss.states[tmp.current_state].push_back(tmp);
}
//KISS before_Minimization = kiss;
//before_Minimization.output_dot(dot);
}
}
}
kiss.output_kiss(output);
kiss.output_dot(dot);
input.close();
output.close();
dot.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment