Skip to content

Commit

Permalink
initial implementation of machine operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Quarz0 committed Mar 20, 2018
1 parent 708c68d commit 82816d6
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 3 deletions.
11 changes: 8 additions & 3 deletions machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>
#include <map>
#define EPS 0xDE

class state {

Expand All @@ -29,11 +30,12 @@ class state {
int get_state_identifier () const;
std::string get_token_class () const;
bool is_accepting () const;
void set_accepting (bool);
std::vector<transition> get_transitions_for (char in) const;
/*
* Adds a new transition from this state to TO state.
*/
void add_new_transition (state to, char on_input = 0xDE); // 0xDE = Epsilon
void add_new_transition (state to, char on_input = EPS); // 0xDE = Epsilon
};

class machine {
Expand All @@ -45,12 +47,15 @@ class machine {

public:
machine (std::string _machine_identifier);
void add_new_state (int state_identifier, std::string token_class, bool is_accepting = false);
void add_new_transition (state from, state to, char on_input = 0xDE); // 0xDE = Epsilon
machine (const machine& copy);
// void add_new_state (int state_identifier, std::string token_class, bool is_accepting = false);
void add_new_state (state);
void add_new_transition (state from, state to, char on_input = EPS);
void set_starting_state (state *_starting);
state get_starting_state () const;
std::string get_machine_identifier () const;
std::vector<state> get_states () const;
std::vector<state> get_accepting_states () const;
};

#endif //KOMPILER_MACHINE_H
116 changes: 116 additions & 0 deletions rexplib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// Created by Marwan Tammam on 3/20/18.
//

#include "rexplib.h"
#include "machine.h"

machine machine_concat(const machine &a_, const machine &b_) {
machine a = new machine(a_);
machine b = new machine(b_);

for (state &s: a.get_accepting_states()) {
s.set_accepting(false);
s.add_new_transition(b.get_starting_state());
}
for (state &s: b.get_states()) {
a.add_new_state(s);
}
return a;
}

machine machine_or(const machine &a_, const machine &b_) {
machine a = new machine(a_);
machine b = new machine(b_);

state starting_state = new state();
starting_state.set_accepting(false);
starting_state.add_new_transition(a.get_starting_state());
starting_state.add_new_transition(b.get_starting_state());

state ending_state = new state();
starting_state.set_accepting(true);

for (state &s : a.get_accepting_states()) {
s.set_accepting(false);
s.add_new_transition(ending_state);
}
for (state &s : b.get_accepting_states()) {
s.set_accepting(false);
s.add_new_transition(ending_state);
}

for (state &s: b.get_states()) {
a.add_new_state(s);
}

a.add_new_state(starting_state);
a.add_new_state(ending_state);
a.set_starting_state(starting_state);
return a;
}

machine machine_star(const machine &a_) {
machine a = new machine(a_);

state starting_state = new state();
starting_state.set_accepting(false);
state ending_state = new state();
starting_state.set_accepting(true);

starting_state.add_new_transition(ending_state);
starting_state.add_new_transition(a.get_starting_state());

for (state &s: a.get_accepting_states()) {
s.set_accepting(false);
s.add_new_transition(starting_state);
s.add_new_transition(ending_state);
}

a.add_new_state(starting_state);
a.add_new_state(ending_state);
a.set_starting_state(starting_state);

return a;
}

machine machine_plus(const machine &a_) {
return machine_concat(a_, machine_star(a_));
}

machine single_char(char c) {
machine m = new machine(std::string(c));

state starting_state = new state();
starting_state.set_accepting(false);
state ending_state = new state();
starting_state.set_accepting(true);

starting_state.add_new_transition(ending_state, c);
m.add_new_state(starting_state);
m.add_new_state(ending_state);
m.set_starting_state(starting_state);

return m;
}

machine char_range(char start, char end) {
machine m = single_char(start);
for (char c = start + 1; c <= end; c++) {
machine p = single_char(c);
m = machine_or(m, p);
}
return m;
}

machine string_concat(std::string s) {
if (s == "")
return single_char(EPS);

machine m = single_char(s[0]);
for (int i = 1; i < s.length(); i++) {
machine p = single_char(s[i]);
m = machine_concat(m, p);
}
return m;
}
24 changes: 24 additions & 0 deletions rexplib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Created by Marwan Tammam on 3/20/18.
//

#ifndef KOMPILER_REXP_LIB_H
#define KOMPILER_REXP_LIB_H

#include "machine.h"

machine machine_concat(const machine &a_, const machine &b_);

machine machine_or(const machine &a_, const machine &b_);

machine machine_star(const machine &a_);

machine machine_plus(const machine &a_);

machine single_char(char c);

machine char_range(char start, char end);

machine string_concat(std::string s);

#endif //KOMPILER_REXP_LIB_H

0 comments on commit 82816d6

Please sign in to comment.