Added caching of the RNGLR table. It is automatically regenerated whenever the grammer changes. Right now it has dropped compiling the test file from 30 seconds to less than one second.
This commit is contained in:
68
main.cpp
68
main.cpp
@@ -3,6 +3,8 @@
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "NodeTree.h"
|
||||
#include "Symbol.h"
|
||||
#include "Lexer.h"
|
||||
@@ -28,7 +30,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
std::ifstream programInFile, grammerInFile, compiledGrammerInFile;
|
||||
std::ofstream outFile, outFileTransformed, outFileAST, outFileC;
|
||||
std::ofstream outFile, outFileTransformed, outFileAST, outFileC, compiledGrammerOutFile;
|
||||
|
||||
programInFile.open(argv[1]);
|
||||
if (!programInFile.is_open()) {
|
||||
@@ -38,18 +40,18 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
std::string grammerFileString = argv[2];
|
||||
|
||||
// compiledGrammerInFile.open(grammerFileString + ".comp");
|
||||
// if (!compiledGrammerInFile.is_open()) {
|
||||
// std::cout << "Problem opening compiledGrammerInFile " << grammerFileString + ".comp" << "\n";
|
||||
// return(1);
|
||||
// }
|
||||
|
||||
grammerInFile.open(grammerFileString);
|
||||
if (!grammerInFile.is_open()) {
|
||||
std::cout << "Problem opening grammerInFile " << grammerFileString << "\n";
|
||||
return(1);
|
||||
}
|
||||
|
||||
compiledGrammerInFile.open(grammerFileString + ".comp", std::ios::binary | std::ios::ate);
|
||||
if (!compiledGrammerInFile.is_open()) {
|
||||
std::cout << "Problem opening compiledGrammerInFile " << grammerFileString + ".comp" << "\n";
|
||||
//return(1);
|
||||
}
|
||||
|
||||
outFile.open(argv[3]);
|
||||
if (!outFile.is_open()) {
|
||||
std::cout << "Probelm opening output file " << argv[3] << "\n";
|
||||
@@ -93,7 +95,57 @@ int main(int argc, char* argv[]) {
|
||||
parser.loadGrammer(grammerInputFileString);
|
||||
//std::cout << "Creating State Set from Main" << std::endl;
|
||||
std::cout << "\nState Set" << std::endl;
|
||||
parser.createStateSet();
|
||||
|
||||
//Start binary stuff
|
||||
bool compGramGood = false;
|
||||
if (compiledGrammerInFile.is_open()) {
|
||||
std::cout << "Compiled grammer file exists, reading it in" << std::endl;
|
||||
std::streampos compGramSize = compiledGrammerInFile.tellg();
|
||||
char* binaryTablePointer = new char [compGramSize];
|
||||
compiledGrammerInFile.seekg(0, std::ios::beg);
|
||||
compiledGrammerInFile.read(binaryTablePointer, compGramSize);
|
||||
compiledGrammerInFile.close();
|
||||
if (binaryTablePointer[0] == 'K' && binaryTablePointer[1] == 'R' && binaryTablePointer[2] == 'A' && binaryTablePointer[3] == 'K') {
|
||||
std::cout << "Valid Kraken Compiled Grammer File" << std::endl;
|
||||
int gramStringLength = *((int*)(binaryTablePointer+4));
|
||||
std::cout << "The grammer string is stored to be " << gramStringLength << " characters long, gramString is "
|
||||
<< grammerInputFileString.length() << " long. Remember 1 extra for null terminator!" << std::endl;
|
||||
if (grammerInputFileString.length() != gramStringLength-1 ||
|
||||
(strncmp(grammerInputFileString.c_str(), (binaryTablePointer+4+sizeof(int)), gramStringLength) != 0)) {
|
||||
//(one less for null terminator that is stored)
|
||||
std::cout << "The Grammer has been changed, will re-create" << std::endl;
|
||||
} else {
|
||||
compGramGood = true;
|
||||
std::cout << "grammer file good" << std::endl;
|
||||
//int tableLength = *((int*)(binaryTablePointer + 4 + sizeof(int) + gramStringLength));
|
||||
parser.importTable(binaryTablePointer + 4 + sizeof(int) + gramStringLength); //Load table starting at the table section
|
||||
}
|
||||
} else {
|
||||
std::cout << grammerFileString << ".comp is NOT A Valid Kraken Compiled Grammer File, aborting" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
delete binaryTablePointer;
|
||||
}
|
||||
|
||||
if (!compGramGood) {
|
||||
//The load failed because either the file does not exist or it is not up-to-date.
|
||||
std::cout << "Compiled grammer file does not exist or is not up-to-date, generating table and writing it out" << std::endl;
|
||||
compiledGrammerOutFile.open(grammerFileString + ".comp", std::ios::binary);
|
||||
if (!compiledGrammerOutFile.is_open())
|
||||
std::cout << "Could not open compiled file to write either!" << std::endl;
|
||||
compiledGrammerOutFile.write("KRAK", sizeof(char)*4);
|
||||
int* intBuffer = new int;
|
||||
*intBuffer = grammerInputFileString.length()+1;
|
||||
compiledGrammerOutFile.write((char*)intBuffer, sizeof(int));
|
||||
delete intBuffer;
|
||||
compiledGrammerOutFile.write(grammerInputFileString.c_str(), grammerInputFileString.length()+1); //Don't forget null terminator
|
||||
|
||||
parser.createStateSet();
|
||||
parser.exportTable(compiledGrammerOutFile);
|
||||
compiledGrammerOutFile.close();
|
||||
}
|
||||
//End binary stuff
|
||||
|
||||
//std::cout << "finished State Set from Main" << std::endl;
|
||||
//std::cout << "Doing stateSetToString from Main" << std::endl;
|
||||
// std::cout << "\n\n\n\n\n\n\n\n\n\nState Set toString" << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user