Removed LALR parser and an associated member from Symbol, which is used so much that actually got us down to ~3.42GB!

This commit is contained in:
Nathan Braswell
2015-03-23 15:28:03 -04:00
parent 2c4dbc60d1
commit 6fa06f2b7e
10 changed files with 11 additions and 133 deletions

View File

@@ -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

View File

@@ -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<Symbol>* parseInput(std::string inputString);
private:
//Nothing
};
#endif

View File

@@ -68,7 +68,6 @@ class Parser {
std::stack<Symbol> symbolStack;
Symbol getOrAddSymbol(std::string symbolString, bool isTerminal);
NodeTree<Symbol>* reduceTreeCombine(Symbol newSymbol, std::vector<Symbol> &symbols);
};
#endif

View File

@@ -32,7 +32,6 @@ class Symbol {
std::string name;
std::string value;
bool terminal;
NodeTree<Symbol>* subTree;
};
#endif
#endif

View File

@@ -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 ;

View File

@@ -8,7 +8,6 @@
#include "NodeTree.h"
#include "Symbol.h"
#include "Lexer.h"
#include "LALRParser.h"
#include "RNGLRParser.h"
#include "Importer.h"

View File

@@ -1,73 +0,0 @@
#include "LALRParser.h"
LALRParser::LALRParser() {
//Nothing to do in this version
}
LALRParser::~LALRParser() {
//Nothing to do in this version
}
NodeTree<Symbol>* LALRParser::parseInput(std::string inputString) {
lexer.setInput(inputString);
Symbol token = lexer.next();
std::vector<ParseAction*>* 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<Symbol> 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;
}
}
}

View File

@@ -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<Symbol>* Parser::reduceTreeCombine(Symbol newSymbol, std::vector<Symbol> &symbols) {
NodeTree<Symbol>* newTree = new NodeTree<Symbol>(newSymbol.getName(), newSymbol);
for (std::vector<Symbol>::size_type i = 0; i < symbols.size(); i++) {
if (symbols[i].isTerminal())
newTree->addChild(new NodeTree<Symbol>(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";

View File

@@ -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;

View File

@@ -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<Symbol>* 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<Symbol>* tree) {
subTree = tree;
}
NodeTree<Symbol>* Symbol::getSubTree() {
return subTree;
}
bool Symbol::isTerminal() {
return terminal;
}