From 9f3ef422680185475c9de838637b41905dae9896 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 2 Feb 2014 10:52:10 +0100 Subject: [PATCH] convert to progtest like format --- aconvert.automaton/src/AutomatonPrinter.cpp | 60 +++++++++++++++++++ aconvert.automaton/src/AutomatonPrinter.h | 17 ++++++ aconvert.automaton/src/aconvert.automaton.cpp | 28 ++++++++- 3 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 aconvert.automaton/src/AutomatonPrinter.cpp create mode 100644 aconvert.automaton/src/AutomatonPrinter.h diff --git a/aconvert.automaton/src/AutomatonPrinter.cpp b/aconvert.automaton/src/AutomatonPrinter.cpp new file mode 100644 index 0000000000..a63c7f8673 --- /dev/null +++ b/aconvert.automaton/src/AutomatonPrinter.cpp @@ -0,0 +1,60 @@ +#include "AutomatonPrinter.h" + +AutomatonPrinter::AutomatonPrinter(std::ostream& out) : m_Out(out) { + +} + +void AutomatonPrinter::printStateSymbolTo(automaton::FSM& automaton, automaton::State from, alphabet::Symbol input) { + bool sign = false; + for(auto iterTransitions = automaton.getTransitions().begin(); iterTransitions != automaton.getTransitions().end(); iterTransitions++) { + if(iterTransitions->getFrom() == from && iterTransitions->getInput() == input) { + m_Out << (sign ? "|" : " ") << iterTransitions->getTo().getName(); + sign = true; + } + } + if(!sign) { + m_Out << " -"; + } +} + +void AutomatonPrinter::print(automaton::FSM& automaton) { + + if(automaton.isDeterministic()) { + m_Out << "DFA"; + } else { + m_Out << "NFA"; + } + + for(auto iterSymbol = automaton.getInputAlphabet().begin(); iterSymbol != automaton.getInputAlphabet().end(); iterSymbol++) { + m_Out << " " << iterSymbol->getSymbol(); + } + + bool isEpsilonFreeSign = automaton.isEpsilonFree(); + if(!isEpsilonFreeSign) { + m_Out << " \\eps"; + } + + m_Out << std::endl; + + for(auto iterState = automaton.getStates().begin(); iterState != automaton.getStates().end(); iterState++) { + if(automaton.getInitialStates().find(*iterState) != automaton.getInitialStates().end()) { + m_Out << ">"; + } + if(automaton.getFinalStates().find(*iterState) != automaton.getFinalStates().end()) { + m_Out << "<"; + } + m_Out << iterState->getName(); + + for(auto iterSymbol = automaton.getInputAlphabet().begin(); iterSymbol != automaton.getInputAlphabet().end(); iterSymbol++) { + printStateSymbolTo(automaton, *iterState, *iterSymbol); + } + + if(!isEpsilonFreeSign) { + printStateSymbolTo(automaton, *iterState, alphabet::Symbol("")); + } + + m_Out << std::endl; + } + +} + diff --git a/aconvert.automaton/src/AutomatonPrinter.h b/aconvert.automaton/src/AutomatonPrinter.h new file mode 100644 index 0000000000..7f41357fec --- /dev/null +++ b/aconvert.automaton/src/AutomatonPrinter.h @@ -0,0 +1,17 @@ +#ifndef AUTOMATON_PRINTER_H_ +#define AUTOMATON_PRINTER_H_ + +#include <iostream> +#include "automaton/FSM/FSM.h" + +class AutomatonPrinter { + std::ostream& m_Out; + + void printStateSymbolTo(automaton::FSM& automaton, automaton::State from, alphabet::Symbol input); + +public: + AutomatonPrinter(std::ostream& out); + void print(automaton::FSM& automaton); +}; + +#endif /* AUTOMATON_PRINTER_H_ */ \ No newline at end of file diff --git a/aconvert.automaton/src/aconvert.automaton.cpp b/aconvert.automaton/src/aconvert.automaton.cpp index 0fcee3cecf..2b23de63b6 100644 --- a/aconvert.automaton/src/aconvert.automaton.cpp +++ b/aconvert.automaton/src/aconvert.automaton.cpp @@ -1,6 +1,7 @@ #include <iostream> #include <fstream> #include "AutomatonParser.h" +#include "AutomatonPrinter.h" #include <ctype.h> #include <stdio.h> @@ -8,6 +9,9 @@ #include <getopt.h> #include "automaton/FSM/FSM.h" +#include "automaton/UnknownAutomaton.h" +#include "AutomatonFactory.h" +#include "AlibException.h" #define FROM 1 #define TO 2 @@ -23,6 +27,22 @@ int fromAutomaton(std::istream& in, std::ostream& out) { } int toAutomaton(std::istream& in, std::ostream& out) { + + try { + + automaton::UnknownAutomaton automaton = automaton::AutomatonFactory::fromStream(in); + if(!automaton::AutomatonFactory::isFSM(automaton)) { + std::cerr << "Automaton minimize require deterministic finite automaton\n" << std::endl; + return 1; + } + automaton::FSM fsm = automaton::AutomatonFactory::buildFSM(automaton); + AutomatonPrinter printer = AutomatonPrinter(out); + printer.print(fsm); + + } catch (alib::AlibException& e) { + std::cerr << e.getCause() << std::endl; + return 1; + } return 0; } @@ -58,14 +78,16 @@ more: std::istream* in = &std::cin; if(optind != argc) in = new std::ifstream(argv[optind]); + int res; if(fromTo == FROM) { - fromAutomaton(*in, std::cout); + res = fromAutomaton(*in, std::cout); } else if(fromTo == TO) { - toAutomaton(*in, std::cout); + res = toAutomaton(*in, std::cout); } else { std::cerr << "error" << std::endl; + res = 1; } if(c != -1) delete in; - return 0; + return res; } \ No newline at end of file -- GitLab