Added main loop of parser.
This commit is contained in:
37
src/ParseAction.cpp
Normal file
37
src/ParseAction.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "ParseAction.h"
|
||||
|
||||
ParseAction::ParseAction(ActionType action, ParseRule* reduceRule, int shiftState) {
|
||||
this->action = action;
|
||||
this->reduceRule = reduceRule;
|
||||
this->shiftState = shiftState;
|
||||
}
|
||||
|
||||
ParseAction::~ParseAction() {
|
||||
|
||||
}
|
||||
|
||||
std::string ParseAction::actionToString(ActionType action) {
|
||||
switch (action) {
|
||||
case REDUCE:
|
||||
return "reduce";
|
||||
break;
|
||||
case SHIFT:
|
||||
return "shift";
|
||||
break;
|
||||
case ACCEPT:
|
||||
return "accept";
|
||||
break;
|
||||
case REJECT:
|
||||
return "reject";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ParseAction::toString() {
|
||||
std::string outputString = actionToString(action);
|
||||
if (reduceRule)
|
||||
outputString += " " + reduceRule->toString();
|
||||
if (shiftState)
|
||||
outputString += " " + shiftState;
|
||||
return(outputString);
|
||||
}
|
||||
@@ -27,6 +27,14 @@ void ParseRule::appendToRight(Symbol* appendee) {
|
||||
rightSide.push_back(appendee);
|
||||
}
|
||||
|
||||
Symbol* ParseRule::getLeftSide() {
|
||||
return leftHandle;
|
||||
}
|
||||
|
||||
std::vector<Symbol*> ParseRule::getRightSide() {
|
||||
return rightSide;
|
||||
}
|
||||
|
||||
bool ParseRule::advancePointer() {
|
||||
if (pointerIndex < rightSide.size()) {
|
||||
pointerIndex++;
|
||||
|
||||
@@ -8,7 +8,6 @@ Parser::~Parser() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
Symbol* Parser::getOrAddSymbol(std::string symbolString, bool isTerminal) {
|
||||
Symbol* symbol;
|
||||
if (symbols.find(symbolString) == symbols.end()) {
|
||||
@@ -52,6 +51,57 @@ void Parser::loadGrammer(std::string grammerInputString) {
|
||||
std::cout << "Parsed!\n";
|
||||
}
|
||||
|
||||
int Parser::gotoTable(int state, Symbol* token) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ParseAction* Parser::actionTable(int state, Symbol* token) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Parser::parseInput(std::string inputString) {
|
||||
StringReader inputReader;
|
||||
inputReader.setString(inputString);
|
||||
|
||||
Symbol* token = new Symbol(reader.word(), false);
|
||||
ParseAction* action;
|
||||
|
||||
stateStack.push(0);
|
||||
symbolStack.push(new Symbol("INVALID", false));
|
||||
|
||||
while (true) {
|
||||
action = actionTable(stateStack.top(), token);
|
||||
switch (action->action) {
|
||||
case ParseAction::REDUCE:
|
||||
{
|
||||
int rightSideLength = action->reduceRule->getRightSide().size();
|
||||
for (int i = 0; i < rightSideLength; i++) {
|
||||
stateStack.pop();
|
||||
symbolStack.pop();
|
||||
}
|
||||
symbolStack.push(action->reduceRule->getLeftSide());
|
||||
stateStack.push(gotoTable(stateStack.top(), symbolStack.top()));
|
||||
std::cout << "Reduce by " << action->reduceRule->toString() << std::endl;
|
||||
break;
|
||||
}
|
||||
case ParseAction::SHIFT:
|
||||
symbolStack.push(token);
|
||||
token = new Symbol(inputReader.word(), false);
|
||||
stateStack.push(action->shiftState);
|
||||
std::cout << "Shift " << symbolStack.top()->toString() << std::endl;
|
||||
break;
|
||||
case ParseAction::ACCEPT:
|
||||
std::cout << "ACCEPTED!" << std::endl;
|
||||
return;
|
||||
break;
|
||||
case ParseAction::REJECT:
|
||||
std::cout << "REJECTED!" << std::endl;
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string Parser::grammerToString() {
|
||||
//Iterate through the vector, adding string representation of each grammer rule
|
||||
std::cout << "About to toString\n";
|
||||
|
||||
@@ -9,6 +9,10 @@ Symbol::~Symbol() {
|
||||
|
||||
}
|
||||
|
||||
const bool Symbol::operator==(const Symbol &other) {
|
||||
return( name == other.name && isTerminal == other.isTerminal);
|
||||
}
|
||||
|
||||
std::string Symbol::toString() {
|
||||
return(name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user