Added main loop of parser.
This commit is contained in:
@@ -4,7 +4,7 @@ project(Kraken)
|
|||||||
|
|
||||||
set( MY_INCLUDES ${PROJECT_SOURCE_DIR}/include)
|
set( MY_INCLUDES ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
set( MY_SOURCES main.cpp src/Parser.cpp src/ParseRule.cpp src/Symbol.cpp src/StringReader.cpp src/NodeTree.cpp src/Parser.cpp )
|
set( MY_SOURCES main.cpp src/Parser.cpp src/ParseAction.cpp src/ParseRule.cpp src/Symbol.cpp src/StringReader.cpp src/NodeTree.cpp )
|
||||||
|
|
||||||
include_directories( ${MY_INCLUDES} )
|
include_directories( ${MY_INCLUDES} )
|
||||||
|
|
||||||
|
|||||||
28
include/ParseAction.h
Normal file
28
include/ParseAction.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef PARSE_ACTION_H
|
||||||
|
#define PARSE_ACTION_H
|
||||||
|
|
||||||
|
#ifndef NULL
|
||||||
|
#define NULL 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "ParseRule.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class ParseAction {
|
||||||
|
public:
|
||||||
|
enum ActionType { INVALID, REDUCE, SHIFT, ACCEPT, REJECT };
|
||||||
|
ParseAction(ActionType action, ParseRule* reduceRule = NULL, int shiftState = 0);
|
||||||
|
~ParseAction();
|
||||||
|
std::string toString();
|
||||||
|
static std::string actionToString(ActionType action);
|
||||||
|
|
||||||
|
ActionType action;
|
||||||
|
ParseRule* reduceRule;
|
||||||
|
int shiftState;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -22,6 +22,9 @@ class ParseRule {
|
|||||||
void setLeftHandle(Symbol* leftHandle);
|
void setLeftHandle(Symbol* leftHandle);
|
||||||
void appendToRight(Symbol* appendee);
|
void appendToRight(Symbol* appendee);
|
||||||
|
|
||||||
|
Symbol* getLeftSide();
|
||||||
|
std::vector<Symbol*> getRightSide();
|
||||||
|
|
||||||
bool advancePointer();
|
bool advancePointer();
|
||||||
|
|
||||||
std::string toString();
|
std::string toString();
|
||||||
|
|||||||
@@ -6,11 +6,13 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "ParseRule.h"
|
#include "ParseRule.h"
|
||||||
|
#include "ParseAction.h"
|
||||||
#include "Symbol.h"
|
#include "Symbol.h"
|
||||||
#include "StringReader.h"
|
#include "StringReader.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <stack>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@@ -20,6 +22,10 @@ class Parser {
|
|||||||
~Parser();
|
~Parser();
|
||||||
|
|
||||||
void loadGrammer(std::string grammerInputString);
|
void loadGrammer(std::string grammerInputString);
|
||||||
|
int gotoTable(int state, Symbol* token);
|
||||||
|
ParseAction* actionTable(int state, Symbol* token);
|
||||||
|
void parseInput(std::string inputString);
|
||||||
|
|
||||||
std::string grammerToString();
|
std::string grammerToString();
|
||||||
std::string grammerToDOT();
|
std::string grammerToDOT();
|
||||||
private:
|
private:
|
||||||
@@ -27,6 +33,9 @@ class Parser {
|
|||||||
std::map<std::string, Symbol*> symbols;
|
std::map<std::string, Symbol*> symbols;
|
||||||
std::vector<ParseRule*> loadedGrammer;
|
std::vector<ParseRule*> loadedGrammer;
|
||||||
|
|
||||||
|
std::stack<int> stateStack;
|
||||||
|
std::stack<Symbol*> symbolStack;
|
||||||
|
|
||||||
Symbol* getOrAddSymbol(std::string symbolString, bool isTerminal);
|
Symbol* getOrAddSymbol(std::string symbolString, bool isTerminal);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ class Symbol {
|
|||||||
public:
|
public:
|
||||||
Symbol(std::string name, bool isTerminal);
|
Symbol(std::string name, bool isTerminal);
|
||||||
~Symbol();
|
~Symbol();
|
||||||
|
bool const operator==(const Symbol &other);
|
||||||
std::string toString();
|
std::string toString();
|
||||||
private:
|
private:
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|||||||
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);
|
rightSide.push_back(appendee);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Symbol* ParseRule::getLeftSide() {
|
||||||
|
return leftHandle;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Symbol*> ParseRule::getRightSide() {
|
||||||
|
return rightSide;
|
||||||
|
}
|
||||||
|
|
||||||
bool ParseRule::advancePointer() {
|
bool ParseRule::advancePointer() {
|
||||||
if (pointerIndex < rightSide.size()) {
|
if (pointerIndex < rightSide.size()) {
|
||||||
pointerIndex++;
|
pointerIndex++;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ Parser::~Parser() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Symbol* Parser::getOrAddSymbol(std::string symbolString, bool isTerminal) {
|
Symbol* Parser::getOrAddSymbol(std::string symbolString, bool isTerminal) {
|
||||||
Symbol* symbol;
|
Symbol* symbol;
|
||||||
if (symbols.find(symbolString) == symbols.end()) {
|
if (symbols.find(symbolString) == symbols.end()) {
|
||||||
@@ -52,6 +51,57 @@ void Parser::loadGrammer(std::string grammerInputString) {
|
|||||||
std::cout << "Parsed!\n";
|
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() {
|
std::string Parser::grammerToString() {
|
||||||
//Iterate through the vector, adding string representation of each grammer rule
|
//Iterate through the vector, adding string representation of each grammer rule
|
||||||
std::cout << "About to toString\n";
|
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() {
|
std::string Symbol::toString() {
|
||||||
return(name);
|
return(name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user