Added in structure for tree transformations.
This commit is contained in:
29
include/ASTData.h
Normal file
29
include/ASTData.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef ASTDATA_H
|
||||||
|
#define ASTDATA_H
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
#define NULL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "Symbol.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
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
|
||||||
16
include/ASTTransformation.h
Normal file
16
include/ASTTransformation.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef ASTTRANSFORMATION_H
|
||||||
|
#define ASTTRANSFORMATION_H
|
||||||
|
|
||||||
|
#include "NodeTransformation.h"
|
||||||
|
|
||||||
|
class ASTTransformation: public Transformation<Symbol*,ASTData> {
|
||||||
|
public:
|
||||||
|
ASTTransformation();
|
||||||
|
~ASTTransformation();
|
||||||
|
virtual NodeTree<Symbol*>* transform(NodeTree<ASTData>* from);
|
||||||
|
|
||||||
|
private:
|
||||||
|
//Nothing
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
35
include/NodeTransformation.h
Normal file
35
include/NodeTransformation.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#ifndef NODETRANSFORMATION_H
|
||||||
|
#define NODETRANSFORMATION_H
|
||||||
|
|
||||||
|
#include "NodeTree.h"
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
#define NULL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template <class FROM, class TO>
|
||||||
|
class NodeTransformation {
|
||||||
|
public:
|
||||||
|
NodeTransformation();
|
||||||
|
virtual ~NodeTransformation();
|
||||||
|
virtual NodeTree<TO>* transform(NodeTree<FROM>* from)=0;
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class FROM, class TO>
|
||||||
|
NodeTransformation<FROM,TO>::NodeTransformation() {
|
||||||
|
//Nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class FROM, class TO>
|
||||||
|
NodeTransformation<FROM,TO>::~NodeTransformation() {
|
||||||
|
//Nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
// template <class FROM, class TO>
|
||||||
|
// NodeTree<TO>* NodeTransformation<FROM,TO>::transform(NodeTree<FROM>* from) {
|
||||||
|
// return (NodeTree<TO>*)0x1234;
|
||||||
|
// }
|
||||||
|
|
||||||
|
#endif
|
||||||
48
include/RemovalTransformation.h
Normal file
48
include/RemovalTransformation.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef ASTTRANSFORMATION_H
|
||||||
|
#define ASTTRANSFORMATION_H
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "NodeTransformation.h"
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class RemovalTransformation: public NodeTransformation<T,T> {
|
||||||
|
public:
|
||||||
|
RemovalTransformation(T toRemove);
|
||||||
|
~RemovalTransformation();
|
||||||
|
virtual NodeTree<T>* transform(NodeTree<T>* from);
|
||||||
|
|
||||||
|
private:
|
||||||
|
T toRemove;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
RemovalTransformation<T>::RemovalTransformation(T toRemove) {
|
||||||
|
this->toRemove = toRemove;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
RemovalTransformation<T>::~RemovalTransformation() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
NodeTree<T>* RemovalTransformation<T>::transform(NodeTree<T>* from) {
|
||||||
|
std::queue<NodeTree<T>*> toProcess;
|
||||||
|
toProcess.push(from);
|
||||||
|
while(!toProcess.empty()) {
|
||||||
|
NodeTree<T>* node = toProcess.front();
|
||||||
|
toProcess.pop();
|
||||||
|
std::vector<NodeTree<T>*> 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;
|
||||||
|
}
|
||||||
@@ -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|.|_)+" ;
|
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)+" ;
|
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)+\"" ;
|
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)+\"" ;
|
||||||
|
|
||||||
|
|||||||
32
main.cpp
32
main.cpp
@@ -1,17 +1,21 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
#include "NodeTree.h"
|
#include "NodeTree.h"
|
||||||
#include "Symbol.h"
|
#include "Symbol.h"
|
||||||
#include "Lexer.h"
|
#include "Lexer.h"
|
||||||
#include "LALRParser.h"
|
#include "LALRParser.h"
|
||||||
#include "RNGLRParser.h"
|
#include "RNGLRParser.h"
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include "NodeTransformation.h"
|
||||||
#include <fstream>
|
#include "RemovalTransformation.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
std::ifstream programInFile, grammerInFile;
|
std::ifstream programInFile, grammerInFile;
|
||||||
std::ofstream outFile;
|
std::ofstream outFile, outFileTransformed;
|
||||||
|
|
||||||
programInFile.open(argv[1]);
|
programInFile.open(argv[1]);
|
||||||
if (!programInFile.is_open()) {
|
if (!programInFile.is_open()) {
|
||||||
@@ -31,6 +35,12 @@ int main(int argc, char* argv[]) {
|
|||||||
return(1);
|
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
|
//Read the input file into a string
|
||||||
std::string programInputFileString, grammerInputFileString;
|
std::string programInputFileString, grammerInputFileString;
|
||||||
std::string line;
|
std::string line;
|
||||||
@@ -72,11 +82,25 @@ int main(int argc, char* argv[]) {
|
|||||||
if (parseTree) {
|
if (parseTree) {
|
||||||
//std::cout << parseTree->DOTGraphString() << std::endl;
|
//std::cout << parseTree->DOTGraphString() << std::endl;
|
||||||
outFile << parseTree->DOTGraphString() << std::endl;
|
outFile << parseTree->DOTGraphString() << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "ParseTree returned from parser is NULL!" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NodeTransformation<Symbol*, Symbol*>* removeWS = new RemovalTransformation<Symbol*>(new Symbol("WS", false));
|
||||||
|
NodeTree<Symbol*>* 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();
|
programInFile.close();
|
||||||
grammerInFile.close();
|
grammerInFile.close();
|
||||||
outFile.close();
|
outFile.close();
|
||||||
|
outFileTransformed.close();
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/ASTData.cpp
Normal file
17
src/ASTData.cpp
Normal file
@@ -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() {
|
||||||
|
}
|
||||||
13
src/ASTTransformation.cpp
Normal file
13
src/ASTTransformation.cpp
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
#include "ASTTransformation.h"
|
||||||
|
|
||||||
|
ASTTransformation::ASTTransformation() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTTransformation::~ASTTransformation() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual NodeTree<Symbol*>* ASTTransformation::transform(NodeTree<ASTData>* from) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
@@ -65,6 +65,9 @@ NodeTree<Symbol*>* LALRParser::parseInput(std::string inputString) {
|
|||||||
std::cout << "REJECTED Symbol was " << token->toString() << std::endl;
|
std::cout << "REJECTED Symbol was " << token->toString() << std::endl;
|
||||||
return(NULL);
|
return(NULL);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
std::cout << "INVALID PARSE ACTION!" << std::endl;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,6 +48,8 @@ std::string ParseAction::actionToString(ActionType action) {
|
|||||||
case REJECT:
|
case REJECT:
|
||||||
return "reject";
|
return "reject";
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
return "INVALID PARSE ACTION";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -270,7 +270,7 @@ bool RNGLRParser::belongsToFamily(NodeTree<Symbol*>* node, std::vector<NodeTree<
|
|||||||
bool containsOne = false;
|
bool containsOne = false;
|
||||||
for (std::vector<NodeTree<Symbol*>*>::size_type j = 0; j < children.size(); j++) {
|
for (std::vector<NodeTree<Symbol*>*>::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
|
//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;
|
containsOne = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user