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

convert to progtest like format

parent 25c8a8b4
No related branches found
No related tags found
No related merge requests found
#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;
}
}
#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
#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
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