SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
Loading...
Searching...
No Matches
lr0_item.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 <string>
21#include <vector>
22
34
35struct Lr0Item {
39 std::string antecedent_;
40
44 std::vector<std::string> consequent_;
45
49 std::string epsilon_;
50
54 std::string eol_;
55
59 unsigned int dot_ = 0;
60
68 Lr0Item(std::string antecedent, std::vector<std::string> consequent,
69 std::string epsilon, std::string eol);
70
79 Lr0Item(std::string antecedent, std::vector<std::string> consequent,
80 unsigned int dot, std::string epsilon, std::string eol);
81
87 std::string NextToDot() const;
88
93 void PrintItem() const;
94
100 std::string ToString() const;
101
105 void AdvanceDot();
106
111 bool IsComplete() const;
112
118 bool operator==(const Lr0Item& other) const;
119};
120
121namespace std {
122template <> struct hash<Lr0Item> {
123 size_t operator()(const Lr0Item& item) const;
124};
125} // namespace std
Represents an LR(0) item used in LR automata construction.
Definition lr0_item.hpp:35
std::string antecedent_
The non-terminal on the left-hand side of the production.
Definition lr0_item.hpp:39
unsigned int dot_
The position of the dot (ยท) in the production.
Definition lr0_item.hpp:59
bool operator==(const Lr0Item &other) const
Equality operator for comparing two LR(0) items.
Definition lr0_item.cpp:100
Lr0Item(std::string antecedent, std::vector< std::string > consequent, std::string epsilon, std::string eol)
Constructs an LR(0) item with the dot at position 0.
Definition lr0_item.cpp:29
std::string epsilon_
The symbol representing the empty string ( ).
Definition lr0_item.hpp:49
std::string NextToDot() const
Returns the symbol immediately after the dot, or empty if the dot is at the end.
Definition lr0_item.cpp:47
bool IsComplete() const
Checks whether the dot has reached the end of the production.
Definition lr0_item.cpp:61
std::vector< std::string > consequent_
The sequence of symbols on the right-hand side of the production.
Definition lr0_item.hpp:44
void PrintItem() const
Prints the LR(0) item to the standard output in a human-readable format.
Definition lr0_item.cpp:69
void AdvanceDot()
Advances the dot one position to the right.
Definition lr0_item.cpp:55
std::string eol_
The symbol representing end-of-line or end-of-input ($).
Definition lr0_item.hpp:54
std::string ToString() const
Converts the item to a string representation, including the dot position.
Definition lr0_item.cpp:83