From baa5c04d29c2dac46e0c7a5f87a7889be513e2ff Mon Sep 17 00:00:00 2001
From: Robin Jadoul <robin.jadoul@gmail.com>
Date: Fri, 25 Nov 2016 16:31:45 +0100
Subject: [PATCH] Basic header for LR tables and generator

---
 include/Parsodus/lrtables/generator.h | 60 +++++++++++++++++++++++++++
 include/Parsodus/lrtables/table.h     | 28 +++++++++++++
 2 files changed, 88 insertions(+)
 create mode 100644 include/Parsodus/lrtables/generator.h
 create mode 100644 include/Parsodus/lrtables/table.h

diff --git a/include/Parsodus/lrtables/generator.h b/include/Parsodus/lrtables/generator.h
new file mode 100644
index 0000000..f49988d
--- /dev/null
+++ b/include/Parsodus/lrtables/generator.h
@@ -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 */
diff --git a/include/Parsodus/lrtables/table.h b/include/Parsodus/lrtables/table.h
new file mode 100644
index 0000000..4e298dc
--- /dev/null
+++ b/include/Parsodus/lrtables/table.h
@@ -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 */