From f17072217bd008ff97c514a0ef948bef65b8af0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <peckato1@fit.cvut.cz> Date: Tue, 18 Mar 2014 18:40:50 +0100 Subject: [PATCH] Changes to atrim - rename atrim to atrim.fsm - Terminology - redundant state is now dead state - allow to remove only unreachable or only dead states (see ./atrim.fsm -h) - remove namespace --- {atrim => atrim.fsm}/.cproject | 2 +- {atrim => atrim.fsm}/.project | 2 +- {atrim => atrim.fsm}/makefile | 2 +- .../src/DeadStateRemover.cpp | 12 +-- .../src/DeadStateRemover.h | 15 ++-- .../src/UnreachableStateRemover.cpp | 6 -- .../src/UnreachableStateRemover.h | 5 -- atrim.fsm/src/atrim.fsm.cpp | 87 +++++++++++++++++++ atrim/src/TrimNFA.cpp | 23 ----- atrim/src/TrimNFA.h | 27 ------ atrim/src/atrim.fsm.cpp | 66 -------------- 11 files changed, 99 insertions(+), 148 deletions(-) rename {atrim => atrim.fsm}/.cproject (98%) rename {atrim => atrim.fsm}/.project (97%) rename {atrim => atrim.fsm}/makefile (95%) rename atrim/src/RedundantStateRemover.cpp => atrim.fsm/src/DeadStateRemover.cpp (88%) rename atrim/src/RedundantStateRemover.h => atrim.fsm/src/DeadStateRemover.h (58%) rename {atrim => atrim.fsm}/src/UnreachableStateRemover.cpp (96%) rename {atrim => atrim.fsm}/src/UnreachableStateRemover.h (93%) create mode 100644 atrim.fsm/src/atrim.fsm.cpp delete mode 100644 atrim/src/TrimNFA.cpp delete mode 100644 atrim/src/TrimNFA.h delete mode 100644 atrim/src/atrim.fsm.cpp diff --git a/atrim/.cproject b/atrim.fsm/.cproject similarity index 98% rename from atrim/.cproject rename to atrim.fsm/.cproject index 6e6d8fdead..3c88f6f08f 100644 --- a/atrim/.cproject +++ b/atrim.fsm/.cproject @@ -27,7 +27,7 @@ <listOptionValue builtIn="false" value="/usr/include/libxml2"/> <listOptionValue builtIn="false" value=""${workspace_loc:/alib/src}""/> </option> - <option id="gnu.cpp.compiler.option.other.other.1520737263" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=c++11" valueType="string"/> + <option id="gnu.cpp.compiler.option.other.other.1520737263" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=c++11" valueType="string"/> <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.236265274" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/> </tool> <tool id="cdt.managedbuild.tool.gnu.c.compiler.exe.debug.667742770" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.exe.debug"> diff --git a/atrim/.project b/atrim.fsm/.project similarity index 97% rename from atrim/.project rename to atrim.fsm/.project index 50b1ef0466..1bd8e07e1d 100644 --- a/atrim/.project +++ b/atrim.fsm/.project @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <projectDescription> - <name>atrim</name> + <name>atrim.fsm</name> <comment></comment> <projects> </projects> diff --git a/atrim/makefile b/atrim.fsm/makefile similarity index 95% rename from atrim/makefile rename to atrim.fsm/makefile index f0d998dec1..a04f7cc8cc 100644 --- a/atrim/makefile +++ b/atrim.fsm/makefile @@ -1,5 +1,5 @@ CC=g++ -EXECUTABLE=atrim +EXECUTABLE=atrim.fsm CCFLAGS= -std=c++11 -O2 -c -Wall -I../alib/src -I/usr/include/libxml2 LDFLAGS= -L../alib/lib -lxml2 -lalib -Wl,-rpath,. diff --git a/atrim/src/RedundantStateRemover.cpp b/atrim.fsm/src/DeadStateRemover.cpp similarity index 88% rename from atrim/src/RedundantStateRemover.cpp rename to atrim.fsm/src/DeadStateRemover.cpp index 409aee5cf7..7045561e12 100644 --- a/atrim/src/RedundantStateRemover.cpp +++ b/atrim.fsm/src/DeadStateRemover.cpp @@ -1,19 +1,16 @@ /* - * RedundantStateRemover.cpp + * DeadStateRemover.cpp * * Created on: 4. 3. 2014 * Author: tomas */ -#include "RedundantStateRemover.h" +#include "DeadStateRemover.h" using namespace automaton; using namespace std; -namespace trim -{ - -FSM RedundantStateRemover::remove( const FSM & fsm ) +FSM DeadStateRemover::remove( const FSM & fsm ) { deque<set<State>> Qi; Qi.push_back( set<State>( ) ); @@ -35,6 +32,7 @@ FSM RedundantStateRemover::remove( const FSM & fsm ) i += 1; } + FSM ret; for( const auto & q : Qi.at( i ) ) @@ -58,5 +56,3 @@ FSM RedundantStateRemover::remove( const FSM & fsm ) return ret; } - -} /* namespace trim */ diff --git a/atrim/src/RedundantStateRemover.h b/atrim.fsm/src/DeadStateRemover.h similarity index 58% rename from atrim/src/RedundantStateRemover.h rename to atrim.fsm/src/DeadStateRemover.h index 3ba2e01149..3ebc1d63db 100644 --- a/atrim/src/RedundantStateRemover.h +++ b/atrim.fsm/src/DeadStateRemover.h @@ -1,32 +1,27 @@ /* - * RedundantStateRemover.h + * DeadStateRemover.h * * Created on: 4. 3. 2014 * Author: tomas */ -#ifndef REDUNDANTSTATEREMOVER_H_ -#define REDUNDANTSTATEREMOVER_H_ +#ifndef DEADSTATEREMOVER_H_ +#define DEADSTATEREMOVER_H_ #include <deque> #include <set> #include <automaton/FSM/FSM.h> -namespace trim -{ - #define isInSet(x,set) ( (set).find((x)) != (set).end()) /** * Melichar 2.32 */ -class RedundantStateRemover +class DeadStateRemover { public: static automaton::FSM remove( const automaton::FSM & fsm ); }; -} /* namespace trim */ - -#endif /* REDUNDANTSTATEREMOVER_H_ */ +#endif /* DEADSTATEREMOVER_H_ */ diff --git a/atrim/src/UnreachableStateRemover.cpp b/atrim.fsm/src/UnreachableStateRemover.cpp similarity index 96% rename from atrim/src/UnreachableStateRemover.cpp rename to atrim.fsm/src/UnreachableStateRemover.cpp index 3caf41fd80..38ddc2eaf4 100644 --- a/atrim/src/UnreachableStateRemover.cpp +++ b/atrim.fsm/src/UnreachableStateRemover.cpp @@ -10,10 +10,6 @@ using namespace alib; using namespace automaton; using namespace std; -using namespace trim; - -namespace trim -{ FSM UnreachableStateRemover::remove( const FSM & fsm ) { @@ -69,5 +65,3 @@ set<State> UnreachableStateRemover::findReachableStates( const FSM & fsm ) return qcurr; } - -} /* namespace trim */ diff --git a/atrim/src/UnreachableStateRemover.h b/atrim.fsm/src/UnreachableStateRemover.h similarity index 93% rename from atrim/src/UnreachableStateRemover.h rename to atrim.fsm/src/UnreachableStateRemover.h index 0345ebbc36..5e5929e7b7 100644 --- a/atrim/src/UnreachableStateRemover.h +++ b/atrim.fsm/src/UnreachableStateRemover.h @@ -16,9 +16,6 @@ #include <map> #include <set> -namespace trim -{ - #define isInSet(x,set) ( (set).find((x)) != (set).end()) /** @@ -33,6 +30,4 @@ private: static std::set<automaton::State> findReachableStates( const automaton::FSM & fsm ); }; -} /* namespace trim */ - #endif /* UNREACHABLE_H_ */ diff --git a/atrim.fsm/src/atrim.fsm.cpp b/atrim.fsm/src/atrim.fsm.cpp new file mode 100644 index 0000000000..798492a1c7 --- /dev/null +++ b/atrim.fsm/src/atrim.fsm.cpp @@ -0,0 +1,87 @@ +#include <iostream> +#include <getopt.h> + +#include <AutomatonFactory.h> +#include <AlibException.h> +#include <automaton/AutomatonParser.h> + +#include <sax/SaxInterface.h> +#include <sax/ParserException.h> + +#include "UnreachableStateRemover.h" +#include "DeadStateRemover.h" + +using namespace std; +using namespace automaton; +using namespace alib; +using namespace sax; + +void help( void ) +{ + cout << "atrim.fsm 0.01" << endl; + cout << "Removes unreachable and dead states from FSM. Input is read from stdin." << endl; + cout << "Usage: atrim [-d] [-u]" << endl << endl; + cout << "If neither --dead nor --unreachable option is used, both dead and unreachable states are removed." << endl; + cout << endl; + cout << " --dead \t Removes dead states." << endl; + cout << " --unreachable \t Removes unreachable states." << endl; + cout << " -h, --help \t shows this." << endl; + + cout << endl; +} + +int main(int argc, char* argv[]) +{ + int del_d = 0, del_u = 0; + + static struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"dead", no_argument, & del_d, 1}, + {"unreachable", no_argument, & del_u, 1}, + {0, 0, 0, 0} + }; + + int long_index = 0, opt = 0; + + while( ( opt = getopt_long( argc, argv, "h", long_options, & long_index ) ) != -1 ) + { + switch( opt ) + { + case 0: + break; + + case 'v': + case 'h': + default: + help( ); + return 0; + } + } + + list<Token> tokens; + if(optind == argc) + { + string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>())); + SaxInterface::parseMemory(input, tokens); + } + else + { + SaxInterface::parseFile(argv[optind],tokens); + } + + + FSM fsm = AutomatonFactory::buildFSM( AutomatonParser::parse(tokens) ); + + // default behaviour, no switches + if( ! del_d && ! del_u ) + del_d = del_u = 1; + + if( del_u ) + fsm = UnreachableStateRemover::remove( fsm ); + if( del_d ) + fsm = DeadStateRemover::remove( fsm ); + + fsm.toXML( cout ); + + return 0; +} diff --git a/atrim/src/TrimNFA.cpp b/atrim/src/TrimNFA.cpp deleted file mode 100644 index b6a184e831..0000000000 --- a/atrim/src/TrimNFA.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * TrimNFA.cpp - * - * Created on: 4. 3. 2014 - * Author: tomas - */ - -#include "TrimNFA.h" - -using namespace automaton; - -namespace trim -{ - -FSM TrimNFA::remove( const FSM & fsm ) -{ - FSM ret; - ret = UnreachableStateRemover::remove( fsm ); - ret = RedundantStateRemover::remove( ret ); - return ret; -} - -} /* namespace trim */ diff --git a/atrim/src/TrimNFA.h b/atrim/src/TrimNFA.h deleted file mode 100644 index 55f21f9e9e..0000000000 --- a/atrim/src/TrimNFA.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * TrimNFA.h - * - * Created on: 4. 3. 2014 - * Author: tomas - */ - -#ifndef TRIMNFA_H_ -#define TRIMNFA_H_ - -#include <automaton/FSM/FSM.h> - -#include "RedundantStateRemover.h" -#include "UnreachableStateRemover.h" - -namespace trim -{ - -class TrimNFA -{ -public: - static automaton::FSM remove( const automaton::FSM & fsm ); -}; - -} /* namespace trim */ - -#endif /* TRIMNFA_H_ */ diff --git a/atrim/src/atrim.fsm.cpp b/atrim/src/atrim.fsm.cpp deleted file mode 100644 index dd33c147fe..0000000000 --- a/atrim/src/atrim.fsm.cpp +++ /dev/null @@ -1,66 +0,0 @@ - #include <iostream> - -#include <AutomatonFactory.h> -#include <AlibException.h> -#include <automaton/AutomatonParser.h> - -#include <sax/SaxInterface.h> -#include <sax/ParserException.h> - -#include "TrimNFA.h" - -using namespace std; -using namespace automaton; -using namespace alib; -using namespace sax; - -using namespace trim; - -int main(int argc, char** argv) { - int fileParameterIndex = -1; - - try { - if( argc > 1 ) - { - for( int i = 1; i < argc; i++ ) - { - if( string( "-h" ).compare( argv[i] ) == 0 ) - { - std::cout << "Removes unreachable states from NFA." << std::endl; - std::cout << "Usage: atrim [automaton.xml]" << std::endl; - return 1; - } - else - { - if(fileParameterIndex == -1) - fileParameterIndex = i; - else - throw AlibException("Only one file can be passed as parameter - " + string(argv[i]) + " " + string(argv[fileParameterIndex])); - } - } - } - - std::list<Token> tokens; - - if(fileParameterIndex != -1) - { - SaxInterface::parseFile(argv[fileParameterIndex],tokens); - } - else - { - string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>())); - SaxInterface::parseMemory(input, tokens); - } - - UnknownAutomaton automaton = AutomatonParser::parse( tokens ); - FSM fsm = AutomatonFactory::buildFSM( automaton ); - - TrimNFA::remove( fsm ).toXML( cout ); - - } catch (AlibException& e) { - cout << e.what() << endl; - return -1; - } - - cout.flush(); -} -- GitLab