Compute FIRST sets correctly

This commit is contained in:
Max Brunsfeld 2014-01-13 12:57:48 -08:00
parent 4cacdcba70
commit 29f73afbc5
5 changed files with 108 additions and 9 deletions

View file

@ -0,0 +1,38 @@
#include "spec_helper.h"
#include "build_tables/next_symbols.h"
#include "grammar.h"
#include "rules.h"
using std::set;
using namespace build_tables;
using namespace rules;
START_TEST
describe("computing FIRST sets", []() {
Grammar grammar({
{ "A", choice({
seq({
sym("B"),
sym("x"),
sym("B") }),
sym("B") }) },
{ "B", choice({
seq({
sym("y"),
sym("z"),
sym("y") }),
sym("y") }) },
});
describe("for a rule", [&]() {
it("searches the tree for terminals", [&]() {
auto terminals = next_terminals(grammar.rules.find("A")->second, grammar);
AssertThat(terminals, Equals(set<Symbol>({
Symbol("y")
})));
});
});
});
END_TEST

View file

@ -4,6 +4,7 @@
#include "bandit/bandit.h"
#include <iostream>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include "grammar.h"
@ -26,7 +27,19 @@ namespace std {
}
return stream << ">";
}
template<typename T>
inline ostream& operator<<(ostream &stream, const set<T> &set) {
stream << string("#<set: ");
bool started = false;
for (auto item : set) {
if (started) stream << string(", ");
stream << item;
started = true;
}
return stream << ">";
}
template<typename TKey, typename TValue>
inline ostream& operator<<(ostream &stream, const unordered_map<TKey, TValue> &map) {
stream << string("#<map: ");