2013-05-20 19:34:15 -04:00
|
|
|
#ifndef PARSER_H
|
|
|
|
|
#define PARSER_H
|
|
|
|
|
|
2013-06-04 19:50:16 -04:00
|
|
|
#include "util.h"
|
2013-05-20 19:34:15 -04:00
|
|
|
#include "ParseRule.h"
|
2013-05-23 01:35:54 -04:00
|
|
|
#include "ParseAction.h"
|
2013-05-20 19:34:15 -04:00
|
|
|
#include "Symbol.h"
|
2013-05-26 22:12:47 -04:00
|
|
|
#include "State.h"
|
2013-05-20 19:34:15 -04:00
|
|
|
#include "StringReader.h"
|
2013-06-13 14:25:10 -04:00
|
|
|
#include "Lexer.h"
|
2013-05-30 19:49:19 -04:00
|
|
|
#include "NodeTree.h"
|
2013-07-16 11:15:58 -04:00
|
|
|
#include "Table.h"
|
2013-05-20 19:34:15 -04:00
|
|
|
|
2013-08-22 15:41:30 -04:00
|
|
|
#include <queue>
|
2014-06-30 01:57:50 -07:00
|
|
|
#include <set>
|
2013-05-20 19:34:15 -04:00
|
|
|
#include <map>
|
|
|
|
|
#include <vector>
|
2013-05-30 19:49:19 -04:00
|
|
|
#include <algorithm>
|
2013-05-23 01:35:54 -04:00
|
|
|
#include <stack>
|
2013-05-20 19:34:15 -04:00
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
class Parser {
|
|
|
|
|
public:
|
|
|
|
|
Parser();
|
|
|
|
|
~Parser();
|
|
|
|
|
|
2013-07-30 01:42:51 -04:00
|
|
|
virtual void loadGrammer(std::string grammerInputString);
|
|
|
|
|
virtual void createStateSet();
|
|
|
|
|
virtual std::string stateSetToString();
|
2013-10-02 03:15:20 -04:00
|
|
|
virtual NodeTree<Symbol>* parseInput(std::string inputString) = 0;
|
2013-07-30 01:42:51 -04:00
|
|
|
virtual std::string grammerToString();
|
|
|
|
|
virtual std::string grammerToDOT();
|
|
|
|
|
|
|
|
|
|
std::string tableToString();
|
2013-12-31 02:53:52 -06:00
|
|
|
void exportTable(std::ofstream &file);
|
|
|
|
|
void importTable(char* tableData);
|
2013-07-30 01:42:51 -04:00
|
|
|
|
|
|
|
|
protected:
|
2014-06-30 01:57:50 -07:00
|
|
|
std::vector<Symbol> firstSet(Symbol token, std::vector<Symbol> avoidList = std::vector<Symbol>(), bool addNewTokens = true);
|
|
|
|
|
bool isNullable(Symbol token);
|
|
|
|
|
bool isNullableHelper(Symbol token, std::set<Symbol> done);
|
|
|
|
|
|
|
|
|
|
std::map<Symbol, std::vector<Symbol>> tokenFirstSet;
|
|
|
|
|
std::map<Symbol, bool> tokenNullable;
|
|
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
std::vector<Symbol>* incrementiveFollowSet(ParseRule* rule);
|
2013-07-30 01:42:51 -04:00
|
|
|
virtual void closure(State* state);
|
2013-08-22 15:41:30 -04:00
|
|
|
virtual void addStates(std::vector< State* >* stateSets, State* state, std::queue<State*>* toDo);
|
2013-07-16 11:15:58 -04:00
|
|
|
int stateNum(State* state);
|
|
|
|
|
|
2013-06-04 19:50:16 -04:00
|
|
|
|
2013-05-20 19:34:15 -04:00
|
|
|
StringReader reader;
|
2013-07-02 01:47:42 -04:00
|
|
|
Lexer lexer;
|
2013-10-02 03:15:20 -04:00
|
|
|
std::map<std::pair<std::string, bool>, Symbol> symbols;
|
2013-05-20 19:34:15 -04:00
|
|
|
std::vector<ParseRule*> loadedGrammer;
|
|
|
|
|
|
2013-05-26 22:12:47 -04:00
|
|
|
std::vector< State* > stateSets;
|
2013-05-24 00:00:41 -04:00
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
Symbol EOFSymbol;
|
|
|
|
|
Symbol nullSymbol;
|
2013-11-01 02:52:18 -04:00
|
|
|
Symbol invalidSymbol;
|
2013-07-09 02:45:59 -04:00
|
|
|
|
2013-07-16 11:15:58 -04:00
|
|
|
Table table;
|
|
|
|
|
|
2013-05-29 20:43:35 -04:00
|
|
|
|
2013-05-23 01:35:54 -04:00
|
|
|
std::stack<int> stateStack;
|
2013-10-02 03:15:20 -04:00
|
|
|
std::stack<Symbol> symbolStack;
|
2013-05-23 01:35:54 -04:00
|
|
|
|
2013-10-02 03:15:20 -04:00
|
|
|
Symbol getOrAddSymbol(std::string symbolString, bool isTerminal);
|
|
|
|
|
NodeTree<Symbol>* reduceTreeCombine(Symbol newSymbol, std::vector<Symbol> &symbols);
|
2013-05-20 19:34:15 -04:00
|
|
|
};
|
|
|
|
|
|
2014-06-30 01:57:50 -07:00
|
|
|
#endif
|