Changed Parser to be a virtual base class, inherited by LALRParser

This commit is contained in:
Nathan Braswell
2013-07-30 01:42:51 -04:00
parent 733fe1c08d
commit 3fba45591b
6 changed files with 128 additions and 79 deletions

View File

@@ -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/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 ) set( MY_SOURCES main.cpp src/Parser.cpp src/LALRParser.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 )
include_directories( ${MY_INCLUDES} ) include_directories( ${MY_INCLUDES} )

39
include/LALRParser.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef LALRPARSER_H
#define LALRPARSER_H
#include "Parser.h"
/*
#include "util.h"
#include "ParseRule.h"
#include "ParseAction.h"
#include "Symbol.h"
#include "State.h"
#include "StringReader.h"
#include "Lexer.h"
#include "NodeTree.h"
#include "Table.h"
#include <map>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#include <iostream>
*/
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

@@ -23,24 +23,24 @@ class Parser {
Parser(); Parser();
~Parser(); ~Parser();
void loadGrammer(std::string grammerInputString); virtual void loadGrammer(std::string grammerInputString);
std::vector<Symbol*>* firstSet(Symbol* token); virtual void createStateSet();
std::vector<Symbol*>* firstSet(Symbol* token, std::vector<Symbol*> &avoidList); virtual std::string stateSetToString();
std::vector<Symbol*>* incrementiveFollowSet(ParseRule* rule); virtual NodeTree<Symbol*>* parseInput(std::string inputString) = 0;
void createStateSet(); virtual std::string grammerToString();
void closure(State* state); virtual std::string grammerToDOT();
void addStates(std::vector< State* >* stateSets, State* state);
int stateNum(State* state);
std::string stateSetToString();
NodeTree<Symbol*>* parseInput(std::string inputString);
std::string grammerToString();
std::string grammerToDOT();
std::string tableToString(); std::string tableToString();
private: protected:
std::vector<Symbol*>* firstSet(Symbol* token);
std::vector<Symbol*>* firstSet(Symbol* token, std::vector<Symbol*> &avoidList);
std::vector<Symbol*>* incrementiveFollowSet(ParseRule* rule);
virtual void closure(State* state);
virtual void addStates(std::vector< State* >* stateSets, State* state);
int stateNum(State* state);
StringReader reader; StringReader reader;
Lexer lexer; Lexer lexer;
std::map<std::string, Symbol*> symbols; std::map<std::string, Symbol*> symbols;

View File

@@ -1,7 +1,7 @@
#include "NodeTree.h" #include "NodeTree.h"
#include "Symbol.h" #include "Symbol.h"
#include "Lexer.h" #include "Lexer.h"
#include "Parser.h" #include "LALRParser.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@@ -43,7 +43,7 @@ int main(int argc, char* argv[]) {
programInputFileString.append(line+"\n"); programInputFileString.append(line+"\n");
} }
Parser parser; LALRParser parser;
parser.loadGrammer(grammerInputFileString); parser.loadGrammer(grammerInputFileString);
//std::cout << "Creating State Set from Main" << std::endl; //std::cout << "Creating State Set from Main" << std::endl;
std::cout << "\n\n\n\n\n\n\n\n\n\nState Set" << std::endl; std::cout << "\n\n\n\n\n\n\n\n\n\nState Set" << std::endl;

70
src/LALRParser.cpp Normal file
View File

@@ -0,0 +1,70 @@
#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(new 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 new Symbol
Symbol* newSymbol = action->reduceRule->getLeftSide()->clone();
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;
}
}
}

View File

@@ -310,67 +310,7 @@ std::string Parser::tableToString() {
return table.toString(); return table.toString();
} }
NodeTree<Symbol*>* Parser::parseInput(std::string inputString) { //parseInput is now pure virtual
lexer.setInput(inputString);
Symbol* token = lexer.next();
std::vector<ParseAction*>* actionList;
ParseAction* action;
stateStack.push(0);
symbolStack.push(new 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 new Symbol
Symbol* newSymbol = action->reduceRule->getLeftSide()->clone();
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;
}
}
}
NodeTree<Symbol*>* Parser::reduceTreeCombine(Symbol* newSymbol, std::vector<Symbol*> &symbols) { NodeTree<Symbol*>* Parser::reduceTreeCombine(Symbol* newSymbol, std::vector<Symbol*> &symbols) {
NodeTree<Symbol*>* newTree = new NodeTree<Symbol*>(newSymbol->getName(), newSymbol); NodeTree<Symbol*>* newTree = new NodeTree<Symbol*>(newSymbol->getName(), newSymbol);