Skip to content
Snippets Groups Projects
Commit bc95e9bc authored by Martin Žák's avatar Martin Žák
Browse files

Acat - option for parsing automaton into some known type, exception when file is not found

parent a778d565
No related branches found
No related tags found
No related merge requests found
......@@ -30,35 +30,67 @@ using namespace alib;
using namespace sax;
using namespace grammar;
 
void getAutomaton(list<Token>& tokens, bool complexTypes) {
UnknownAutomaton automaton = AutomatonParser::parse(tokens);
if(complexTypes) {
Automaton* concreateAutomaton = AutomatonFactory::buildAutomaton(automaton);
concreateAutomaton->toXML(cout);
} else {
automaton.toXML(cout);
}
}
void getGrammar(list<Token>& tokens) {
UnknownGrammar grammar = GrammarParser::parse(tokens);
grammar.toXML(cout);
}
void getRegExp(list<Token>& tokens) {
RegExp regexp = RegExpParser::parse(tokens);
regexp.toXML(cout);
}
int main(int argc, char** argv) {
list<Token> tokens;
int fileParameterIndex = -1;
bool complexTypes = false;
try {
if (argc > 1) {
if (string("-h").compare(argv[1]) == 0) {
cout << "Automaton parsing.\nUsage: catPDA [automaton.xml]\n";
return -1;
for (int i = 1; i < argc; i++) {
if (string("-h").compare(argv[i]) == 0) {
cout << "Automaton parsing.\nUsage: catPDA [automaton.xml]\n";
return -1;
} else if (string("-c").compare(argv[i]) == 0) {
complexTypes = true;
} else {
if(fileParameterIndex == -1) {
fileParameterIndex = i;
} else {
throw AlibException("Only one file can be passed as parameter - " + string(argv[i]) + " " + string(argv[fileParameterIndex]));
}
}
}
}
list<Token> tokens;
 
SaxInterface::parseFile(argv[1], tokens);
if(fileParameterIndex != -1) {
SaxInterface::parseFile(argv[fileParameterIndex],tokens);
} else {
string input(istreambuf_iterator<char>(cin),
(istreambuf_iterator<char>()));
string input(istreambuf_iterator<char>(cin), (istreambuf_iterator<char>()));
SaxInterface::parseMemory(input, tokens);
}
 
if (tokens.front().getData() == "automaton") {
UnknownAutomaton automaton = AutomatonParser::parse(tokens);
automaton.toXML(cout);
getAutomaton(tokens, complexTypes);
} else if (tokens.front().getData() == "grammar") {
UnknownGrammar grammar = GrammarParser::parse(tokens);
grammar.toXML(cout);
getGrammar(tokens);
} else if (tokens.front().getData() == "regexp") {
RegExp regexp = RegExpParser::parse(tokens);
regexp.toXML(cout);
getRegExp(tokens);
} else {
throw AlibException(
"Expected root tag automaton or grammar. Read: "
+ tokens.front().getData());
throw AlibException("Expected root tag automaton, grammar or regexp. Read: " + tokens.front().getData());
}
 
} catch (AlibException& e) {
......
......@@ -9,8 +9,10 @@
 
#include <string>
#include <cstring>
#include "../AlibException.h"
 
namespace sax {
void SaxInterface::initSAXHandler(xmlSAXHandler& handler) {
memset(&handler, 0, sizeof(handler));
handler.initialized = XML_SAX2_MAGIC;
......@@ -34,9 +36,13 @@ void SaxInterface::parseFile(std::string filename, std::list<Token>& out) {
xmlSAXHandler handler;
initSAXHandler(handler);
 
xmlSAXUserParseFile(&handler, (void*) &out, filename.c_str());
int result = xmlSAXUserParseFile(&handler, (void*) &out, filename.c_str());
xmlCleanupParser();
if(result != 0) {
throw alib::AlibException("Cannot parse the XML file " + filename);
}
}
 
void SaxInterface::characters(void * user_data, const xmlChar * ch, int len) {
......
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