6#include <unordered_set>
64 std::map<unsigned int, std::map<std::string, SLR1Parser::s_action>>;
79 std::map<unsigned int, std::map<std::string, unsigned int>>;
93 std::unordered_set<Lr0Item>
AllItems()
const;
105 void Closure(std::unordered_set<Lr0Item>& items);
120 void ClosureUtil(std::unordered_set<Lr0Item>& items,
unsigned int size,
121 std::unordered_set<std::string>& visited);
133 std::unordered_set<Lr0Item>
Delta(
const std::unordered_set<Lr0Item>& items,
134 const std::string& str);
176 void First(std::span<const std::string> rule,
177 std::unordered_set<std::string>& result);
233 std::unordered_set<std::string>
Follow(
const std::string& arg);
277 std::string
PrintItems(
const std::unordered_set<Lr0Item>& items)
const;
283 std::unordered_map<std::string, std::unordered_set<std::string>>
287 std::unordered_map<std::string, std::unordered_set<std::string>>
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
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