Basic header for LR tables and generator

This commit is contained in:
Robin Jadoul 2016-11-25 16:31:45 +01:00
parent 55e228fc57
commit baa5c04d29
2 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,60 @@
#pragma once
#ifndef LRTABLES_GENERATOR_H_YW3GIUNH
#define LRTABLES_GENERATOR_H_YW3GIUNH
#include "Parsodus/grammar.h"
#include "Parsodus/lrtables/table.h"
namespace pds {
namespace lr {
/**
* Base class for LR (and derivative) table generators (such as SLR and LALR)
* Parametrized on the type of item to be used in the configuration sets
*/
template <typename Item>
class Generator {
public:
/**
* Constructor
*
* @param g The grammar to translate
*/
Generator(const Grammar& g);
/**
* Generate an LRTable based on given grammar
*
* @returns An LR (or derivative) table for the grammar given at construction
*/
LRTable generate();
protected:
/**
* Indicate whether this specific algorithm needs to have the First and Follow sets generated
*/
virtual bool needsFollowSet() = 0;
std::set<std::string> first(std::string s);
std::set<std::string> follow(std::string s);
private:
/**
* Build the `First` set
*/
void buildFirst();
/**
* Build the `Follow` set
*/
void buildFollow();
Grammar m_gram;
std::map<std::string, std::set<std::string>> m_first;
std::map<std::string, std::set<std::string>> m_follow;
};
} /* lr */
} /* pdf */
#endif /* LRTABLES_GENERATOR_H_YW3GIUNH */

View File

@ -0,0 +1,28 @@
#pragma once
#ifndef LRTABLES_TABLE_H_4JGXOTCZ
#define LRTABLES_TABLE_H_4JGXOTCZ
#include <map>
#include <string>
#include <vector>
#include <utility>
namespace pds {
namespace lr {
enum class Action {
ERROR,
SHIFT,
REDUCE,
ACCEPT
};
struct LRTable {
std::vector<std::map<std::string, std::pair<Action, std::size_t>>> act; ///< indexed on state number, then on terminal -> action to take + (next state | rule applied)
std::vector<std::map<std::string, size_t>> goto_; ///< indexed on state number, then on nonterminal -> next state
};
} /* lr */
} /* pds */
#endif /* LRTABLES_TABLE_H_4JGXOTCZ */