Skip to content

Commit

Permalink
Pass options to translators
Browse files Browse the repository at this point in the history
Implement enhancement #7
  • Loading branch information
mhhollomon committed Feb 13, 2019
1 parent 1cd93c1 commit 5b6cccc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
14 changes: 14 additions & 0 deletions src/CLIOptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#if ! defined(YALR_CLIOPTIONS_HPP)
#define YALR_CLIOPTIONS_HPP

#include <string>

struct CLIOptions {
std::string output_file;
std::string translate;
std::string state_file;
std::string input_file;
bool help = false;
};

#endif
11 changes: 2 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "CLIOptions.hpp"
#include "parser.hpp"
#include "analyzer.hpp"
#include "tablegen.hpp"
Expand All @@ -22,13 +23,6 @@
// } catch ...


struct CLIOptions {
std::string output_file;
std::string translate;
std::string state_file;
std::string input_file;
bool help = false;
};

CLIOptions parse_commandline(int argc, char**argv) {

Expand Down Expand Up @@ -159,8 +153,7 @@ int main(int argc, char* argv[]) {
const auto format = clopts.translate;

if (format == "grammophone") {
std::ofstream code_out(outfilename, std::ios_base::out);
yalr::translate::grammophone(*ana_tree).output(code_out);
yalr::translate::grammophone().output(*ana_tree, clopts);
} else {
std::cerr << "Unknown format '" << format << "'\n";
exit(1);
Expand Down
24 changes: 19 additions & 5 deletions src/translate.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "translate.hpp"

#include <fstream>
#include <ios>

namespace yalr::translate {

void output_prod(const analyzer::production& p, std::ostream& strm) {
Expand All @@ -10,12 +13,23 @@ void output_prod(const analyzer::production& p, std::ostream& strm) {
strm << " .\n";

}
void grammophone::output(std::ostream& strm) const {
void grammophone::output(const analyzer::grammar& gr, CLIOptions &clopts) const {

std::string outfilename;
if (not clopts.output_file.empty()) {
outfilename = clopts.output_file;
} else {
outfilename = gr.parser_class + ".txt";
}

std::ofstream code_out(outfilename, std::ios_base::out);

output_prod(g.productions[g.target_prod], strm);
for (const auto& p : g.productions) {
if (p.prod_id != g.target_prod) {
output_prod(p, strm);
// grammophone uses the first production as the target
// So, be sure to our target out first.
output_prod(gr.productions[gr.target_prod], code_out);
for (const auto& p : gr.productions) {
if (p.prod_id != gr.target_prod) {
output_prod(p, code_out);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/translate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
#define YALR_TRANSLATE_HPP

#include "analyzer.hpp"
#include "CLIOptions.hpp"

namespace yalr::translate {

struct grammophone {
const analyzer::grammar& g;

grammophone(const analyzer::grammar& gr) : g(gr) {};

void output(std::ostream& strm) const;
void output(const analyzer::grammar& gr, CLIOptions &opts) const;
};

} // namespace yalr::translate
Expand Down

0 comments on commit 5b6cccc

Please sign in to comment.