Infinite loop, to debug

This commit is contained in:
Robin Jadoul 2017-01-11 17:49:40 +01:00
parent aa2dbbefae
commit cf237b04eb
6 changed files with 86 additions and 22 deletions

View File

@ -11,6 +11,7 @@
#include "Parsodus/lrtables/LR0Itemset.h" #include "Parsodus/lrtables/LR0Itemset.h"
#include "Parsodus/lrtables/SLR1Itemset.h" #include "Parsodus/lrtables/SLR1Itemset.h"
#include "Parsodus/lrtables/LR1Itemset.h" #include "Parsodus/lrtables/LR1Itemset.h"
#include "Parsodus/lrtables/LALR1Itemset.h"
#include "Parsodus/util/parserType.h" #include "Parsodus/util/parserType.h"
namespace pds { namespace pds {
@ -32,6 +33,7 @@ namespace pds {
registerBackend(std::make_unique<T<lr::Generator<lr::LR0Itemset>>>(util::ParserType::LR_0)); registerBackend(std::make_unique<T<lr::Generator<lr::LR0Itemset>>>(util::ParserType::LR_0));
registerBackend(std::make_unique<T<lr::Generator<lr::SLR1Itemset>>>(util::ParserType::SLR_1)); registerBackend(std::make_unique<T<lr::Generator<lr::SLR1Itemset>>>(util::ParserType::SLR_1));
registerBackend(std::make_unique<T<lr::Generator<lr::LR1Itemset>>>(util::ParserType::LR_1)); registerBackend(std::make_unique<T<lr::Generator<lr::LR1Itemset>>>(util::ParserType::LR_1));
registerBackend(std::make_unique<T<lr::Generator<lr::LALR1Itemset>>>(util::ParserType::LALR_1));
} }
/** /**

View File

@ -0,0 +1,24 @@
#pragma once
#ifndef PARSODUS_LRTABLES_LALR1ITEMSET_H_TJ1FUOEG
#define PARSODUS_LRTABLES_LALR1ITEMSET_H_TJ1FUOEG
#include "Parsodus/lrtables/LR1ItemsetBase.h"
namespace pds {
namespace lr {
class LALR1Itemset : public LR1ItemsetBase<LALR1Itemset> {
public:
LALR1Itemset();
LALR1Itemset(std::shared_ptr<Rule> start);
bool canMerge(const LALR1Itemset& rhs) const;
void merge(const LALR1Itemset& rhs);
private:
};
} /* lr */
} /* pds */
#endif /* PARSODUS_LRTABLES_LALR1ITEMSET_H_TJ1FUOEG */

View File

@ -24,8 +24,8 @@ public:
bool empty() const; bool empty() const;
std::set<std::size_t> getReduces(const Grammar& g, std::string lookahead) const; std::set<std::size_t> getReduces(const Grammar& g, std::string lookahead) const;
private: protected:
std::set<LR1Item> m_items; std::vector<LR1Item> m_items;
}; };
template <typename Itemset> template <typename Itemset>
@ -34,7 +34,7 @@ LR1ItemsetBase<Itemset>::LR1ItemsetBase()
template <typename Itemset> template <typename Itemset>
LR1ItemsetBase<Itemset>::LR1ItemsetBase(std::shared_ptr<Rule> start) { LR1ItemsetBase<Itemset>::LR1ItemsetBase(std::shared_ptr<Rule> start) {
m_items.emplace(LR1Item{start, 0, {util::EOF_PLACEHOLDER}}); m_items.emplace_back(LR1Item{start, 0, {util::EOF_PLACEHOLDER}});
} }
template <typename Itemset> template <typename Itemset>
@ -78,13 +78,9 @@ void LR1ItemsetBase<Itemset>::close(const Grammar& g) {
} }
} }
std::vector<LR1Item> newItems;
for (auto& it : m_items) {
newItems.emplace_back(std::move(it));
}
for (const auto& newItem : toAdd) { for (const auto& newItem : toAdd) {
bool found = false; bool found = false;
for (auto& oldItem : newItems) { for (auto& oldItem : m_items) {
if (newItem.dotIdx == oldItem.dotIdx && newItem.rule == oldItem.rule) { if (newItem.dotIdx == oldItem.dotIdx && newItem.rule == oldItem.rule) {
found = true; found = true;
oldItem.lookaheads.insert(newItem.lookaheads.begin(), newItem.lookaheads.end()); oldItem.lookaheads.insert(newItem.lookaheads.begin(), newItem.lookaheads.end());
@ -92,13 +88,9 @@ void LR1ItemsetBase<Itemset>::close(const Grammar& g) {
} }
} }
if (!found) { if (!found) {
newItems.push_back(newItem); m_items.push_back(newItem);
} }
} }
m_items.clear();
for (auto& it : newItems) {
m_items.emplace(std::move(it));
}
} }
} }
@ -108,7 +100,7 @@ Itemset LR1ItemsetBase<Itemset>::succ(std::string sym) const {
for (auto& item : m_items) { for (auto& item : m_items) {
if (item.dotIdx < item.rule->tail.size()) { if (item.dotIdx < item.rule->tail.size()) {
if (item.rule->tail[item.dotIdx] == sym) { if (item.rule->tail[item.dotIdx] == sym) {
sc.m_items.insert(LR1Item{item.rule, item.dotIdx + 1, item.lookaheads}); sc.m_items.push_back(LR1Item{item.rule, item.dotIdx + 1, item.lookaheads});
} }
} }
} }

View File

@ -1,12 +1,8 @@
parser: SLR(1) parser: lalr(1)
lexesis: lexer.lxs lexesis: lexer.lxs
terminals: terminals:
TERMINAL A B
start: s start: s
grammar: grammar:
s -> a s s -> x x;
| b x -> A x | B;
;
a → TERMINAL;
b -> a;

View File

@ -25,6 +25,7 @@ add_library(Parsodus-tables
lrtables/SLR1Itemset.cpp lrtables/SLR1Itemset.cpp
lrtables/LR1Item.cpp lrtables/LR1Item.cpp
lrtables/LR1Itemset.cpp lrtables/LR1Itemset.cpp
lrtables/LALR1Itemset.cpp
) )
# add_library(Parsodus-backends # add_library(Parsodus-backends

View File

@ -0,0 +1,49 @@
#include "Parsodus/lrtables/LALR1Itemset.h"
#include <cassert>
namespace pds {
namespace lr {
LALR1Itemset::LALR1Itemset() : LR1ItemsetBase<LALR1Itemset>()
{}
LALR1Itemset::LALR1Itemset(std::shared_ptr<Rule> start) : LR1ItemsetBase<LALR1Itemset>(start)
{}
bool LALR1Itemset::canMerge(const LALR1Itemset& rhs) const {
if (rhs.m_items.size() != m_items.size()) {
return false;
}
for (auto& rhsIt : rhs.m_items) {
bool ok = false;
for (auto& it : m_items) {
if (it.rule == rhsIt.rule && it.dotIdx == rhsIt.dotIdx) {
ok = true;
break;
}
}
if (!ok)
return false;
}
return true;
}
void LALR1Itemset::merge(const LALR1Itemset& rhs) {
for (auto& toMerge : rhs.m_items) {
bool found = false;
for (auto& it : m_items) {
if (it.rule == toMerge.rule && it.dotIdx == toMerge.dotIdx) {
found = true;
it.lookaheads.insert(toMerge.lookaheads.begin(), toMerge.lookaheads.end());
break;
}
}
assert(found);
}
}
} /* lr */
} /* pds */