Skip to content

Commit

Permalink
generate DFA states from NFA
Browse files Browse the repository at this point in the history
  • Loading branch information
Raekye committed Mar 1, 2015
1 parent e3c3a47 commit b4fdbff
Show file tree
Hide file tree
Showing 20 changed files with 2,307 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Each of these directories has a `Makefile` that puts stuff in a `bin/` folder.
- `madoka/`: one of my first passes, arguably my first success I could call a "compiler". Pre 2014-summer
- `sayaka/` (in progress): successor to `madoka/`, had ideas on what to do differently. The ideas were pre 2014-summer, most of the work on it is post 2014-summer
- `primed/`: hand written LL(1) regex parser and NFA state generator
- `siyu/` (in progress): DFA state generator, lexer-generator, and parser-generator
- `siyu/` (in progress): successor to `primed/`, DFA state generator, lexer-generator, and parser-generator

### Siyu
- Successor to `primed/`; working based on the dragon book, trying to follow the "harder, more theoretical" stuff, needed a fresh start :P
- generating DFA states directly from regex
- generate DFA states from NFA states
- `siyu-1/` is a start at generating DFA states directly from a regex based on the dragon book

### Primed
- hand written, recursive descent basic regex parser (builds AST)
Expand Down
1 change: 1 addition & 0 deletions siyu-1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
37 changes: 37 additions & 0 deletions siyu-1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
CXX = g++
LD = g++
CXXFLAGS = -g -std=c++11 -c
LDFLAGS = -g
LDLIBS =

SRC_DIR = src
BIN_DIR = bin

EXECUTABLE = $(BIN_DIR)/siyu

SRCS = $(sort $(wildcard $(SRC_DIR)/*.cpp))
OBJS = $(addsuffix .o,$(subst $(SRC_DIR),$(BIN_DIR),$(SRCS)))
DEPS = $(addsuffix .d,$(subst $(SRC_DIR),$(BIN_DIR),$(SRCS)))

.PHONY: all build clean

all: build

build: $(EXECUTABLE)

clean:
rm -rf $(BIN_DIR)

$(EXECUTABLE): $(OBJS)
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

$(BIN_DIR)/%.o: $(SRC_DIR)/%
$(CXX) $(CXXFLAGS) $< -o $@

$(BIN_DIR):
mkdir -p $@

$(BIN_DIR)/%.d: $(SRC_DIR)/% $(BIN_DIR)
$(CXX) $(CXXFLAGS) -MM $< -MT '$(@:.d=.o)' -MF $@

include $(DEPS)
23 changes: 23 additions & 0 deletions siyu-1/src/finite_automata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "finite_automata.h"

#include <cstdio>

DFAState::DFAState() : terminal(false) {
return;
}

DFA::DFA() {
this->root = this->new_state();
}

DFAState* DFA::new_state() {
std::unique_ptr<DFAState> state(new DFAState());
DFAState* ptr = state.get();
this->id_from_state[ptr] = this->states.size();
this->states.push_back(std::move(state));
return ptr;
}

UInt DFA::id(DFAState* state) {
return this->id_from_state.at(state);
}
27 changes: 27 additions & 0 deletions siyu-1/src/finite_automata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef SIYU_FINITE_AUTOMATA_H_INCLUDED
#define SIYU_FINITE_AUTOMATA_H_INCLUDED

#include <map>
#include <vector>
#include <memory>
#include "global.h"

class DFAState {
public:
std::map<UInt, DFAState*> next_states;
bool terminal;
DFAState();
};

class DFA {
std::map<DFAState*, UInt> id_from_state;
public:
DFAState* root;
std::vector<std::unique_ptr<DFAState>> states;

DFA();
DFAState* new_state();
UInt id(DFAState*);
};

#endif /* SIYU_FINITE_AUTOMATA_H_INCLUDED */
1 change: 1 addition & 0 deletions siyu-1/src/global.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "global.h"
15 changes: 15 additions & 0 deletions siyu-1/src/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef SIYU_GLOBAL_H_INCLUDED
#define SIYU_GLOBAL_H_INCLUDED

#include <cstdint>

typedef int8_t Byte;
typedef uint8_t UByte;
typedef int16_t Short;
typedef uint16_t UShort;
typedef int32_t Int;
typedef uint32_t UInt;
typedef int64_t Long;
typedef uint64_t ULong;

#endif /* SIYU_GLOBAL_H_INCLUDED */
11 changes: 11 additions & 0 deletions siyu-1/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
#include "regex.h"

int main() {
std::string str = "abc|def|ghi{3,5}";
RegexParser parser;
RegexAST* r = parser.parse(str);
RegexASTPrinter printer;
r->accept(&printer);
return 0;
}
Loading

0 comments on commit b4fdbff

Please sign in to comment.