Skip to content
Snippets Groups Projects
Commit 8da9d69f authored by Jan Trávníček's avatar Jan Trávníček
Browse files

aoptimize.regexp -> atrim2

parent 0df58785
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,16 @@
 
namespace regexp {
 
FormalRegExp RegExpOptimize::optimize( FormalRegExp const & regexp )
{
throw exception::AlibException("Unimplemented");
}
void RegExpOptimize::optimize( FormalRegExpElement & element )
{
throw exception::AlibException("Unimplemented");
}
UnboundedRegExp RegExpOptimize::optimize( UnboundedRegExp const & regexp )
{
UnboundedRegExpElement* optimized = optimize( & regexp.getRegExp( ) );
......
......@@ -15,6 +15,9 @@
#include <regexp/unbounded/UnboundedRegExp.h>
#include <regexp/unbounded/UnboundedRegExpElements.h>
 
#include <regexp/formal/FormalRegExp.h>
#include <regexp/formal/FormalRegExpElements.h>
#include <exception/AlibException.h>
 
namespace regexp {
......@@ -59,6 +62,9 @@ class RegExpOptimize
public:
regexp::UnboundedRegExp optimize( const regexp::UnboundedRegExp & regexp );
void optimize( regexp::UnboundedRegExpElement & regexp );
regexp::FormalRegExp optimize( const regexp::FormalRegExp & regexp );
void optimize( regexp::FormalRegExpElement & regexp );
private:
regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpElement const * const & node );
regexp::UnboundedRegExpElement * optimize( regexp::UnboundedRegExpAlternation const * const & node );
......
......@@ -30,6 +30,7 @@ protected:
 
std::set<alphabet::Symbol> alphabet;
 
public:
/**
* @copydoc FormalRegExpElement::clone() const
*/
......@@ -40,7 +41,6 @@ protected:
*/
virtual RegExpBase* plunder() &&;
 
public:
FormalRegExp();
explicit FormalRegExp(const UnboundedRegExp& other);
FormalRegExp(const std::set<alphabet::Symbol>& alphabet, const FormalRegExpElement& regExp);
......
......@@ -30,6 +30,7 @@ protected:
std::set<alphabet::Symbol> alphabet;
 
public:
/**
* @copydoc UnboundedRegExpElement::clone() const
*/
......@@ -40,7 +41,6 @@ protected:
*/
virtual RegExpBase* plunder() &&;
 
public:
UnboundedRegExp();
explicit UnboundedRegExp(const FormalRegExp& other);
UnboundedRegExp(const std::set<alphabet::Symbol>& alphabet, const UnboundedRegExpElement& regExp);
......
CC=g++
EXECUTABLE=aoptimize.regexp
CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib/src -I../libaregexptree/src -I/usr/include/libxml2
LDFLAGS= -L../alib/lib -L../libaregexptree/lib -lxml2 -laregexptree -lalib -Wl,-rpath,.
SOURCES=$(shell find src/ -name *cpp)
OBJECTS=$(patsubst src/%.cpp, obj/%.o, $(SOURCES))
all: $(SOURCES) bin/$(EXECUTABLE)
bin/$(EXECUTABLE): $(OBJECTS)
mkdir -p bin
$(CC) $(OBJECTS) -o $@ $(LDFLAGS)
obj/%.o: src/%.cpp
mkdir -p $(dir $@)
$(CC) $(CCFLAGS) $< -o $@
clean:
$(RM) -r *.o *.d bin obj
#include <iostream>
#include "regexp/RegExpParser.h"
#include "AlibException.h"
#include "sax/SaxInterface.h"
#include "RegExpOptimize.h"
using namespace std;
using namespace regexp;
using namespace sax;
using namespace alib;
int main(int argc, char** argv) {
list<Token> tokens;
if (argc > 1) {
SaxInterface::parseFile(argv[1], tokens);
} else {
string input(istreambuf_iterator<char>(cin),
(istreambuf_iterator<char>()));
SaxInterface::parseMemory(input, tokens);
}
RegExp regexp = RegExpParser::parse(tokens);
RegExpOptimize opt;
regexp = opt.optimize(regexp);
regexp.toXML(cout);
return 0;
}
......@@ -6,15 +6,17 @@
 
#include "trim/grammar/TrimCFG.h"
#include "trim/automaton/TrimFSM.h"
#include "regexp/RegExpOptimize.h"
 
void help( void ) {
std::cout << "atrim 0.01" << std::endl;
std::cout << "Removes unreachable and useless states from FSM, productive and unreachable nonterminals from CFG." << std::endl;
std::cout << "Removes unreachable and useless states from FSM, productive and unreachable nonterminals from CFG. Simplifies representation of RE" << std::endl;
std::cout << "Usage: atrim [-u] [-r]" << std::endl << std::endl;
std::cout << "If neither --useless nor --unreachable option is used, both useless and unreachable states (or symbols) are removed." << std::endl;
std::cout << std::endl;
std::cout << " -u, --useless \t Removes useless states only (works with FSM only)." << std::endl;
std::cout << " -r, --unreachable \t Removes unreachable states only. (works with FSM or CFG)." << std::endl;
std::cout << " -u, --useless \t Removes useless states. (works with FSM only)" << std::endl;
std::cout << " -r, --unreachable \t Removes unreachable states. (works with FSM or CFG)" << std::endl;
std::cout << " -s, --simplify \t Simplifies representation. (works with RE)" << std::endl;
std::cout << " -h, --help \t shows this." << std::endl;
std::cout << std::endl;
}
......@@ -102,6 +104,32 @@ automaton::Automaton trimAutomaton(const automaton::Automaton& g, bool del_unrea
throw exception::AlibException("Unsupported automaton type");
}
 
template<typename T>
regexp::RegExpBase* dynamicOptimize(const regexp::RegExp& r) {
const T* rp = dynamic_cast<const T*>(&r.getData());
if(rp) {
T res(*rp);
regexp::RegExpOptimize opt;
res = opt.optimize( res );
return std::move(res).plunder();
} else {
return NULL;
}
}
regexp::RegExp optimizeRegExp(const regexp::RegExp& r) {
regexp::RegExpBase* res = NULL;
res = dynamicOptimize<regexp::UnboundedRegExp>(r);
if(res) return regexp::RegExp(*res);
res = dynamicOptimize<regexp::FormalRegExp>(r);
if(res) return regexp::RegExp(*res);
throw exception::AlibException("Unsupported automaton type");
}
int main(int argc, char* argv[]) {
bool del_useless = false, del_unreachables = false;
 
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment