SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
Loading...
Searching...
No Matches
state.hpp
Go to the documentation of this file.
1/*
2 * SyntaxTutor - Interactive Tutorial About Syntax Analyzers
3 * Copyright (C) 2025 Jose R. (jose-rzm)
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20#include "lr0_item.hpp"
21#include <cstddef>
22#include <functional>
23#include <numeric>
24#include <unordered_set>
25
33struct state {
37 std::unordered_set<Lr0Item> items_;
38
42 unsigned int id_;
43
49 bool operator==(const state& other) const { return other.items_ == items_; }
50};
51
52namespace std {
53template <> struct hash<state> {
54 size_t operator()(const state& st) const {
55 size_t seed =
56 std::accumulate(st.items_.begin(), st.items_.end(), 0,
57 [](size_t acc, const Lr0Item& item) {
58 return acc ^ (std::hash<Lr0Item>()(item));
59 });
60 return seed;
61 }
62};
63} // namespace std
Represents a state in the LR(0) automaton.
Definition state.hpp:33
bool operator==(const state &other) const
Equality operator for comparing states based on their items.
Definition state.hpp:49
std::unordered_set< Lr0Item > items_
The set of LR(0) items that make up this state.
Definition state.hpp:37
unsigned int id_
Unique identifier of the state.
Definition state.hpp:42