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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user