2013-09-26 15:16:58 -04:00
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
2013-10-02 03:15:20 -04:00
|
|
|
#include <vector>
|
2013-09-26 15:16:58 -04:00
|
|
|
|
2013-05-11 16:13:46 -04:00
|
|
|
#include "NodeTree.h"
|
2013-07-28 19:45:08 -04:00
|
|
|
#include "Symbol.h"
|
2013-06-13 14:25:10 -04:00
|
|
|
#include "Lexer.h"
|
2013-07-30 01:42:51 -04:00
|
|
|
#include "LALRParser.h"
|
2013-07-31 23:51:05 -04:00
|
|
|
#include "RNGLRParser.h"
|
2013-09-26 15:16:58 -04:00
|
|
|
|
|
|
|
|
#include "NodeTransformation.h"
|
|
|
|
|
#include "RemovalTransformation.h"
|
2013-10-02 03:15:20 -04:00
|
|
|
#include "CollapseTransformation.h"
|
|
|
|
|
#include "ASTTransformation.h"
|
|
|
|
|
#include "ASTData.h"
|
2013-11-01 02:52:18 -04:00
|
|
|
#include "CGenerator.h"
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2013-12-19 10:39:36 -06:00
|
|
|
#include "util.h"
|
2013-05-11 16:13:46 -04:00
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2013-10-25 02:04:22 -07:00
|
|
|
if (argc == 2 && std::string(argv[1]) == "--test") {
|
|
|
|
|
StringReader::test();
|
2013-10-26 23:29:23 -07:00
|
|
|
RegEx::test();
|
2013-10-26 23:05:25 -07:00
|
|
|
Lexer::test();
|
2013-12-19 10:39:36 -06:00
|
|
|
//std::cout << strSlice("123", 0, -1) << std::endl;
|
2013-10-25 02:04:22 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-28 21:55:43 -05:00
|
|
|
std::ifstream programInFile, grammerInFile, compiledGrammerInFile;
|
2013-11-01 02:52:18 -04:00
|
|
|
std::ofstream outFile, outFileTransformed, outFileAST, outFileC;
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2013-05-29 20:43:35 -04:00
|
|
|
programInFile.open(argv[1]);
|
|
|
|
|
if (!programInFile.is_open()) {
|
|
|
|
|
std::cout << "Problem opening programInFile " << argv[1] << "\n";
|
2013-05-11 16:13:46 -04:00
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-28 21:55:43 -05:00
|
|
|
std::string grammerFileString = argv[2];
|
|
|
|
|
|
|
|
|
|
// compiledGrammerInFile.open(grammerFileString + ".comp");
|
|
|
|
|
// if (!compiledGrammerInFile.is_open()) {
|
|
|
|
|
// std::cout << "Problem opening compiledGrammerInFile " << grammerFileString + ".comp" << "\n";
|
|
|
|
|
// return(1);
|
|
|
|
|
// }
|
|
|
|
|
|
2013-12-28 21:54:22 -05:00
|
|
|
grammerInFile.open(grammerFileString);
|
2013-05-29 20:43:35 -04:00
|
|
|
if (!grammerInFile.is_open()) {
|
2013-12-28 21:54:22 -05:00
|
|
|
std::cout << "Problem opening grammerInFile " << grammerFileString << "\n";
|
2013-05-29 20:43:35 -04:00
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outFile.open(argv[3]);
|
2013-05-11 16:13:46 -04:00
|
|
|
if (!outFile.is_open()) {
|
2013-05-29 20:43:35 -04:00
|
|
|
std::cout << "Probelm opening output file " << argv[3] << "\n";
|
2013-05-11 16:13:46 -04:00
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-26 15:16:58 -04:00
|
|
|
outFileTransformed.open((std::string(argv[3]) + ".transformed.dot").c_str());
|
|
|
|
|
if (!outFileTransformed.is_open()) {
|
|
|
|
|
std::cout << "Probelm opening second output file " << std::string(argv[3]) + ".transformed.dot" << "\n";
|
|
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
outFileAST.open((std::string(argv[3]) + ".AST.dot").c_str());
|
|
|
|
|
if (!outFileAST.is_open()) {
|
|
|
|
|
std::cout << "Probelm opening second output file " << std::string(argv[3]) + ".AST.dot" << "\n";
|
|
|
|
|
return(1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-01 02:52:18 -04:00
|
|
|
outFileC.open((std::string(argv[3]) + ".c").c_str());
|
|
|
|
|
if (!outFileC.is_open()) {
|
|
|
|
|
std::cout << "Probelm opening third output file " << std::string(argv[3]) + ".c" << "\n";
|
|
|
|
|
return(1);
|
|
|
|
|
}
|
2013-05-20 19:34:15 -04:00
|
|
|
//Read the input file into a string
|
2013-05-29 20:43:35 -04:00
|
|
|
std::string programInputFileString, grammerInputFileString;
|
2013-05-20 19:34:15 -04:00
|
|
|
std::string line;
|
2013-05-29 20:43:35 -04:00
|
|
|
while(grammerInFile.good()) {
|
|
|
|
|
getline(grammerInFile, line);
|
|
|
|
|
grammerInputFileString.append(line+"\n");
|
|
|
|
|
}
|
2013-11-01 02:52:18 -04:00
|
|
|
grammerInFile.close();
|
2013-05-29 20:43:35 -04:00
|
|
|
|
|
|
|
|
while(programInFile.good()) {
|
|
|
|
|
getline(programInFile, line);
|
|
|
|
|
programInputFileString.append(line+"\n");
|
2013-05-20 19:34:15 -04:00
|
|
|
}
|
2013-11-01 02:52:18 -04:00
|
|
|
programInFile.close();
|
2013-05-20 19:34:15 -04:00
|
|
|
|
2013-07-31 23:51:05 -04:00
|
|
|
//LALRParser parser;
|
|
|
|
|
RNGLRParser parser;
|
2013-05-29 20:43:35 -04:00
|
|
|
parser.loadGrammer(grammerInputFileString);
|
2013-05-30 19:49:19 -04:00
|
|
|
//std::cout << "Creating State Set from Main" << std::endl;
|
2013-08-16 00:03:26 -04:00
|
|
|
std::cout << "\nState Set" << std::endl;
|
2013-05-24 00:00:41 -04:00
|
|
|
parser.createStateSet();
|
2013-05-30 19:49:19 -04:00
|
|
|
//std::cout << "finished State Set from Main" << std::endl;
|
|
|
|
|
//std::cout << "Doing stateSetToString from Main" << std::endl;
|
2013-08-16 00:03:26 -04:00
|
|
|
// std::cout << "\n\n\n\n\n\n\n\n\n\nState Set toString" << std::endl;
|
2013-08-26 15:37:49 -04:00
|
|
|
// std::cout << parser.stateSetToString() << std::endl;
|
2013-12-18 18:05:21 -06:00
|
|
|
// std::cout << "finished stateSetToString from Main" << std::endl;
|
2013-08-26 15:37:49 -04:00
|
|
|
// std::cout << "\n\n\n\n\n\n\n\n\n\nTable" << std::endl;
|
|
|
|
|
// std::cout << parser.tableToString() << std::endl;
|
2013-08-16 00:03:26 -04:00
|
|
|
// std::cout << "\n\n\n\n\n\n\n\n\n\nGrammer Input File" << std::endl;
|
|
|
|
|
// std::cout << grammerInputFileString << std::endl;
|
|
|
|
|
// std::cout << "\n\n\n\n\n\n\n\n\n\nGrammer toString" << std::endl;
|
|
|
|
|
// std::cout << parser.grammerToString() << std::endl;
|
2013-05-30 19:49:19 -04:00
|
|
|
//std::cout << parser.grammerToDOT() << std::endl;
|
2013-05-20 19:34:15 -04:00
|
|
|
|
2013-05-30 19:49:19 -04:00
|
|
|
//outFile << parser.grammerToDOT() << std::endl;
|
2013-08-16 00:03:26 -04:00
|
|
|
std::cout << "\nParsing" << std::endl;
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2013-05-29 20:43:35 -04:00
|
|
|
std::cout << programInputFileString << std::endl;
|
2013-10-02 03:15:20 -04:00
|
|
|
NodeTree<Symbol>* parseTree = parser.parseInput(programInputFileString);
|
2013-05-30 19:49:19 -04:00
|
|
|
|
|
|
|
|
if (parseTree) {
|
2013-08-16 00:03:26 -04:00
|
|
|
//std::cout << parseTree->DOTGraphString() << std::endl;
|
2013-05-30 19:49:19 -04:00
|
|
|
outFile << parseTree->DOTGraphString() << std::endl;
|
2013-09-26 15:16:58 -04:00
|
|
|
} else {
|
|
|
|
|
std::cout << "ParseTree returned from parser is NULL!" << std::endl;
|
2013-05-30 19:49:19 -04:00
|
|
|
}
|
2013-11-01 02:52:18 -04:00
|
|
|
outFile.close();
|
2013-05-29 20:43:35 -04:00
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
//Remove Transformations
|
2013-12-23 01:26:24 -06:00
|
|
|
std::vector<Symbol> removeSymbols;
|
|
|
|
|
removeSymbols.push_back(Symbol("WS", false));
|
|
|
|
|
removeSymbols.push_back(Symbol("\\(", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("\\)", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("::", true));
|
|
|
|
|
removeSymbols.push_back(Symbol(";", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("{", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("}", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("(", true));
|
|
|
|
|
removeSymbols.push_back(Symbol(")", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("import", true)); //Don't need the actual text of the symbol
|
|
|
|
|
removeSymbols.push_back(Symbol("interpreter_directive", false));
|
|
|
|
|
removeSymbols.push_back(Symbol("if", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("while", true));
|
2013-12-27 13:05:07 -06:00
|
|
|
removeSymbols.push_back(Symbol("__if_comp__", true));
|
|
|
|
|
removeSymbols.push_back(Symbol("comp_simple_passthrough", true));
|
2013-12-23 01:26:24 -06:00
|
|
|
|
|
|
|
|
for (int i = 0; i < removeSymbols.size(); i++)
|
|
|
|
|
parseTree = RemovalTransformation<Symbol>(removeSymbols[i]).transform(parseTree);
|
|
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
//Collapse Transformations
|
2013-12-23 01:26:24 -06:00
|
|
|
std::vector<Symbol> collapseSymbols;
|
|
|
|
|
|
|
|
|
|
collapseSymbols.push_back(Symbol("opt_typed_parameter_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("opt_parameter_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("opt_import_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("import_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("statement_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("parameter_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("typed_parameter_list", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("unorderd_list_part", false));
|
|
|
|
|
collapseSymbols.push_back(Symbol("if_comp_pred", false));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < collapseSymbols.size(); i++)
|
|
|
|
|
parseTree = CollapseTransformation<Symbol>(collapseSymbols[i]).transform(parseTree);
|
|
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
if (parseTree) {
|
|
|
|
|
outFileTransformed << parseTree->DOTGraphString() << std::endl;
|
2013-09-26 15:16:58 -04:00
|
|
|
} else {
|
|
|
|
|
std::cout << "Tree returned from transformation is NULL!" << std::endl;
|
|
|
|
|
}
|
2013-11-01 02:52:18 -04:00
|
|
|
outFileTransformed.close();
|
2013-09-26 15:16:58 -04:00
|
|
|
|
2013-12-27 13:05:07 -06:00
|
|
|
NodeTree<ASTData>* AST = ASTTransformation().transform(parseTree);
|
2013-10-02 03:15:20 -04:00
|
|
|
if (AST) {
|
2013-10-16 01:43:18 -04:00
|
|
|
outFileAST << AST->DOTGraphString() << std::endl;
|
2013-10-02 03:15:20 -04:00
|
|
|
} else {
|
|
|
|
|
std::cout << "Tree returned from ASTTransformation is NULL!" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
outFileAST.close();
|
2013-05-11 16:13:46 -04:00
|
|
|
|
2013-11-01 02:52:18 -04:00
|
|
|
//Do type checking, scope creation, etc. here.
|
2013-12-22 01:34:59 -06:00
|
|
|
//None at this time, instead going straight to C in this first (more naive) version
|
2013-11-01 02:52:18 -04:00
|
|
|
|
|
|
|
|
//Code generation
|
|
|
|
|
//For right now, just C
|
|
|
|
|
std::string c_code = CGenerator().generate(AST);
|
|
|
|
|
outFileC << c_code << std::endl;
|
2013-12-22 01:34:59 -06:00
|
|
|
outFileC.close();
|
2013-11-01 02:52:18 -04:00
|
|
|
|
2013-05-11 16:13:46 -04:00
|
|
|
return(0);
|
|
|
|
|
}
|
2013-10-25 02:04:22 -07:00
|
|
|
|