diff --git a/src/main.cpp b/src/main.cpp index ee8bc1f..1cc83ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,70 +1,53 @@ #include #include +#include #include "optparse.h" #include "Parsodus/inputparser.h" #include "Parsodus/backendmanager.h" -#include "Parsodus/lrtables/generator.h" -#include "Parsodus/lrtables/SLR1Itemset.h" #include "Parsodus/backends/cppLR.h" -#include "Parsodus/util/parserType.h" #include "Parsodus/driver.h" - int main(int argc, char** argv) { - optparse::OptionParser parser = optparse::OptionParser().description("Parsodus").usage("Parsodus [-d ] [-l ] [-n ] "); + /* Set and parse command line arguments */ + optparse::OptionParser parser = optparse::OptionParser().description("Parsodus").usage("Parsodus [-d ] [-l ] [-n ] "); parser.add_help_option(true); parser.version("%prog 1.0"); parser.add_option("-d", "--outputdir").dest("outputdir").help("Output the generated files to this directory\n[default: .]").metavar("").set_default("."); parser.add_option("-l", "--lang", "--language").dest("language").help("The programming language to generate source files for\n[default: c++]").metavar("").set_default("c++"); - parser.add_option("-n", "--name").dest("lexername").help("Use this name for the generated parser, the default is based on the input file name").metavar(""); + parser.add_option("-n", "--name").dest("parsername").help("Use this name for the generated parser, the default is based on the input file name").metavar(""); optparse::Values options = parser.parse_args(argc, argv); std::vector args = parser.args(); + /* Check whether a parsodus config file is given */ if (args.size() != 1) { parser.print_usage(std::cerr); return 1; } + /* Open parsodus config file, if possible */ std::ifstream infile(args[0]); if (!infile.good()) { std::cerr << "Could not open file '" << args[0] << "' for reading" << std::endl; return 1; } + std::string parsername = options["parsername"]; + + /* Set name of parser, if unset */ + if (parsername.empty() && args[0].size() >= 4 && args[0].substr(args[0].size() - 4,4) == ".pds") + parsername = args[0].substr(0, args[0].length() - 4); + auto backendManager = std::make_unique(pds::BackendManager()); + /* Register languages */ backendManager->registerLR(); - pds::Driver driver(std::move(backendManager), infile, options["outputdir"], options["language"], "testParser"); - return driver.run(); - - // Reporting what the inputparser found, to be removed... - // std::cout << "Start: " << config.grammar.start << std::endl; - // for(auto a: config.grammar.terminals) - // std::cout << "Terminal: " << a << std::endl; - // for(auto a: config.grammar.variables) - // std::cout << "Variable: " << a << std::endl; - // std::cout << "Rules: " << std::endl; - // for(auto a: config.grammar.rules) { - // std::cout << "\t" << a->head << " -> "; - // for(auto c: a->tail) { - // std::cout << c << " "; - // } - // std::cout << std::endl; - // } - // - // std::vector names = {"ERROR", "SHIFT", "REDUCE", "ACCEPT"}; - // pds::lr::Generator g(config.grammar); - // auto tbl = g.generate(); - // for (std::size_t i = 0; i < tbl.act.size(); i++) { - // std::cout << "State " << i << std::endl; - // std::cout << " Action:" << std::endl; - // for (auto& p : tbl.act[i]) { - // std::cout << " " << p.first << ": " << names[static_cast(p.second.first)] << " " << p.second.second << std::endl; - // } - // std::cout << " Goto:" << std::endl; - // for (auto& p : tbl.goto_[i]) { - // std::cout << " " << p.first << ": " << p.second << std::endl;; - // } - // } + /* Create parserfile, if possible */ + try { + pds::Driver driver(std::move(backendManager), infile, options["outputdir"], options["language"], parsername); + return driver.run(); + } catch (std::exception &err) { + std::cout << err.what() << std::endl; + return 1; + } }