Skip to content

Commit

Permalink
Merge pull request #5 from kpdyer/kpdyer-issue2
Browse files Browse the repository at this point in the history
Expose C++ API
  • Loading branch information
kpdyer committed Jun 19, 2014
2 parents ea4633b + 46db24e commit 0dc9fe6
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 57 deletions.
5 changes: 3 additions & 2 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ RE2_INC_DIR = $(RE2_DIR)

OPTIMIZATION_FLAGS = -O3
CXXFLAGS_ = $(CXXFLAGS) $(OPTIMIZATION_FLAGS) -std=c++0x -DUSE_CXX0X -Wall -I. -Isrc -I$(THIRDPARTY_DIR) -I$(RE2_INC_DIR) -I$(FST_INC_DIR)
LDFLAGS_ = $(LDFLAGS) $(OPTIMIZATION_FLAGS) -L$(RE2_LIB_DIR) -L$(FST_LIB_DIR) -L$(FSTSCRIPT_LIB_DIR) -pthread -lre2 -lfst -lfstscript -ldl
LDFLAGS_ = $(LDFLAGS) $(OPTIMIZATION_FLAGS) -L$(RE2_LIB_DIR) -L$(FST_LIB_DIR) -L$(FSTSCRIPT_LIB_DIR) -lre2 -lfst -lfstscript -ldl

OBJ_REGEX2DFA = src/regex2dfa.o
OBJ_REGEX2DFA = src/regex2dfa.o src/main.o

TARGET_REGEX2DFA = bin/regex2dfa
TARGET_LIBRE2 = $(RE2_LIB_DIR)/libre2.a
Expand Down Expand Up @@ -46,3 +46,4 @@ clean:
cd $(FST_DIR) && $(MAKE) clean
cd $(RE2_DIR) && $(MAKE) clean
rm -f $(TARGET_REGEX2DFA)
rm -vf */*.o
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Once you have your developement environment setup:
$ ./configure
$ make
$ ls bin/
fstcompile fstminimize fstprint regex2dfa
regex2dfa
```

Example Usage
Expand Down
43 changes: 43 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#include <iostream>

#include "regex2dfa.h"

int main (int argc, char **argv) {
extern char *optarg;
char *regex;
int c;
bool regex_set = false;
std::string usage = "usage: %s -r REGEX\n";

while ((c = getopt(argc, argv, "r:")) != -1)
switch (c) {
case 'r':
regex = optarg;
regex_set = true;
break;
}

if (!regex_set) {
std::cerr << usage << std::endl;
exit(1);
}

std::string input_regex = std::string(regex);
std::string minimized_dfa;

bool success = regex2dfa::Regex2Dfa(input_regex, &minimized_dfa);
if (success) {
std::cout << minimized_dfa << std::endl;
} else {
std::cerr << "\033[1;31mERROR\033[0m";
std::cerr << ": Failed to compile regex: \"" + input_regex + "\"";
std::cerr << std::endl;
return 1;
}

return 0;
}
55 changes: 13 additions & 42 deletions src/regex2dfa.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <map>

#include "fst/fstlib.h"
#include "fst/script/fstscript.h"

#include "re2/re2.h"
#include "re2/regexp.h"
#include "re2/prog.h"

#include "regex2dfa.h"

namespace regex2dfa {

std::map< std::string, uint32_t > state_map;
uint32_t state_counter = 0;

Expand Down Expand Up @@ -175,41 +171,16 @@ bool AttFstMinimize(const std::string & str_dfa,
return true;
}

int main (int argc, char **argv) {
extern char *optarg;
char *regex;
int c;
bool regex_set = false;
std::string usage = "usage: %s -r REGEX\n";

while ((c = getopt(argc, argv, "r:")) != -1)
switch (c) {
case 'r':
regex = optarg;
regex_set = true;
break;
}

if (!regex_set) {
std::cerr << usage << std::endl;
exit(1);
}

std::string input_regex = std::string(regex);
bool Regex2Dfa(const std::string & regex,
std::string * minimized_dfa) {
bool success = false;
std::string dfa;
std::string minimized_dfa;

bool compile_success = AttFstFromRegex(input_regex, &dfa);

bool compile_success = AttFstFromRegex(regex, &dfa);
if (compile_success) {
bool minimize_success = AttFstMinimize(dfa, &minimized_dfa);
} else {
std::cerr << "\033[1;31mERROR\033[0m";
std::cerr << ": Failed to compile regex: \"" + input_regex + "\"";
std::cerr << std::endl;
return 1;
bool minimize_success = AttFstMinimize(dfa, minimized_dfa);
success = true;
}

std::cout << minimized_dfa << std::endl;
return 0;
return success;
}

} // namespace regex2dfa
11 changes: 11 additions & 0 deletions src/regex2dfa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _REGEX2DFA_H
#define _REGEX2DFA_H

namespace regex2dfa {

bool Regex2Dfa(const std::string & regex,
std::string * minimized_dfa);

} // namespace regex2dfa

#endif /* _REGEX2DFA_H */
24 changes: 12 additions & 12 deletions third_party/re2/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
#define DCHECK_GT(val1, val2) assert((val1) > (val2))

