SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
Loading...
Searching...
No Matches
slr1_parser.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 <map>
21#include <span>
22#include <string>
23#include <unordered_set>
24
25#include "grammar.hpp"
26#include "lr0_item.hpp"
27#include "state.hpp"
28
39 public:
51 enum class Action { Shift, Reduce, Accept, Empty };
52
64 struct s_action {
65 const Lr0Item* item;
67 };
68
82 std::map<unsigned int, std::map<std::string, SLR1Parser::s_action>>;
83
97 std::map<unsigned int, std::map<std::string, unsigned int>>;
98
99 SLR1Parser() = default;
100 explicit SLR1Parser(Grammar gr);
101
110 std::unordered_set<Lr0Item> AllItems() const;
111
122 void Closure(std::unordered_set<Lr0Item>& items);
123
137 void ClosureUtil(std::unordered_set<Lr0Item>& items, unsigned int size,
138 std::unordered_set<std::string>& visited);
139
150 std::unordered_set<Lr0Item> Delta(const std::unordered_set<Lr0Item>& items,
151 const std::string& str);
152
165 bool SolveLRConflicts(const state& st);
166
193 void First(std::span<const std::string> rule,
194 std::unordered_set<std::string>& result);
195
206 void ComputeFirstSets();
207
233 void ComputeFollowSets();
234
249 std::unordered_set<std::string> Follow(const std::string& arg);
250
263 void MakeInitialState();
264
282 bool MakeParser();
283
293 std::string PrintItems(const std::unordered_set<Lr0Item>& items) const;
294
297
299 std::unordered_map<std::string, std::unordered_set<std::string>>
301
303 std::unordered_map<std::string, std::unordered_set<std::string>>
305
309
313
315 std::unordered_set<state> states_;
316};
void ClosureUtil(std::unordered_set< Lr0Item > &items, unsigned int size, std::unordered_set< std::string > &visited)
Helper function for computing the closure of LR(0) items.
Definition slr1_parser.cpp:186
void First(std::span< const std::string > rule, std::unordered_set< std::string > &result)
Calculates the FIRST set for a given production rule in a grammar.
Definition slr1_parser.cpp:254
void ComputeFollowSets()
Computes the FOLLOW sets for all non-terminal symbols in the grammar. The FOLLOW set of a non-termina...
Definition slr1_parser.cpp:317
void Closure(std::unordered_set< Lr0Item > &items)
Computes the closure of a set of LR(0) items.
Definition slr1_parser.cpp:181
std::unordered_map< std::string, std::unordered_set< std::string > > follow_sets_
Cached FOLLOW sets for all non-terminal symbols in the grammar.
Definition slr1_parser.hpp:304
std::unordered_map< std::string, std::unordered_set< std::string > > first_sets_
Cached FIRST sets for all symbols in the grammar.
Definition slr1_parser.hpp:300
action_table actions_
The action table used by the parser to determine shift/reduce actions.
Definition slr1_parser.hpp:308
bool SolveLRConflicts(const state &st)
Resolves LR conflicts in a given state.
Definition slr1_parser.cpp:61
std::map< unsigned int, std::map< std::string, SLR1Parser::s_action > > action_table
Represents the action table for the SLR(1) parser.
Definition slr1_parser.hpp:81
void MakeInitialState()
Creates the initial state of the parser's state machine.
Definition slr1_parser.cpp:50
std::string PrintItems(const std::unordered_set< Lr0Item > &items) const
Returns a string representation of a set of LR(0) items.
Definition slr1_parser.cpp:242
Grammar gr_
The grammar being processed by the parser.
Definition slr1_parser.hpp:296
std::map< unsigned int, std::map< std::string, unsigned int > > transition_table
Represents the transition table for the SLR(1) parser.
Definition slr1_parser.hpp:96
bool MakeParser()
Constructs the SLR(1) parsing tables (action and transition tables).
Definition slr1_parser.cpp:110
std::unordered_set< state > states_
The set of states in the parser's state machine.
Definition slr1_parser.hpp:315
transition_table transitions_
The transition table used by the parser to determine state transitions.
Definition slr1_parser.hpp:312
Action
Represents the possible actions in the SLR(1) parsing table.
Definition slr1_parser.hpp:51
@ Shift
Definition slr1_parser.hpp:51
@ Accept
Definition slr1_parser.hpp:51
@ Empty
Definition slr1_parser.hpp:51
@ Reduce
Definition slr1_parser.hpp:51
std::unordered_set< Lr0Item > Delta(const std::unordered_set< Lr0Item > &items, const std::string &str)
Computes the GOTO transition ( ) for a given set of LR(0) items and a symbol. This function is equiva...
Definition slr1_parser.cpp:213
void ComputeFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition slr1_parser.cpp:286
std::unordered_set< Lr0Item > AllItems() const
Retrieves all LR(0) items in the grammar. This function returns a set of all LR(0) items derived from...
Definition slr1_parser.cpp:38
std::unordered_set< std::string > Follow(const std::string &arg)
Computes the FOLLOW set for a given non-terminal symbol in the grammar.
Definition slr1_parser.cpp:370
SLR1Parser()=default
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
Definition grammar.hpp:46
Represents an LR(0) item used in LR automata construction.
Definition lr0_item.hpp:35
Definition slr1_parser.hpp:64
const Lr0Item * item
Definition slr1_parser.hpp:65
Action action
Definition slr1_parser.hpp:66
Represents a state in the LR(0) automaton.
Definition state.hpp:33