From 6fa06f2b7e373dfa33e35985a4531d2f06be613f Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Mon, 23 Mar 2015 15:28:03 -0400 Subject: [PATCH] Removed LALR parser and an associated member from Symbol, which is used so much that actually got us down to ~3.42GB! --- CMakeLists.txt | 2 +- include/LALRParser.h | 22 ------------- include/Parser.h | 1 - include/Symbol.h | 3 +- krakenGrammer.kgm | 2 +- main.cpp | 1 - src/LALRParser.cpp | 73 -------------------------------------------- src/Parser.cpp | 19 +++--------- src/RNGLRParser.cpp | 4 ++- src/Symbol.cpp | 17 ----------- 10 files changed, 11 insertions(+), 133 deletions(-) delete mode 100644 include/LALRParser.h delete mode 100644 src/LALRParser.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8dc844b..e429b9a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set( MY_INCLUDES ${PROJECT_SOURCE_DIR}/include) -set( MY_SOURCES main.cpp src/Parser.cpp src/LALRParser.cpp src/GraphStructuredStack.cpp src/RNGLRParser.cpp src/ParseAction.cpp src/ParseRule.cpp src/Symbol.cpp src/StringReader.cpp src/State.cpp src/util.cpp src/Lexer.cpp src/RegEx.cpp src/RegExState.cpp src/Table.cpp src/ASTData.cpp src/ASTTransformation.cpp src/CGenerator.cpp src/Type.cpp src/Importer.cpp src/Tester.cpp ) +set( MY_SOURCES main.cpp src/Parser.cpp src/GraphStructuredStack.cpp src/RNGLRParser.cpp src/ParseAction.cpp src/ParseRule.cpp src/Symbol.cpp src/StringReader.cpp src/State.cpp src/util.cpp src/Lexer.cpp src/RegEx.cpp src/RegExState.cpp src/Table.cpp src/ASTData.cpp src/ASTTransformation.cpp src/CGenerator.cpp src/Type.cpp src/Importer.cpp src/Tester.cpp ) add_custom_target(STDLibCopy ALL) add_custom_command(TARGET STDLibCopy POST_BUILD diff --git a/include/LALRParser.h b/include/LALRParser.h deleted file mode 100644 index 6f36034..0000000 --- a/include/LALRParser.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef LALRPARSER_H -#define LALRPARSER_H - -#include "Parser.h" - -class LALRParser: public Parser { - public: - LALRParser(); - ~LALRParser(); - - //using Parser::loadGrammer; - - //Defaults in parser are mostly LALR, so we only need to - //implement the actual parsing function - NodeTree* parseInput(std::string inputString); - - private: - //Nothing - -}; - -#endif \ No newline at end of file diff --git a/include/Parser.h b/include/Parser.h index cd9a9f8..73f878d 100644 --- a/include/Parser.h +++ b/include/Parser.h @@ -68,7 +68,6 @@ class Parser { std::stack symbolStack; Symbol getOrAddSymbol(std::string symbolString, bool isTerminal); - NodeTree* reduceTreeCombine(Symbol newSymbol, std::vector &symbols); }; #endif diff --git a/include/Symbol.h b/include/Symbol.h index 8516555..34e07dc 100644 --- a/include/Symbol.h +++ b/include/Symbol.h @@ -32,7 +32,6 @@ class Symbol { std::string name; std::string value; bool terminal; - NodeTree* subTree; }; -#endif \ No newline at end of file +#endif diff --git a/krakenGrammer.kgm b/krakenGrammer.kgm index e8dcb48..7f45387 100644 --- a/krakenGrammer.kgm +++ b/krakenGrammer.kgm @@ -21,7 +21,7 @@ path_part = forward_slash alphanumeric | back_slash alphanumeric ; forward_slash = "/" ; back_slash = "\\" ; -# all for optional semicolons +# all for optional semicolons k line_break = " +" ; actual_white = "( | )+" | line_break | line_break actual_white | "( | )+" actual_white ; diff --git a/main.cpp b/main.cpp index cb1d8d1..6f51cb9 100644 --- a/main.cpp +++ b/main.cpp @@ -8,7 +8,6 @@ #include "NodeTree.h" #include "Symbol.h" #include "Lexer.h" -#include "LALRParser.h" #include "RNGLRParser.h" #include "Importer.h" diff --git a/src/LALRParser.cpp b/src/LALRParser.cpp deleted file mode 100644 index 0f391ce..0000000 --- a/src/LALRParser.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "LALRParser.h" - -LALRParser::LALRParser() { - //Nothing to do in this version -} -LALRParser::~LALRParser() { - //Nothing to do in this version -} - -NodeTree* LALRParser::parseInput(std::string inputString) { - lexer.setInput(inputString); - Symbol token = lexer.next(); - std::vector* actionList; - ParseAction* action; - - stateStack.push(0); - symbolStack.push(Symbol("INVALID", false)); - - while (true) { - std::cout << "In state: " << intToString(stateStack.top()) << std::endl; - actionList = table.get(stateStack.top(), token); - action = (*(actionList))[actionList->size()-1]; - //std::cout << "Doing ParseAction: " << action->toString() << std::endl; - switch (action->action) { - case ParseAction::REDUCE: - { - std::cout << "Reduce by " << action->reduceRule->toString() << std::endl; - - int rightSideLength = action->reduceRule->getRightSide().size(); - //Keep track of symbols popped for parse tree - std::vector poppedSymbols; - for (int i = 0; i < rightSideLength; i++) { - poppedSymbols.push_back(symbolStack.top()); - stateStack.pop(); - symbolStack.pop(); - } - std::reverse(poppedSymbols.begin(), poppedSymbols.end()); //To put in order - //Assign the new tree to the Symbol - Symbol newSymbol = action->reduceRule->getLeftSide(); - newSymbol.setSubTree(reduceTreeCombine(newSymbol, poppedSymbols)); - symbolStack.push(newSymbol); - std::cout << "top of state is " << intToString(stateStack.top()) << " symbolStack top is " << symbolStack.top().toString() << std::endl; - - actionList = table.get(stateStack.top(), symbolStack.top()); - action = (*(actionList))[actionList->size()-1]; - - stateStack.push(action->shiftState); - //std::cout << "Reduced, now condition is" << std::endl; - //std::cout << "top of state is " << intToString(stateStack.top()) << " symbolStack top is " << symbolStack.top()->toString() << std::endl; - break; - } - case ParseAction::SHIFT: - std::cout << "Shift " << token.toString() << std::endl; - - symbolStack.push(token); - token = lexer.next(); - stateStack.push(action->shiftState); - break; - case ParseAction::ACCEPT: - std::cout << "ACCEPTED!" << std::endl; - return(symbolStack.top().getSubTree()); - break; - case ParseAction::REJECT: - std::cout << "REJECTED!" << std::endl; - 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/Parser.cpp b/src/Parser.cpp index c19d421..91e8742 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -38,9 +38,11 @@ void Parser::loadGrammer(std::string grammerInputString) { //First, if this starts with a '#', skip this if (currToken.front() == '#') { //If this line is more than one token long, eat it - std::cout << "Ate: " << currToken << std::endl; - if (currToken.back() != '\n') - std::cout << "Eating " << reader.line() << " b/c grammer comment" << std::endl; + //std::cout << "Ate: " << currToken << std::endl; + if (currToken.back() != '\n') { + std::string ate = reader.line(); + //std::cout << "Eating " << ate << " b/c grammer comment" << std::endl; + } currToken = reader.word(false); continue; } @@ -375,17 +377,6 @@ std::string Parser::tableToString() { //parseInput is now pure virtual -NodeTree* Parser::reduceTreeCombine(Symbol newSymbol, std::vector &symbols) { - NodeTree* newTree = new NodeTree(newSymbol.getName(), newSymbol); - for (std::vector::size_type i = 0; i < symbols.size(); i++) { - if (symbols[i].isTerminal()) - newTree->addChild(new NodeTree(symbols[i].getName(), symbols[i])); - else - newTree->addChild(symbols[i].getSubTree()); - } - return(newTree); -} - std::string Parser::grammerToString() { //Iterate through the vector, adding string representation of each grammer rule std::cout << "About to toString\n"; diff --git a/src/RNGLRParser.cpp b/src/RNGLRParser.cpp index 6e17ce1..676bd3d 100644 --- a/src/RNGLRParser.cpp +++ b/src/RNGLRParser.cpp @@ -359,7 +359,9 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std: State* newState = new State(stateSets->size()+newStates.size(),advancedRule, state); newStates.push_back(newState); } - } + } else { + delete advancedRule; + } } //Put all our new states in the set of states only if they're not already there. bool stateAlreadyInAllStates = false; diff --git a/src/Symbol.cpp b/src/Symbol.cpp index 4ccb772..8a1fcec 100644 --- a/src/Symbol.cpp +++ b/src/Symbol.cpp @@ -3,30 +3,21 @@ Symbol::Symbol() { this->name = "UninitlizedSymbol"; this->terminal = false; - this->subTree = NULL; value = "NoValue"; } Symbol::Symbol(std::string name, bool isTerminal) { this->name = name; this->terminal = isTerminal; - this->subTree = NULL; value = "NoValue"; } Symbol::Symbol(std::string name, bool isTerminal, std::string value) { this->name = name; this->terminal = isTerminal; - this->subTree = NULL; this->value = value; } -Symbol::Symbol(std::string name, bool isTerminal, NodeTree* tree) { - this->name = name; - this->terminal = isTerminal; - this->subTree = tree; -} - Symbol::~Symbol() { } @@ -55,14 +46,6 @@ std::string Symbol::toString() const { return(name + (terminal ? " " + value : "")); } -void Symbol::setSubTree(NodeTree* tree) { - subTree = tree; -} - -NodeTree* Symbol::getSubTree() { - return subTree; -} - bool Symbol::isTerminal() { return terminal; }