// Always-on checking
#define CHECK(x) if(x){}else RE2RE2LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
#define CHECK(x) if(x){}else RE2RE2RE2LogMessageFatal(__FILE__, __LINE__).stream() << "Check failed: " #x
#define CHECK_LT(x, y) CHECK((x) < (y))
#define CHECK_GT(x, y) CHECK((x) > (y))
#define CHECK_LE(x, y) CHECK((x) <= (y))
#define CHECK_GE(x, y) CHECK((x) >= (y))
#define CHECK_EQ(x, y) CHECK((x) == (y))
#define CHECK_NE(x, y) CHECK((x) != (y))

#define LOG_INFO RE2RE2LogMessage(__FILE__, __LINE__)
#define LOG_INFO RE2RE2RE2LogMessage(__FILE__, __LINE__)
#define LOG_ERROR LOG_INFO
#define LOG_WARNING LOG_INFO
#define LOG_FATAL RE2RE2LogMessageFatal(__FILE__, __LINE__)
#define LOG_FATAL RE2RE2RE2LogMessageFatal(__FILE__, __LINE__)
#define LOG_QFATAL LOG_FATAL

#define VLOG(x) if((x)>0){}else LOG_INFO.stream()
Expand All @@ -46,9 +46,9 @@

#define LOG(severity) LOG_ ## severity.stream()

class RE2RE2LogMessage {
class RE2RE2RE2LogMessage {
public:
RE2RE2LogMessage(const char* file, int line) : flushed_(false) {
RE2RE2RE2LogMessage(const char* file, int line) : flushed_(false) {
stream() << file << ":" << line << ": ";
}
void Flush() {
Expand All @@ -58,7 +58,7 @@ class RE2RE2LogMessage {
if(write(2, s.data(), n) < 0) {} // shut up gcc
flushed_ = true;
}
~RE2RE2LogMessage() {
~RE2RE2RE2LogMessage() {
if (!flushed_) {
Flush();
}
Expand All @@ -68,19 +68,19 @@ class RE2RE2LogMessage {
private:
bool flushed_;
std::ostringstream str_;
DISALLOW_EVIL_CONSTRUCTORS(RE2RE2LogMessage);
DISALLOW_EVIL_CONSTRUCTORS(RE2RE2RE2LogMessage);
};

class RE2RE2LogMessageFatal : public RE2RE2LogMessage {
class RE2RE2RE2LogMessageFatal : public RE2RE2RE2LogMessage {
public:
RE2RE2LogMessageFatal(const char* file, int line)
: RE2RE2LogMessage(file, line) { }
~RE2RE2LogMessageFatal() {
RE2RE2RE2LogMessageFatal(const char* file, int line)
: RE2RE2RE2LogMessage(file, line) { }
~RE2RE2RE2LogMessageFatal() {
Flush();
abort();
}
private:
DISALLOW_EVIL_CONSTRUCTORS(RE2RE2LogMessageFatal);
DISALLOW_EVIL_CONSTRUCTORS(RE2RE2RE2LogMessageFatal);
};

#endif // RE2_UTIL_LOGGING_H__

0 comments on commit 0dc9fe6

Please sign in to comment.