修正代码
This commit is contained in:
parent
70ac1bb025
commit
f7355e63e2
@ -1,6 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.17)
|
cmake_minimum_required(VERSION 3.17)
|
||||||
project(syntaxParser)
|
project(syntaxParser)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
add_executable(syntaxParser main.cpp)
|
add_executable(syntaxParser main.cpp)
|
77
main.cpp
77
main.cpp
@ -9,6 +9,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -62,7 +63,7 @@ public:
|
|||||||
line.push_back(symbol);
|
line.push_back(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<const Symbol *> &getAllSymbols() const {
|
[[nodiscard]] const vector<const Symbol *> &getAllSymbols() const {
|
||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +92,7 @@ public:
|
|||||||
return symbol->index;
|
return symbol->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Symbol *getSymbol(int symbol_index) const {
|
[[nodiscard]] const Symbol *getSymbol(int symbol_index) const {
|
||||||
const auto &it = cache.find(symbol_index);
|
const auto &it = cache.find(symbol_index);
|
||||||
if(it != cache.end()) {
|
if(it != cache.end()) {
|
||||||
return it->second;
|
return it->second;
|
||||||
@ -100,7 +101,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int getSymbolIndex(const wstring &name) const {
|
[[nodiscard]] int getSymbolIndex(const wstring &name) const {
|
||||||
const auto &it = table.find(name);
|
const auto &it = table.find(name);
|
||||||
if(it != table.end()) {
|
if(it != table.end()) {
|
||||||
return it->second->index;
|
return it->second->index;
|
||||||
@ -119,7 +120,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Symbol *getStartSymbol() const {
|
[[nodiscard]] const Symbol *getStartSymbol() const {
|
||||||
for(const auto & symbol : getAllSymbols()) {
|
for(const auto & symbol : getAllSymbols()) {
|
||||||
if(symbol->start) {
|
if(symbol->start) {
|
||||||
return symbol;
|
return symbol;
|
||||||
@ -428,15 +429,15 @@ public:
|
|||||||
productions.push_back(p_pdt);
|
productions.push_back(p_pdt);
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<const Production *> &get_productions() const {
|
[[nodiscard]] const vector<const Production *> &get_productions() const {
|
||||||
return productions;
|
return productions;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Symbol *getSymbol(int symbol_index) const {
|
[[nodiscard]] const Symbol *getSymbol(int symbol_index) const {
|
||||||
return symbolTable.getSymbol(symbol_index);
|
return symbolTable.getSymbol(symbol_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Symbol *getStartSymbol() const {
|
[[nodiscard]] const Symbol *getStartSymbol() const {
|
||||||
return symbolTable.getStartSymbol();
|
return symbolTable.getStartSymbol();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -454,7 +455,7 @@ public:
|
|||||||
return p_pdt;
|
return p_pdt;
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<const Symbol *> &getAllSymbols() const {
|
[[nodiscard]] const vector<const Symbol *> &getAllSymbols() const {
|
||||||
return symbolTable.getAllSymbols();
|
return symbolTable.getAllSymbols();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -487,11 +488,11 @@ public:
|
|||||||
this->dot_index = m_dot_index;
|
this->dot_index = m_dot_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_dot_index() const {
|
[[nodiscard]] int get_dot_index() const {
|
||||||
return dot_index;
|
return dot_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_dot_next_symbol() const {
|
[[nodiscard]] int get_dot_next_symbol() const {
|
||||||
if(get_dot_index() == production->right.size()) {
|
if(get_dot_index() == production->right.size()) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
@ -499,7 +500,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_dot_next_i_symbol(int i) const {
|
[[nodiscard]] int get_dot_next_i_symbol(int i) const {
|
||||||
if(get_dot_index() + i >= production->right.size()) {
|
if(get_dot_index() + i >= production->right.size()) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
@ -507,11 +508,11 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_terminator() const {
|
[[nodiscard]] int get_terminator() const {
|
||||||
return terminator;
|
return terminator;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Production *get_production() const {
|
[[nodiscard]] const Production *get_production() const {
|
||||||
return production;
|
return production;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -524,7 +525,7 @@ class ItemCollection{
|
|||||||
|
|
||||||
map<size_t, Item *> items;
|
map<size_t, Item *> items;
|
||||||
|
|
||||||
vector<const Item *> cache;
|
vector<Item *> cache;
|
||||||
|
|
||||||
GrammarResourcePool *pool;
|
GrammarResourcePool *pool;
|
||||||
|
|
||||||
@ -537,17 +538,27 @@ class ItemCollection{
|
|||||||
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
|
seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool compare_item_ptr(const Item* lhs, const Item* rhs)
|
||||||
|
{
|
||||||
|
if(lhs->get_production() != rhs->get_production())
|
||||||
|
return lhs->get_production() < rhs->get_production();
|
||||||
|
else if(lhs->get_dot_index() != rhs->get_dot_index())
|
||||||
|
return lhs->get_dot_index() < rhs->get_dot_index();
|
||||||
|
else
|
||||||
|
return lhs->get_terminator() < rhs->get_terminator();
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit ItemCollection(GrammarResourcePool *pool) : pool(pool) {
|
explicit ItemCollection(GrammarResourcePool *pool) : pool(pool) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<const Item *> &getItems() const {
|
[[nodiscard]] const vector<Item *> &getItems() const {
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getIndex() const {
|
[[nodiscard]] int getIndex() const {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -617,20 +628,24 @@ public:
|
|||||||
wcout << pool->getSymbol(p_pdt->left)->name << L" -> " ;
|
wcout << pool->getSymbol(p_pdt->left)->name << L" -> " ;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for(const auto &symbol : p_pdt->right) {
|
for(const auto &symbol : p_pdt->right) {
|
||||||
if(i++ == dot_index) cout << "·";
|
if(i++ == dot_index) wcout << L'*';
|
||||||
wcout << pool->getSymbol(symbol)->name;
|
wcout << pool->getSymbol(symbol)->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(i++ == dot_index) cout << "·";
|
if(i++ == dot_index) wcout << L'*';
|
||||||
|
|
||||||
wcout << L"," << pool->getSymbol(item->get_terminator())->name << endl;
|
wcout << L',' << pool->getSymbol(item->get_terminator())->name << endl;
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t getHash() const {
|
[[nodiscard]] size_t getHash() const {
|
||||||
size_t seed = 0;
|
size_t seed = 0;
|
||||||
for(const auto item : cache) {
|
|
||||||
|
vector<Item *> cache_sorted(cache.begin(), cache.end());
|
||||||
|
sort(cache_sorted.begin(), cache_sorted.end(), compare_item_ptr);
|
||||||
|
|
||||||
|
for(const auto item : cache_sorted) {
|
||||||
|
|
||||||
if(item->generated) {
|
if(item->generated) {
|
||||||
continue;
|
continue;
|
||||||
@ -656,7 +671,7 @@ class ItemCollectionManager{
|
|||||||
|
|
||||||
GrammarResourcePool *pool;
|
GrammarResourcePool *pool;
|
||||||
|
|
||||||
const Production *start_pdt;
|
const Production *start_pdt{};
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
inline void hash_combine(std::size_t& seed, const T& v) const
|
inline void hash_combine(std::size_t& seed, const T& v) const
|
||||||
@ -716,11 +731,11 @@ public:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Production *getStartProduction() const {
|
[[nodiscard]] const Production *getStartProduction() const {
|
||||||
return start_pdt;
|
return start_pdt;
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<const ItemCollection *> &getItemCollections() const{
|
[[nodiscard]] const vector<const ItemCollection *> &getItemCollections() const{
|
||||||
return ics;
|
return ics;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -763,7 +778,7 @@ public:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const ItemCollection* getGOTO(int idx, int symbol) const {
|
[[nodiscard]] const ItemCollection* getGOTO(int idx, int symbol) const {
|
||||||
|
|
||||||
auto hasher = hash<int>();
|
auto hasher = hash<int>();
|
||||||
size_t seed = hasher(idx);
|
size_t seed = hasher(idx);
|
||||||
@ -809,13 +824,15 @@ class AnalyseTableGenerator {
|
|||||||
struct Step {
|
struct Step {
|
||||||
|
|
||||||
const Action action;
|
const Action action;
|
||||||
const union Target{
|
union Target{
|
||||||
int index;
|
int index;
|
||||||
const Production *production;
|
const Production *production;
|
||||||
} target;
|
} target{};
|
||||||
|
|
||||||
Step(Action action, int index) : action(action), target(Target{index}){}
|
Step(Action action, int index) : action(action), target(Target{index}){}
|
||||||
Step(Action action, const Production *p_pdt) : action(action), target(Target{.production = p_pdt}){}
|
Step(Action action, const Production *p_pdt) : action(action) {
|
||||||
|
target.production = p_pdt;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
map<size_t, Step *> ACTION;
|
map<size_t, Step *> ACTION;
|
||||||
@ -966,7 +983,7 @@ public:
|
|||||||
wcout << std::left << std::setw(4) << wstring(L"s") + to_wstring(p_step->target.index);
|
wcout << std::left << std::setw(4) << wstring(L"s") + to_wstring(p_step->target.index);
|
||||||
else if(p_step->action == ACC)
|
else if(p_step->action == ACC)
|
||||||
wcout << std::left << std::setw(4) << L"acc";
|
wcout << std::left << std::setw(4) << L"acc";
|
||||||
else
|
else if(p_step->action == STATUTE)
|
||||||
wcout << std::left << std::setw(4) << L"r";
|
wcout << std::left << std::setw(4) << L"r";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1022,8 +1039,6 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
clock_t start,end;//定义clock_t变量
|
clock_t start,end;//定义clock_t变量
|
||||||
start = clock(); //开始时间
|
start = clock(); //开始时间
|
||||||
|
Loading…
Reference in New Issue
Block a user