23#include <unordered_set>
82 std::map<unsigned int, std::map<std::string, SLR1Parser::s_action>>;
97 std::map<unsigned int, std::map<std::string, unsigned int>>;
110 std::unordered_set<Lr0Item>
AllItems()
const;
122 void Closure(std::unordered_set<Lr0Item>& items);
137 void ClosureUtil(std::unordered_set<Lr0Item>& items,
unsigned int size,
138 std::unordered_set<std::string>& visited);
150 std::unordered_set<Lr0Item>
Delta(
const std::unordered_set<Lr0Item>& items,
151 const std::string& str);
193 void First(std::span<const std::string> rule,
194 std::unordered_set<std::string>& result);
249 std::unordered_set<std::string>
Follow(
const std::string& arg);
293 std::string
PrintItems(
const std::unordered_set<Lr0Item>& items)
const;
299 std::unordered_map<std::string, std::unordered_set<std::string>>
303 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: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
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