From 4d73c6c6f200fb041953374852bb828048d2aa74 Mon Sep 17 00:00:00 2001 From: satunreric Date: Tue, 27 Apr 2021 12:04:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E6=B7=BB=E5=8A=A0=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 108 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index c80d511..17b19aa 100644 --- a/main.cpp +++ b/main.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include using namespace std; @@ -18,6 +18,7 @@ using std::pair; using std::wcout; using std::endl; using std::to_string; +using std::hash; struct Symbol { @@ -120,13 +121,113 @@ struct Production { // 项 class Item{ // 对应的产生式 - Production* production; + const Production* const production; + // 点的位置 - int dotIndex = 0; + int dot_index = 0; + + const int terminator = 0; + + public: - explicit Item(Production *p_pdt) { - production = p_pdt; + explicit Item(Production *p_pdt, int m_terminator) : production(p_pdt), terminator(m_terminator) {} + + void set_dot_index(int m_dot_index) { + if(m_dot_index > production->right.size()) { + throw runtime_error("DOT_INDEX out of range"); + } + this->dot_index = m_dot_index; } + + int get_dot_index() const { + return dot_index; + } + + int get_dot_next_symbol() { + if(get_dot_index() == production->right.size()) { + return 0; + } else { + return production->right[dot_index]; + } + } +}; + +class ItemCollectionManager; + +class ItemCollection{ + + int index = 0; + + vector items; + + Production *G; + + friend ItemCollectionManager; + +public: + + void addItem(Production *p_pdt, int dot_index, int terminator) { + auto *p_item = new Item(p_pdt, terminator); + p_item->set_dot_index(dot_index); + items.push_back(p_item); + } + + void CLOSURE() { + bool ifAdd = true; + + while(ifAdd) { + ifAdd = false; + + for(const auto & item : items) { + if(item->get_dot_next_symbol() == 0) { + continue; + } + // TODO + + } + + } + } + +}; + +class ItemCollectionManager{ + + int index = 1; + + map ic_map; + + vector ics; + + template + inline void hash_combine(std::size_t& seed, const T& v) + { + std::hash hasher; + seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2); + } + +public: + + const vector &getItemCollections() { + return ics; + } + + bool addItemCollection(int idx, int terminator, ItemCollection *p_ic){ + auto hasher = hash(); + size_t seed = hasher(idx); + hash_combine(seed, terminator); + + auto it = ic_map.find(seed); + if(it != ic_map.end()) { + return false; + } else { + p_ic->index = this->index++; + ic_map.insert(pair(seed, p_ic)); + ics.push_back(p_ic); + return true; + } + } + }; class Generator{ @@ -146,6 +247,8 @@ class Generator{ // FOLLOW结果存储表 map *> follows; + map C; + // 去掉首尾空格 static wstring& trim(wstring &&str) { if (str.empty()) {