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#pragma once
2
3#include <map>
4#include <span>
5#include <string>
6#include <unordered_set>
7
8#include "grammar.hpp"
9#include "lr0_item.hpp"
10#include "state.hpp"
11
21 public:
33 enum class Action { Shift, Reduce, Accept, Empty };
34
46 struct s_action {
47 const Lr0Item* item;
49 };
50
64 std::map<unsigned int, std::map<std::string, SLR1Parser::s_action>>;
65
79 std::map<unsigned int, std::map<std::string, unsigned int>>;
80
81 SLR1Parser() = default;
82 explicit SLR1Parser(Grammar gr);
83
93 std::unordered_set<Lr0Item> AllItems() const;
94
105 void Closure(std::unordered_set<Lr0Item>& items);
106
120 void ClosureUtil(std::unordered_set<Lr0Item>& items, unsigned int size,
121 std::unordered_set<std::string>& visited);
122
133 std::unordered_set<Lr0Item> Delta(const std::unordered_set<Lr0Item>& items,
134 const std::string& str);
135
148 bool SolveLRConflicts(const state& st);
149
176 void First(std::span<const std::string> rule,
177 std::unordered_set<std::string>& result);
188 void ComputeFirstSets();
189
217 void ComputeFollowSets();
218
233 std::unordered_set<std::string> Follow(const std::string& arg);
234
247 void MakeInitialState();
248
266 bool MakeParser();
267
277 std::string PrintItems(const std::unordered_set<Lr0Item>& items) const;
278
281
283 std::unordered_map<std::string, std::unordered_set<std::string>>
285
287 std::unordered_map<std::string, std::unordered_set<std::string>>
289
293
297
299 std::unordered_set<state> states_;
300};
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:168
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:236
void ComputeFollowSets()
Computes the FOLLOW sets for all non-terminal symbols in the grammar.
Definition slr1_parser.cpp:299
void Closure(std::unordered_set< Lr0Item > &items)
Computes the closure of a set of LR(0) items.
Definition slr1_parser.cpp:163
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:288
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:284
action_table actions_
The action table used by the parser to determine shift/reduce actions.
Definition slr1_parser.hpp:292
bool SolveLRConflicts(const state &st)
Resolves LR conflicts in a given state.
Definition slr1_parser.cpp:43
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:63
void MakeInitialState()
Creates the initial state of the parser's state machine.
Definition slr1_parser.cpp:32
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:224
Grammar gr_
The grammar being processed by the parser.
Definition slr1_parser.hpp:280
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:78
bool MakeParser()
Constructs the SLR(1) parsing tables (action and transition tables).
Definition slr1_parser.cpp:92
std::unordered_set< state > states_
The set of states in the parser's state machine.
Definition slr1_parser.hpp:299
transition_table transitions_
The transition table used by the parser to determine state transitions.
Definition slr1_parser.hpp:296
Action
Represents the possible actions in the SLR(1) parsing table.
Definition slr1_parser.hpp:33
@ Shift
Definition slr1_parser.hpp:33
@ Accept
Definition slr1_parser.hpp:33
@ Empty
Definition slr1_parser.hpp:33
@ Reduce
Definition slr1_parser.hpp:33
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.
Definition slr1_parser.cpp:196
void ComputeFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition slr1_parser.cpp:268
std::unordered_set< Lr0Item > AllItems() const
Retrieves all LR(0) items in the grammar.
Definition slr1_parser.cpp:20
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:352
SLR1Parser()=default
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
Definition grammar.hpp:27
Represents an LR(0) item used in LR automata construction.
Definition lr0_item.hpp:14
Definition slr1_parser.hpp:46
const Lr0Item * item
Definition slr1_parser.hpp:47
Action action
Definition slr1_parser.hpp:48
Represents a state in the LR(0) automaton.
Definition state.hpp:16