From 0110672f50d98497699f4a5e4223b12dfe0cc9c1 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Thu, 26 Sep 2013 15:16:58 -0400 Subject: [PATCH] Added in structure for tree transformations. --- include/ASTData.h | 29 ++++++++++++++++++++ include/ASTTransformation.h | 16 +++++++++++ include/NodeTransformation.h | 35 ++++++++++++++++++++++++ include/RemovalTransformation.h | 48 +++++++++++++++++++++++++++++++++ krakenGrammer.kgm | 1 - main.cpp | 32 +++++++++++++++++++--- src/ASTData.cpp | 17 ++++++++++++ src/ASTTransformation.cpp | 13 +++++++++ src/LALRParser.cpp | 3 +++ src/ParseAction.cpp | 2 ++ src/RNGLRParser.cpp | 2 +- 11 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 include/ASTData.h create mode 100644 include/ASTTransformation.h create mode 100644 include/NodeTransformation.h create mode 100644 include/RemovalTransformation.h create mode 100644 src/ASTData.cpp create mode 100644 src/ASTTransformation.cpp diff --git a/include/ASTData.h b/include/ASTData.h new file mode 100644 index 0000000..079b408 --- /dev/null +++ b/include/ASTData.h @@ -0,0 +1,29 @@ +#ifndef ASTDATA_H +#define ASTDATA_H + +#ifndef NULL +#define NULL 0 +#endif + +#include "Symbol.h" + +#include + +class ASTData { + public: + enum ASTType {translation_unit, interpreter_directive, identifier, + import, interpreter_directive, function, code_block, + typed_parameter, expression, boolean_expression, statement, + if_statement, return_statement, assignment_statement, function_call, + value}; + enum ValueType {none, boolean, integer, floating, double_percision, char_string } + + ASTData(ASTType type, ValueType valueType = none); + ASTData(ASTType type, Symbol* symbol, ValueType valueType = none); + ASTType type; + Symbol* symbol; + private: + +}; + +#endif \ No newline at end of file diff --git a/include/ASTTransformation.h b/include/ASTTransformation.h new file mode 100644 index 0000000..f2b3508 --- /dev/null +++ b/include/ASTTransformation.h @@ -0,0 +1,16 @@ +#ifndef ASTTRANSFORMATION_H +#define ASTTRANSFORMATION_H + +#include "NodeTransformation.h" + +class ASTTransformation: public Transformation { + public: + ASTTransformation(); + ~ASTTransformation(); + virtual NodeTree* transform(NodeTree* from); + + private: + //Nothing +}; + +#endif \ No newline at end of file diff --git a/include/NodeTransformation.h b/include/NodeTransformation.h new file mode 100644 index 0000000..dc18105 --- /dev/null +++ b/include/NodeTransformation.h @@ -0,0 +1,35 @@ +#ifndef NODETRANSFORMATION_H +#define NODETRANSFORMATION_H + +#include "NodeTree.h" + +#ifndef NULL +#define NULL 0 +#endif + +template +class NodeTransformation { + public: + NodeTransformation(); + virtual ~NodeTransformation(); + virtual NodeTree* transform(NodeTree* from)=0; + private: + +}; + +template +NodeTransformation::NodeTransformation() { + //Nothing +} + +template +NodeTransformation::~NodeTransformation() { + //Nothing +} + +// template +// NodeTree* NodeTransformation::transform(NodeTree* from) { +// return (NodeTree*)0x1234; +// } + +#endif \ No newline at end of file diff --git a/include/RemovalTransformation.h b/include/RemovalTransformation.h new file mode 100644 index 0000000..cc2eacd --- /dev/null +++ b/include/RemovalTransformation.h @@ -0,0 +1,48 @@ +#ifndef ASTTRANSFORMATION_H +#define ASTTRANSFORMATION_H + +#include +#include + +#include "NodeTransformation.h" + +template +class RemovalTransformation: public NodeTransformation { + public: + RemovalTransformation(T toRemove); + ~RemovalTransformation(); + virtual NodeTree* transform(NodeTree* from); + + private: + T toRemove; +}; + +#endif + +template +RemovalTransformation::RemovalTransformation(T toRemove) { + this->toRemove = toRemove; +} + +template +RemovalTransformation::~RemovalTransformation() { + // +} + +template +NodeTree* RemovalTransformation::transform(NodeTree* from) { + std::queue*> toProcess; + toProcess.push(from); + while(!toProcess.empty()) { + NodeTree* node = toProcess.front(); + toProcess.pop(); + std::vector*> children = node->getChildren(); + for (int i = 0; i < children.size(); i++) { + if (*(children[i]->getData()) == *toRemove) + node->removeChild(children[i]); + else + toProcess.push(children[i]); + } + } + return from; +} diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index 354810c..0b14dfc 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -61,4 +61,3 @@ bool = "true" | "false" | "True" | "False" ; alpha = "(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|_)+" ; numeric = "(0|1|2|3|4|5|6|7|8|9)+" ; string = "\"(a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z|.|!|_|-| | |\\|/|\||0|1|2|3|4|5|6|7|8|9)+\"" ; - diff --git a/main.cpp b/main.cpp index 8488c74..a79fd02 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,21 @@ +#include +#include +#include + #include "NodeTree.h" #include "Symbol.h" #include "Lexer.h" #include "LALRParser.h" #include "RNGLRParser.h" -#include -#include -#include + +#include "NodeTransformation.h" +#include "RemovalTransformation.h" int main(int argc, char* argv[]) { std::ifstream programInFile, grammerInFile; - std::ofstream outFile; + std::ofstream outFile, outFileTransformed; programInFile.open(argv[1]); if (!programInFile.is_open()) { @@ -31,6 +35,12 @@ int main(int argc, char* argv[]) { return(1); } + 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); + } + //Read the input file into a string std::string programInputFileString, grammerInputFileString; std::string line; @@ -72,11 +82,25 @@ int main(int argc, char* argv[]) { if (parseTree) { //std::cout << parseTree->DOTGraphString() << std::endl; outFile << parseTree->DOTGraphString() << std::endl; + } else { + std::cout << "ParseTree returned from parser is NULL!" << std::endl; } + NodeTransformation* removeWS = new RemovalTransformation(new Symbol("WS", false)); + NodeTree* noWhiteSpace = removeWS->transform(parseTree); + delete removeWS; + + if (noWhiteSpace) { + outFileTransformed << noWhiteSpace->DOTGraphString() << std::endl; + } else { + std::cout << "Tree returned from transformation is NULL!" << std::endl; + } + + programInFile.close(); grammerInFile.close(); outFile.close(); + outFileTransformed.close(); return(0); } diff --git a/src/ASTData.cpp b/src/ASTData.cpp new file mode 100644 index 0000000..5080907 --- /dev/null +++ b/src/ASTData.cpp @@ -0,0 +1,17 @@ +#include "ASTData.h" + +ASTData::ASTData(ASTType type, ValueType valueType) { + this->type = type; + this->valueType = valueType; + this->symbol = NULL; +} + +ASTData::ASTData(ASTType type, Symbol* symbol, ValueType valueType) { + this->type = type; + this->valueType = valueType; + this->symbol = symbol; +} + + +ASTData::~ASTData() { +} diff --git a/src/ASTTransformation.cpp b/src/ASTTransformation.cpp new file mode 100644 index 0000000..67f9321 --- /dev/null +++ b/src/ASTTransformation.cpp @@ -0,0 +1,13 @@ +#include "ASTTransformation.h" + +ASTTransformation::ASTTransformation() { + // +} + +ASTTransformation::~ASTTransformation() { + // +} + +virtual NodeTree* ASTTransformation::transform(NodeTree* from) { + return NULL; +} diff --git a/src/LALRParser.cpp b/src/LALRParser.cpp index b682b01..39ef40b 100644 --- a/src/LALRParser.cpp +++ b/src/LALRParser.cpp @@ -65,6 +65,9 @@ NodeTree* LALRParser::parseInput(std::string inputString) { std::cout << "REJECTED Symbol was " << token->toString() << std::endl; return(NULL); break; + default: + std::cout << "INVALID PARSE ACTION!" << std::endl; + break; } } } \ No newline at end of file diff --git a/src/ParseAction.cpp b/src/ParseAction.cpp index e8a304f..64f3e95 100644 --- a/src/ParseAction.cpp +++ b/src/ParseAction.cpp @@ -48,6 +48,8 @@ std::string ParseAction::actionToString(ActionType action) { case REJECT: return "reject"; break; + default: + return "INVALID PARSE ACTION"; } } diff --git a/src/RNGLRParser.cpp b/src/RNGLRParser.cpp index ec180a0..4fd4280 100644 --- a/src/RNGLRParser.cpp +++ b/src/RNGLRParser.cpp @@ -270,7 +270,7 @@ bool RNGLRParser::belongsToFamily(NodeTree* node, std::vector*>::size_type j = 0; j < children.size(); j++) { //Not sure where null comes from. For right now, just check to be sure we don't segfault - if ((*nodes)[i] == children[j] || (*nodes)[i] != NULL && children[j] != NULL && (*(*nodes)[i]) == *(children[j])) { + if ((*nodes)[i] == children[j] || ( (*nodes)[i] != NULL && children[j] != NULL && (*(*nodes)[i]) == *(children[j]) )) { containsOne = true; break; }