From 6fe448860ed29fcddd00fc936e53de3e858cba6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <tomaspecka@gmail.com> Date: Sun, 15 Dec 2013 16:29:26 +0100 Subject: [PATCH] Revert weird merge. --- aconversions/src/fsm2rg/FSM2RGConvert.cpp | 145 ---------------------- aconversions/src/fsm2rg/FSM2RGConvert.h | 30 ----- aconversions/src/fsm2rg/afsm2rg.cpp | 54 -------- aconversions/src/rg2fsm/RG2FSMConvert.cpp | 86 ------------- aconversions/src/rg2fsm/RG2FSMConvert.h | 27 ---- aconversions/src/rg2fsm/arg2fsm.cpp | 56 --------- 6 files changed, 398 deletions(-) delete mode 100644 aconversions/src/fsm2rg/FSM2RGConvert.cpp delete mode 100644 aconversions/src/fsm2rg/FSM2RGConvert.h delete mode 100644 aconversions/src/fsm2rg/afsm2rg.cpp delete mode 100644 aconversions/src/rg2fsm/RG2FSMConvert.cpp delete mode 100644 aconversions/src/rg2fsm/RG2FSMConvert.h delete mode 100644 aconversions/src/rg2fsm/arg2fsm.cpp diff --git a/aconversions/src/fsm2rg/FSM2RGConvert.cpp b/aconversions/src/fsm2rg/FSM2RGConvert.cpp deleted file mode 100644 index 33c93d5021..0000000000 --- a/aconversions/src/fsm2rg/FSM2RGConvert.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * FSM2RGConvert.cpp - * - * Created on: 10. 11. 2013 - * Author: tomas - */ - -#include <algorithm> -#include "FSM2RGConvert.h" - -#include <iostream> -#include <cassert> - -/* - * implements algorithm 2.104 in "Melichar, B. Jazyky a překlady. Praha, Vydavatelstvà ČVUT 2007." - * Input: NFA M = (Q, T, \delta, q_0, F) - * Output: Right regular grammar G=(N,T,P,S), L(M)=L(G) - */ - -// Check whether the nonterminal is on right side of any rule. -bool FSM2RGConvert::isNonterminalOnRightSide( const set<Rule> & rules, const Symbol & nonterminal ) -{ - for( set<Rule>::const_iterator i = rules.begin(); i!= rules.end(); i++ ) - { - list<Symbol> leftSide = i -> getLeftSide(); - - list<Symbol>::const_iterator onRightSide = find( leftSide.begin(), leftSide.end(), nonterminal ); - if ( onRightSide != leftSide.end() ) - return true; - } - - return false; -} - - -const Symbol& FSM2RGConvert::getSymbol( const set<Symbol> & symbolSet, const string& symbol ) -{ - for( set<Symbol>::const_iterator i = symbolSet.begin(); i != symbolSet.end(); i++ ) - { - if ( i->getSymbol() == symbol ) - return *i; - } - - assert(false); - return *symbolSet.begin(); -} - - -RightRegularGrammar * FSM2RGConvert::convert( FSM* automaton ) -{ - RightRegularGrammar* regularGrammar = new RightRegularGrammar(); - - // step 1 - init T and N in G - for( set<Symbol>::const_iterator i = automaton -> getInputAlphabet().begin(); i != automaton -> getInputAlphabet().end(); i++ ) - regularGrammar -> addTerminalSymbol( *i ); - - for( set<State>::const_iterator i = automaton -> getStates().begin(); i != automaton -> getStates().end(); i++) - regularGrammar -> addNonTerminalSymbol( Symbol( i->getName() ) ); - - - // step 2 - create set of P in G - const set<Symbol>& nonTerminals = regularGrammar -> getNonTerminalSymbols(); - const set<Symbol>& terminals = regularGrammar -> getTerminalSymbols(); - - for( set<TransitionFSM>::const_iterator i = automaton -> getTransitions().begin(); i != automaton -> getTransitions().end(); i++ ) - { - Rule r; - list<Symbol> leftSide, rightSide; - - // 2a - leftSide.push_back( getSymbol ( nonTerminals, i->getFrom().getName() )); - rightSide.push_back( getSymbol ( terminals, i->getInput().getSymbol() )); - rightSide.push_back( getSymbol ( nonTerminals, i->getTo().getName() )); - - r . setLeftSide( leftSide ); - r . setRightSide( rightSide ); - regularGrammar -> addRule(r); - - // 2b - if ( automaton -> getFinalStates() . find( i-> getTo()) != automaton->getFinalStates().end() ) - { - Rule r; - list<Symbol> leftSide, rightSide; - - leftSide.push_back( getSymbol( nonTerminals, i->getFrom().getName() )); - rightSide.push_back( getSymbol( terminals, i->getInput().getSymbol() )); - r.setLeftSide( leftSide ); - r.setRightSide( rightSide ); - - regularGrammar -> addRule(r); - } - } - - // step 3 - Set S in G - assert( automaton->getInitialStates().size() == 1 ); // TODO: What if... FSM has more than 1 initial state? - regularGrammar-> setStartSymbol( Symbol( automaton->getInitialStates().begin() -> getName() )); - - // step 4 - if ( automaton->getFinalStates() . find( State(regularGrammar -> getStartSymbol().getSymbol())) != automaton->getFinalStates().end() ) - { - regularGrammar -> addTerminalSymbol( Symbol( "" )); - // 4a - if ( ! isNonterminalOnRightSide( regularGrammar -> getRules(), regularGrammar -> getStartSymbol() ) ) - { - Rule r; - list<Symbol> leftSide, rightSide; - - leftSide.push_back( regularGrammar -> getStartSymbol() ); - rightSide.push_back( Symbol( "" ) ); - - regularGrammar -> addRule(r); - } - else // 4b - { - Symbol newStart(regularGrammar->getStartSymbol().getSymbol() + "'"); //TODO: unique name - regularGrammar -> addNonTerminalSymbol( newStart ); - - for( set<Rule>::const_iterator i = regularGrammar->getRules().begin(); i != regularGrammar->getRules().end(); i++ ) - { - if( *(i->getLeftSide().begin()) == regularGrammar->getStartSymbol()) - { - Rule r; - list<Symbol> leftSide, rightSide( i->getRightSide() ); - leftSide.push_back( newStart ); - - r.setLeftSide( leftSide ); - r.setRightSide( rightSide ); - regularGrammar -> addRule(r); - } - } - regularGrammar -> setStartSymbol( newStart ); - - Rule rr; - list<Symbol> leftSide, rightSide; - leftSide.push_back( newStart ); - rightSide.push_back( Symbol( "" ) ); - rr.setLeftSide( leftSide ); - rr.setRightSide( rightSide ); - - regularGrammar-> addRule(rr); - } - } - - return regularGrammar; -} diff --git a/aconversions/src/fsm2rg/FSM2RGConvert.h b/aconversions/src/fsm2rg/FSM2RGConvert.h deleted file mode 100644 index 7307b8c5d5..0000000000 --- a/aconversions/src/fsm2rg/FSM2RGConvert.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * FSM2RGConvert.h - * - * Created on: 10. 11. 2013 - * Author: Tomas Pecka - */ - -//FIXME Find better name than fsm2rg ! - -#ifndef FSM2RGCONVERT_H_ -#define FSM2RGCONVERT_H_ - -#include "automaton/FSM/FSM.h" -#include "grammar/RightRegularGrammar.h" - -using namespace std; -using namespace automaton; -using namespace grammar; - -class FSM2RGConvert -{ -public: - static RightRegularGrammar* convert( FSM* automaton ); - -protected: - static bool isNonterminalOnRightSide( const set<Rule> & rules, const Symbol & nonterminal ); - static const Symbol& getSymbol( const set<Symbol> & symbolSet, const string& symbol ); -}; - -#endif /* FSM2RGCONVERT_H_ */ diff --git a/aconversions/src/fsm2rg/afsm2rg.cpp b/aconversions/src/fsm2rg/afsm2rg.cpp deleted file mode 100644 index 37af23c730..0000000000 --- a/aconversions/src/fsm2rg/afsm2rg.cpp +++ /dev/null @@ -1,54 +0,0 @@ -//============================================================================ -// Name : afsm2rg.cpp -// Author : Tomas Pecka -// Version : -// Copyright : -// Description : -//============================================================================ - -#include <iostream> - -#include "automaton/UnknownAutomaton.h" -#include "AutomatonFactory.h" -#include "AlibException.h" - -#include "FSM2RGConvert.h" -#include "grammar/GrammarPrinter.h" - -using namespace std; -using namespace automaton; -using namespace alib; - -int main(int argc, char** argv) -{ - UnknownAutomaton automaton; - - try - { - if (argc > 1) - { - if (string("-h").compare(argv[1]) == 0) - { - cout << "Converts FSM to right regular grammar." << endl; - cout << "Usage: afsm2rg [automaton.xml]" << endl; - return -1; - } - - automaton = AutomatonFactory::fromFile(argv[1]); - } - else - { - string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>())); - automaton = AutomatonFactory::fromString(input); - } - - Automaton* knownAutomaton = AutomatonFactory::buildAutomaton(&automaton); - Grammar* rg = FSM2RGConvert::convert( dynamic_cast<FSM*>(knownAutomaton) ); - GrammarPrinter::toXML(*rg, cout); - } - catch (AlibException& e) - { - cout << e.what() << endl; - return 0; - } -} diff --git a/aconversions/src/rg2fsm/RG2FSMConvert.cpp b/aconversions/src/rg2fsm/RG2FSMConvert.cpp deleted file mode 100644 index 7b10ecbc35..0000000000 --- a/aconversions/src/rg2fsm/RG2FSMConvert.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * FSM2RGConvert.cpp - * - * Created on: 10. 11. 2013 - * Author: tomas - */ - -#include <algorithm> -#include "RG2FSMConvert.h" - -#include <iostream> -#include <cassert> - -/* - * implements 2.98 in "[2] Melichar, B. Jazyky a překlady. Praha, Vydavatelstvà ČVUT 2007." - * Input: Right regular grammar G=(N,T,P,S) - * Output: NFA M = (Q, T, \delta, q_0, F) , L(M)=L(G) - */ - -// Check whether the nonterminal is on right side of any rule. -const Symbol& RG2FSMConvert::getSymbol( const set<Symbol> & symbolSet, const string& symbol ) -{ - for( set<Symbol>::const_iterator i = symbolSet.begin(); i != symbolSet.end(); i++ ) - { - if ( i->getSymbol() == symbol ) - return *i; - } - - assert(false); - return *symbolSet.begin(); -} - - -FSM * RG2FSMConvert::convert( RightRegularGrammar* grammar ) -{ - FSM* automaton = new FSM(); - - // step 1 - for( set<Symbol>::const_iterator i = grammar -> getTerminalSymbols().begin(); i != grammar -> getTerminalSymbols().end(); i++ ) - automaton -> addInputSymbol( *i ); - - // step 2 - for( set<Symbol>::const_iterator i = grammar -> getNonTerminalSymbols().begin(); i != grammar -> getNonTerminalSymbols().end(); i++ ) - automaton -> addState( State( i->getSymbol() ) ); - - State AState( "o____o" ); // TODO: unique name - automaton -> addState( AState ); - - // step 3, constructing \delta - for( set<Rule>::const_iterator i = grammar -> getRules().begin(); i != grammar -> getRules().end(); i++ ) - { - State current( i->getLeftSide().begin()->getSymbol() ); - Symbol input( i->getRightSide().begin()->getSymbol() ); - - // if B->a => \delta(B,a)=AState - if( i->getRightSide().size() == 1 ) - automaton->addTransition( current, input, AState ); - - // if B->aC => \delta(B,a)=C - if( i->getRightSide().size() == 2 ) - { - list<Symbol>::const_iterator it = i->getRightSide().begin(); - it ++; - State next( it->getSymbol() ); - automaton->addTransition( current, input, next ); - } - - } - - // step 4 - automaton -> addInitialState( State( grammar->getStartSymbol().getSymbol()) ); - - // step 5 - automaton -> addFinalState( AState ); - for( set<Rule>::const_iterator i = grammar->getRules().begin(); i != grammar->getRules().end(); i++ ) - { - // if S->\epsilon - if( i->getLeftSide().size() == 1 && *( i->getLeftSide().begin() ) == grammar->getStartSymbol() && - i->getRightSide().size() == 1 && *( i->getRightSide().begin()) == Symbol("") ) - { - automaton -> addFinalState( State( grammar->getStartSymbol().getSymbol() )); - } - } - - return automaton; -} diff --git a/aconversions/src/rg2fsm/RG2FSMConvert.h b/aconversions/src/rg2fsm/RG2FSMConvert.h deleted file mode 100644 index 0e11ce44ce..0000000000 --- a/aconversions/src/rg2fsm/RG2FSMConvert.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * RG2FSMConvert.h - * - * Created on: 10. 11. 2013 - * Author: Tomas Pecka - */ - -#ifndef RG2FSMCONVERT_H_ -#define RG2FSMCONVERT_H_ - -#include "automaton/FSM/FSM.h" -#include "grammar/RightRegularGrammar.h" - -using namespace std; -using namespace automaton; -using namespace grammar; - -class RG2FSMConvert -{ -public: - static FSM* convert( RightRegularGrammar* grammar ); - -protected: - static const Symbol& getSymbol( const set<Symbol> & symbolSet, const string& symbol ); -}; - -#endif /* RG2FSMCONVERT_H_ */ diff --git a/aconversions/src/rg2fsm/arg2fsm.cpp b/aconversions/src/rg2fsm/arg2fsm.cpp deleted file mode 100644 index 48bec1d747..0000000000 --- a/aconversions/src/rg2fsm/arg2fsm.cpp +++ /dev/null @@ -1,56 +0,0 @@ -//============================================================================ -// Name : afsm2rg.cpp -// Author : Tomas Pecka -// Version : -// Copyright : -// Description : -//============================================================================ - -#include <iostream> - -#include "grammar/UnknownGrammar.h" -#include "GrammarFactory.h" -#include "AlibException.h" - -#include "automaton/AutomatonPrinter.h" - -#include "RG2FSMConvert.h" - -using namespace std; -using namespace grammar; -using namespace automaton; -using namespace alib; - -int main(int argc, char** argv) -{ - UnknownGrammar grammar; - - try - { - if (argc > 1) - { - if (string("-h").compare(argv[1]) == 0) - { - cout << "Converts right regular grammar to nfsm." << endl; - cout << "Usage: arg2fsm [grammar.xml]" << endl; - return -1; - } - - grammar = GrammarFactory::fromFile(argv[1]); - } - else - { - string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>())); - grammar = GrammarFactory::fromString(input); - } - - RightRegularGrammar knownGrammar = GrammarFactory::buildRightRegularGrammar(&grammar); - FSM* fsm = RG2FSMConvert::convert( & knownGrammar ); - AutomatonPrinter::toXML(*fsm, cout); - } - catch (AlibException& e) - { - cout << e.what() << endl; - return 0; - } -} -- GitLab