Now generates for all files that have been imported. CGenerator uses this to generate all files AND a shell script with the compile command to compile the generated C file.
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "NodeTree.h"
|
#include "NodeTree.h"
|
||||||
#include "ASTData.h"
|
#include "ASTData.h"
|
||||||
@@ -15,8 +16,10 @@ class CGenerator {
|
|||||||
public:
|
public:
|
||||||
CGenerator();
|
CGenerator();
|
||||||
~CGenerator();
|
~CGenerator();
|
||||||
|
void generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName);
|
||||||
std::string generate(NodeTree<ASTData>* from);
|
std::string generate(NodeTree<ASTData>* from);
|
||||||
static std::string ValueTypeToCType(Type type);
|
static std::string ValueTypeToCType(Type type);
|
||||||
|
|
||||||
std::string generatorString;
|
std::string generatorString;
|
||||||
private:
|
private:
|
||||||
std::string tabs();
|
std::string tabs();
|
||||||
|
|||||||
@@ -19,10 +19,12 @@ class Importer {
|
|||||||
Importer(Parser* parserIn);
|
Importer(Parser* parserIn);
|
||||||
~Importer();
|
~Importer();
|
||||||
NodeTree<ASTData>* import(std::string fileName);
|
NodeTree<ASTData>* import(std::string fileName);
|
||||||
|
std::map<std::string, NodeTree<ASTData>*> getASTMap();
|
||||||
private:
|
private:
|
||||||
Parser* parser;
|
Parser* parser;
|
||||||
std::vector<Symbol> removeSymbols;
|
std::vector<Symbol> removeSymbols;
|
||||||
std::vector<Symbol> collapseSymbols;
|
std::vector<Symbol> collapseSymbols;
|
||||||
|
std::map<std::string, NodeTree<ASTData>*> imported;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
22
main.cpp
22
main.cpp
@@ -31,7 +31,7 @@ int main(int argc, char* argv[]) {
|
|||||||
std::string outputName = argv[3];
|
std::string outputName = argv[3];
|
||||||
|
|
||||||
std::ifstream grammerInFile, compiledGrammerInFile;
|
std::ifstream grammerInFile, compiledGrammerInFile;
|
||||||
std::ofstream outFileC, compiledGrammerOutFile;
|
std::ofstream /*outFileC,*/ compiledGrammerOutFile;
|
||||||
|
|
||||||
grammerInFile.open(grammerFileString);
|
grammerInFile.open(grammerFileString);
|
||||||
if (!grammerInFile.is_open()) {
|
if (!grammerInFile.is_open()) {
|
||||||
@@ -44,12 +44,13 @@ int main(int argc, char* argv[]) {
|
|||||||
std::cout << "Problem opening compiledGrammerInFile " << grammerFileString + ".comp" << "\n";
|
std::cout << "Problem opening compiledGrammerInFile " << grammerFileString + ".comp" << "\n";
|
||||||
//return(1);
|
//return(1);
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
outFileC.open((outputName + ".c").c_str());
|
outFileC.open((outputName + ".c").c_str());
|
||||||
if (!outFileC.is_open()) {
|
if (!outFileC.is_open()) {
|
||||||
std::cout << "Probelm opening third output file " << outputName + ".c" << "\n";
|
std::cout << "Probelm opening third output file " << outputName + ".c" << "\n";
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
//Read the input file into a string
|
//Read the input file into a string
|
||||||
std::string grammerInputFileString;
|
std::string grammerInputFileString;
|
||||||
std::string line;
|
std::string line;
|
||||||
@@ -63,7 +64,7 @@ int main(int argc, char* argv[]) {
|
|||||||
RNGLRParser parser;
|
RNGLRParser parser;
|
||||||
parser.loadGrammer(grammerInputFileString);
|
parser.loadGrammer(grammerInputFileString);
|
||||||
//std::cout << "Creating State Set from Main" << std::endl;
|
//std::cout << "Creating State Set from Main" << std::endl;
|
||||||
std::cout << "\nState Set" << std::endl;
|
//std::cout << "\nState Set" << std::endl;
|
||||||
|
|
||||||
//Start binary stuff
|
//Start binary stuff
|
||||||
bool compGramGood = false;
|
bool compGramGood = false;
|
||||||
@@ -78,15 +79,15 @@ int main(int argc, char* argv[]) {
|
|||||||
if (binaryTablePointer[0] == 'K' && binaryTablePointer[1] == 'R' && binaryTablePointer[2] == 'A' && binaryTablePointer[3] == 'K') {
|
if (binaryTablePointer[0] == 'K' && binaryTablePointer[1] == 'R' && binaryTablePointer[2] == 'A' && binaryTablePointer[3] == 'K') {
|
||||||
std::cout << "Valid Kraken Compiled Grammer File" << std::endl;
|
std::cout << "Valid Kraken Compiled Grammer File" << std::endl;
|
||||||
int gramStringLength = *((int*)(binaryTablePointer+4));
|
int gramStringLength = *((int*)(binaryTablePointer+4));
|
||||||
std::cout << "The grammer string is stored to be " << gramStringLength << " characters long, gramString is "
|
//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;
|
//<< grammerInputFileString.length() << " long. Remember 1 extra for null terminator!" << std::endl;
|
||||||
if (grammerInputFileString.length() != gramStringLength-1 ||
|
if (grammerInputFileString.length() != gramStringLength-1 ||
|
||||||
(strncmp(grammerInputFileString.c_str(), (binaryTablePointer+4+sizeof(int)), gramStringLength) != 0)) {
|
(strncmp(grammerInputFileString.c_str(), (binaryTablePointer+4+sizeof(int)), gramStringLength) != 0)) {
|
||||||
//(one less for null terminator that is stored)
|
//(one less for null terminator that is stored)
|
||||||
std::cout << "The Grammer has been changed, will re-create" << std::endl;
|
std::cout << "The Grammer has been changed, will re-create" << std::endl;
|
||||||
} else {
|
} else {
|
||||||
compGramGood = true;
|
compGramGood = true;
|
||||||
std::cout << "grammer file good" << std::endl;
|
std::cout << "Grammer file is up to date." << std::endl;
|
||||||
//int tableLength = *((int*)(binaryTablePointer + 4 + sizeof(int) + gramStringLength));
|
//int tableLength = *((int*)(binaryTablePointer + 4 + sizeof(int) + gramStringLength));
|
||||||
parser.importTable(binaryTablePointer + 4 + sizeof(int) + gramStringLength); //Load table starting at the table section
|
parser.importTable(binaryTablePointer + 4 + sizeof(int) + gramStringLength); //Load table starting at the table section
|
||||||
}
|
}
|
||||||
@@ -134,17 +135,22 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
Importer importer(&parser);
|
Importer importer(&parser);
|
||||||
|
|
||||||
NodeTree<ASTData>* AST = importer.import(programName);
|
/*NodeTree<ASTData>* AST =*/
|
||||||
|
importer.import(programName);
|
||||||
|
std::map<std::string, NodeTree<ASTData>*> ASTs =importer.getASTMap();
|
||||||
|
|
||||||
//Do optomization, etc. here.
|
//Do optomization, etc. here.
|
||||||
//None at this time, instead going straight to C in this first (more naive) version
|
//None at this time, instead going straight to C in this first (more naive) version
|
||||||
|
|
||||||
//Code generation
|
//Code generation
|
||||||
//For right now, just C
|
//For right now, just C
|
||||||
|
|
||||||
|
CGenerator().generateCompSet(ASTs, outputName);
|
||||||
|
/*
|
||||||
std::string c_code = CGenerator().generate(AST);
|
std::string c_code = CGenerator().generate(AST);
|
||||||
outFileC << c_code << std::endl;
|
outFileC << c_code << std::endl;
|
||||||
outFileC.close();
|
outFileC.close();
|
||||||
|
*/
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
return newNode; // Don't need children of import
|
return newNode; // Don't need children of import
|
||||||
} else if (name == "identifier") {
|
} else if (name == "identifier") {
|
||||||
std::string lookupName = concatSymbolTree(children[0]);
|
std::string lookupName = concatSymbolTree(children[0]);
|
||||||
std::cout << "scope lookup from identifier" << std::endl;
|
//std::cout << "scope lookup from identifier" << std::endl;
|
||||||
newNode = scopeLookup(scope, lookupName);
|
newNode = scopeLookup(scope, lookupName);
|
||||||
if (newNode == NULL) {
|
if (newNode == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << lookupName << std::endl;
|
std::cout << "scope lookup error! Could not find " << lookupName << std::endl;
|
||||||
@@ -87,7 +87,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
//If this is an actual part of an expression, not just a premoted term
|
//If this is an actual part of an expression, not just a premoted term
|
||||||
if (children.size() > 1) {
|
if (children.size() > 1) {
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
std::cout << "scope lookup from boolen_expression or similar" << std::endl;
|
//std::cout << "scope lookup from boolen_expression or similar" << std::endl;
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
||||||
@@ -97,7 +97,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
newNode->addChild(function); // First child of function call is a link to the function definition
|
newNode->addChild(function); // First child of function call is a link to the function definition
|
||||||
skipChildren.insert(1);
|
skipChildren.insert(1);
|
||||||
} else {
|
} else {
|
||||||
std::cout << children.size() << std::endl;
|
//std::cout << children.size() << std::endl;
|
||||||
if (children.size() == 0)
|
if (children.size() == 0)
|
||||||
return new NodeTree<ASTData>();
|
return new NodeTree<ASTData>();
|
||||||
return transform(children[0], scope); //Just a promoted term, so do child
|
return transform(children[0], scope); //Just a promoted term, so do child
|
||||||
@@ -107,7 +107,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
//If this is an actual part of an expression, not just a premoted child
|
//If this is an actual part of an expression, not just a premoted child
|
||||||
if (children.size() > 2) {
|
if (children.size() > 2) {
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
std::cout << "scope lookup from expression or similar" << std::endl;
|
//std::cout << "scope lookup from expression or similar" << std::endl;
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
||||||
@@ -130,7 +130,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
else
|
else
|
||||||
funcName = concatSymbolTree(children[1]), funcNum = 1;
|
funcName = concatSymbolTree(children[1]), funcNum = 1;
|
||||||
|
|
||||||
std::cout << "scope lookup from factor" << std::endl;
|
//std::cout << "scope lookup from factor" << std::endl;
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, funcName);
|
NodeTree<ASTData>* function = scopeLookup(scope, funcName);
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << funcName << std::endl;
|
std::cout << "scope lookup error! Could not find " << funcName << std::endl;
|
||||||
@@ -199,7 +199,7 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
|
|||||||
//children[0] is scope
|
//children[0] is scope
|
||||||
std::string functionCallName = concatSymbolTree(children[1]);
|
std::string functionCallName = concatSymbolTree(children[1]);
|
||||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
||||||
std::cout << "scope lookup from function_call" << std::endl;
|
//std::cout << "scope lookup from function_call" << std::endl;
|
||||||
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
NodeTree<ASTData>* function = scopeLookup(scope, functionCallName);
|
||||||
if (function == NULL) {
|
if (function == NULL) {
|
||||||
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
std::cout << "scope lookup error! Could not find " << functionCallName << std::endl;
|
||||||
@@ -260,8 +260,8 @@ NodeTree<ASTData>* ASTTransformation::scopeLookup(NodeTree<ASTData>* scope, std:
|
|||||||
//Seach the map
|
//Seach the map
|
||||||
auto scopeMap = scope->getDataRef()->scope;
|
auto scopeMap = scope->getDataRef()->scope;
|
||||||
//std::cout << "scope size: " << scopeMap.size() << ", scope from " << scope->getName() << std::endl;
|
//std::cout << "scope size: " << scopeMap.size() << ", scope from " << scope->getName() << std::endl;
|
||||||
for (auto i = scopeMap.begin(); i != scopeMap.end(); i++)
|
// for (auto i = scopeMap.begin(); i != scopeMap.end(); i++)
|
||||||
std::cout << i->first << " : " << i-> second << " - " << i->second->getName() << std::endl;
|
// std::cout << i->first << " : " << i-> second << " - " << i->second->getName() << std::endl;
|
||||||
|
|
||||||
auto elementIterator = scopeMap.find(lookup);
|
auto elementIterator = scopeMap.find(lookup);
|
||||||
if (elementIterator != scopeMap.end()) {
|
if (elementIterator != scopeMap.end()) {
|
||||||
|
|||||||
@@ -7,6 +7,27 @@ CGenerator::~CGenerator() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CGenerator::generateCompSet(std::map<std::string, NodeTree<ASTData>*> ASTs, std::string outputName) {
|
||||||
|
//Generate an entire set of files
|
||||||
|
std::string buildString = "#!/bin/sh\ncc -std=c99 ";
|
||||||
|
for (auto i = ASTs.begin(); i != ASTs.end(); i++) {
|
||||||
|
buildString += i->first + ".c ";
|
||||||
|
std::ofstream outputCFile;
|
||||||
|
outputCFile.open(i->first + ".c");
|
||||||
|
if (outputCFile.is_open()) {
|
||||||
|
outputCFile << generate(i->second);
|
||||||
|
} else {
|
||||||
|
std::cout << "Cannot open file " << i->first << ".c" << std::endl;
|
||||||
|
}
|
||||||
|
outputCFile.close();
|
||||||
|
}
|
||||||
|
buildString += "-o " + outputName;
|
||||||
|
std::ofstream outputBuild;
|
||||||
|
outputBuild.open(outputName + ".sh");
|
||||||
|
outputBuild << buildString;
|
||||||
|
outputBuild.close();
|
||||||
|
}
|
||||||
|
|
||||||
std::string CGenerator::tabs() {
|
std::string CGenerator::tabs() {
|
||||||
std::string returnTabs;
|
std::string returnTabs;
|
||||||
for (int i = 0; i < tabLevel; i++)
|
for (int i = 0; i < tabLevel; i++)
|
||||||
@@ -39,7 +60,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
|||||||
output+= "); /*func*/\n";
|
output+= "); /*func*/\n";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
|
//std::cout << "Declaration? named " << declaration->getName() << " of unknown type " << ASTData::ASTTypeToString(declarationData.type) << " in translation unit scope" << std::endl;
|
||||||
output += "/*unknown declaration named " + declaration->getName() + "*/\n";
|
output += "/*unknown declaration named " + declaration->getName() + "*/\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,7 +130,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
|||||||
//Handle operators specially for now. Will later replace with
|
//Handle operators specially for now. Will later replace with
|
||||||
//Inlined functions in the standard library
|
//Inlined functions in the standard library
|
||||||
std::string name = data.symbol.getName();
|
std::string name = data.symbol.getName();
|
||||||
std::cout << name << " == " << children[0]->getData().symbol.getName() << std::endl;
|
//std::cout << name << " == " << children[0]->getData().symbol.getName() << std::endl;
|
||||||
if (name == "++" || name == "--")
|
if (name == "++" || name == "--")
|
||||||
return generate(children[1]) + name;
|
return generate(children[1]) + name;
|
||||||
if (name == "*" && children.size() == 2) //Is dereference, not multiplication
|
if (name == "*" && children.size() == 2) //Is dereference, not multiplication
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ Importer::~Importer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NodeTree<ASTData>* Importer::import(std::string fileName) {
|
NodeTree<ASTData>* Importer::import(std::string fileName) {
|
||||||
|
//Check to see if we've already done it
|
||||||
|
if (imported.find(fileName) != imported.end())
|
||||||
|
return imported[fileName];
|
||||||
|
|
||||||
std::ifstream programInFile;
|
std::ifstream programInFile;
|
||||||
std::ofstream outFile, outFileTransformed, outFileAST;
|
std::ofstream outFile, outFileTransformed, outFileAST;
|
||||||
|
|
||||||
@@ -64,7 +68,7 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
|
|||||||
std::cout << "Probelm opening second output file " << outputName + ".AST.dot" << "\n";
|
std::cout << "Probelm opening second output file " << outputName + ".AST.dot" << "\n";
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
//ljklj
|
|
||||||
std::string programInputFileString, line;
|
std::string programInputFileString, line;
|
||||||
while(programInFile.good()) {
|
while(programInFile.good()) {
|
||||||
getline(programInFile, line);
|
getline(programInFile, line);
|
||||||
@@ -72,7 +76,7 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
|
|||||||
}
|
}
|
||||||
programInFile.close();
|
programInFile.close();
|
||||||
|
|
||||||
std::cout << programInputFileString << std::endl;
|
//std::cout << programInputFileString << std::endl;
|
||||||
NodeTree<Symbol>* parseTree = parser->parseInput(programInputFileString);
|
NodeTree<Symbol>* parseTree = parser->parseInput(programInputFileString);
|
||||||
|
|
||||||
if (parseTree) {
|
if (parseTree) {
|
||||||
@@ -109,5 +113,11 @@ NodeTree<ASTData>* Importer::import(std::string fileName) {
|
|||||||
}
|
}
|
||||||
outFileAST.close();
|
outFileAST.close();
|
||||||
|
|
||||||
|
imported[fileName] = AST;
|
||||||
|
|
||||||
return AST;
|
return AST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::map<std::string, NodeTree<ASTData>*> Importer::getASTMap() {
|
||||||
|
return imported;
|
||||||
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ void Parser::loadGrammer(std::string grammerInputString) {
|
|||||||
//Get next token
|
//Get next token
|
||||||
currToken = reader.word();
|
currToken = reader.word();
|
||||||
}
|
}
|
||||||
std::cout << "Parsed!\n";
|
//std::cout << "Parsed!\n";
|
||||||
|
|
||||||
// for (std::vector<ParseRule*>::size_type i = 0; i < loadedGrammer.size(); i++)
|
// for (std::vector<ParseRule*>::size_type i = 0; i < loadedGrammer.size(); i++)
|
||||||
// std::cout << loadedGrammer[i]->toString() << std::endl;
|
// std::cout << loadedGrammer[i]->toString() << std::endl;
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
|
|||||||
input.push_back(currentToken);
|
input.push_back(currentToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "\nDone with Lexing, length:" << input.size() << std::endl;
|
// std::cout << "\nDone with Lexing, length:" << input.size() << std::endl;
|
||||||
std::cout << input[0].toString() << std::endl;
|
// std::cout << input[0].toString() << std::endl;
|
||||||
|
|
||||||
|
|
||||||
// for (int i = 0; i < input.size(); i++)
|
// for (int i = 0; i < input.size(); i++)
|
||||||
@@ -59,13 +59,13 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
|
|||||||
// std::cout << std::endl;
|
// std::cout << std::endl;
|
||||||
|
|
||||||
|
|
||||||
std::cout << "Setting up 0th frontier, first actions, toShift, toReduce" << std::endl;
|
//std::cout << "Setting up 0th frontier, first actions, toShift, toReduce" << std::endl;
|
||||||
|
|
||||||
//Frontier 0, new node with state 0
|
//Frontier 0, new node with state 0
|
||||||
NodeTree<int>* v0 = gss.newNode(0);
|
NodeTree<int>* v0 = gss.newNode(0);
|
||||||
gss.addToFrontier(0,v0);
|
gss.addToFrontier(0,v0);
|
||||||
|
|
||||||
std::cout << "Done setting up new frontier" << std::endl;
|
//std::cout << "Done setting up new frontier" << std::endl;
|
||||||
|
|
||||||
std::vector<ParseAction*> firstActions = *(table.get(0, input[0]));
|
std::vector<ParseAction*> firstActions = *(table.get(0, input[0]));
|
||||||
for (std::vector<ParseAction*>::size_type i = 0; i < firstActions.size(); i++) {
|
for (std::vector<ParseAction*>::size_type i = 0; i < firstActions.size(); i++) {
|
||||||
@@ -80,7 +80,7 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
|
|||||||
|
|
||||||
// std::cout << "GSS:\n" << gss.toString() << std::endl;
|
// std::cout << "GSS:\n" << gss.toString() << std::endl;
|
||||||
|
|
||||||
std::cout << "Starting parse loop" << std::endl;
|
//std::cout << "Starting parse loop" << std::endl;
|
||||||
|
|
||||||
for (int i = 0; i < input.size(); i++) {
|
for (int i = 0; i < input.size(); i++) {
|
||||||
// std::cout << "Checking if frontier " << i << " is empty" << std::endl;
|
// std::cout << "Checking if frontier " << i << " is empty" << std::endl;
|
||||||
@@ -110,7 +110,7 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
|
|||||||
shifter(i);
|
shifter(i);
|
||||||
//std::cout << "GSS:\n" << gss.toString() << std::endl;
|
//std::cout << "GSS:\n" << gss.toString() << std::endl;
|
||||||
}
|
}
|
||||||
std::cout << "Done with parsing loop, checking for acceptance" << std::endl;
|
//std::cout << "Done with parsing loop, checking for acceptance" << std::endl;
|
||||||
NodeTree<int>* accState = gss.frontierGetAccState(input.size()-1);
|
NodeTree<int>* accState = gss.frontierGetAccState(input.size()-1);
|
||||||
if (accState) {
|
if (accState) {
|
||||||
std::cout << "Accepted!" << std::endl;
|
std::cout << "Accepted!" << std::endl;
|
||||||
@@ -143,7 +143,7 @@ void RNGLRParser::reducer(int i) {
|
|||||||
//The end of the current path
|
//The end of the current path
|
||||||
NodeTree<int>* currentReached = currentPath[currentPath.size()-1];
|
NodeTree<int>* currentReached = currentPath[currentPath.size()-1];
|
||||||
|
|
||||||
std::cout << "Getting the shfit state for state " << currentReached->getData() << " and symbol " << reduction.symbol.toString() << std::endl;
|
//std::cout << "Getting the shift state for state " << currentReached->getData() << " and symbol " << reduction.symbol.toString() << std::endl;
|
||||||
int toState = table.getShift(currentReached->getData(), reduction.symbol)->shiftState;
|
int toState = table.getShift(currentReached->getData(), reduction.symbol)->shiftState;
|
||||||
|
|
||||||
//If reduction length is 0, then we make the new label the appropriate nullable parts
|
//If reduction length is 0, then we make the new label the appropriate nullable parts
|
||||||
@@ -189,7 +189,7 @@ void RNGLRParser::reducer(int i) {
|
|||||||
//std::cout << "Adding shifts and reductions for a state that did not exist" << std::endl;
|
//std::cout << "Adding shifts and reductions for a state that did not exist" << std::endl;
|
||||||
std::vector<ParseAction*> actions = *(table.get(toState, input[i]));
|
std::vector<ParseAction*> actions = *(table.get(toState, input[i]));
|
||||||
for (std::vector<ParseAction*>::size_type k = 0; k < actions.size(); k++) {
|
for (std::vector<ParseAction*>::size_type k = 0; k < actions.size(); k++) {
|
||||||
std::cout << "Action is " << actions[k]->toString() << std::endl;
|
//std::cout << "Action is " << actions[k]->toString() << std::endl;
|
||||||
if (actions[k]->action == ParseAction::SHIFT) {
|
if (actions[k]->action == ParseAction::SHIFT) {
|
||||||
toShift.push(std::make_pair(toStateNode, actions[k]->shiftState));
|
toShift.push(std::make_pair(toStateNode, actions[k]->shiftState));
|
||||||
} else if (actions[k]->action == ParseAction::REDUCE && fullyReducesToNull(actions[k]->reduceRule)) {
|
} else if (actions[k]->action == ParseAction::REDUCE && fullyReducesToNull(actions[k]->reduceRule)) {
|
||||||
@@ -213,7 +213,7 @@ void RNGLRParser::shifter(int i) {
|
|||||||
while (!toShift.empty()) {
|
while (!toShift.empty()) {
|
||||||
std::pair<NodeTree<int>*, int> shift = toShift.front();
|
std::pair<NodeTree<int>*, int> shift = toShift.front();
|
||||||
toShift.pop();
|
toShift.pop();
|
||||||
std::cout << "Current potential shift from " << shift.first->getData() << " to " << shift.second << std::endl;
|
//std::cout << "Current potential shift from " << shift.first->getData() << " to " << shift.second << std::endl;
|
||||||
NodeTree<int>* shiftTo = gss.inFrontier(i+1, shift.second);
|
NodeTree<int>* shiftTo = gss.inFrontier(i+1, shift.second);
|
||||||
if (shiftTo) {
|
if (shiftTo) {
|
||||||
//std::cout << "State already existed, just adding edge" << std::endl;
|
//std::cout << "State already existed, just adding edge" << std::endl;
|
||||||
@@ -232,7 +232,7 @@ void RNGLRParser::shifter(int i) {
|
|||||||
gss.addEdge(shiftTo, shift.first, newLabel);
|
gss.addEdge(shiftTo, shift.first, newLabel);
|
||||||
std::vector<ParseAction*> actions = *(table.get(shift.second, input[i+1]));
|
std::vector<ParseAction*> actions = *(table.get(shift.second, input[i+1]));
|
||||||
for (std::vector<ParseAction*>::size_type j = 0; j < actions.size(); j++) {
|
for (std::vector<ParseAction*>::size_type j = 0; j < actions.size(); j++) {
|
||||||
std::cout << "Adding action " << actions[j]->toString() << " to either nextShifts or toReduce" << std::endl;
|
//std::cout << "Adding action " << actions[j]->toString() << " to either nextShifts or toReduce" << std::endl;
|
||||||
//Shift
|
//Shift
|
||||||
if (actions[j]->action == ParseAction::SHIFT) {
|
if (actions[j]->action == ParseAction::SHIFT) {
|
||||||
nextShifts.push(std::make_pair(shiftTo, actions[j]->shiftState));
|
nextShifts.push(std::make_pair(shiftTo, actions[j]->shiftState));
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ std::string StringReader::getTokens(const char *stop_chars, bool truncateEnd)
|
|||||||
{
|
{
|
||||||
//End of String
|
//End of String
|
||||||
end_reached = true;
|
end_reached = true;
|
||||||
std::cout << "Reached end of file!\n";
|
//std::cout << "Reached end of file!\n";
|
||||||
return "";
|
return "";
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|||||||
@@ -303,7 +303,7 @@ std::vector<ParseAction*>* Table::get(int state, Symbol token) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Get for state: " << state << ", and Symbol: " << token.toString() << std::endl;
|
//std::cout << "Get for state: " << state << ", and Symbol: " << token.toString() << std::endl;
|
||||||
if (state < 0 || state >= table.size()) {
|
if (state < 0 || state >= table.size()) {
|
||||||
std::cout << "State bad: " << state << std::endl;
|
std::cout << "State bad: " << state << std::endl;
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -312,7 +312,7 @@ std::vector<ParseAction*>* Table::get(int state, Symbol token) {
|
|||||||
std::vector<ParseAction*>* action = NULL;
|
std::vector<ParseAction*>* action = NULL;
|
||||||
|
|
||||||
if (symbolIndex < 0 || symbolIndex >= table[state]->size()) {
|
if (symbolIndex < 0 || symbolIndex >= table[state]->size()) {
|
||||||
std::cout << "Symbol bad for this state: " << token.toString() << ". This is a reject." << std::endl;
|
//std::cout << "Symbol bad for this state: " << token.toString() << ". This is a reject." << std::endl;
|
||||||
} else {
|
} else {
|
||||||
action = (*(table[state]))[symbolIndex];
|
action = (*(table[state]))[symbolIndex];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ Type::Type(std::string typeIn) {
|
|||||||
baseType = character;
|
baseType = character;
|
||||||
else
|
else
|
||||||
baseType = none;
|
baseType = none;
|
||||||
std::cout << ":ALKJF:LSKDJF:SDJF:LKSJDF\t\t\t" << typeIn << "\t" << edited << std::endl;
|
//std::cout << ":ALKJF:LSKDJF:SDJF:LKSJDF\t\t\t" << typeIn << "\t" << edited << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user