8#include <unordered_map>
9#include <unordered_set>
13 using ll1_table = std::unordered_map<
14 std::string, std::unordered_map<std::string, std::vector<production>>>;
31 LL1Parser(
const std::string& grammar_file, std::string text_file,
32 bool table_format =
true);
39 explicit LL1Parser(
const std::string& grammar_file,
40 bool table_format =
true);
86 const std::string& current_symbol);
110 const std::string& current_symbol);
171 void First(std::span<const std::string> rule,
172 std::unordered_set<std::string>& result);
232 bool UpdateFollow(
const std::string& symbol,
const std::string& lhs,
233 const production& rhs,
size_t i);
252 std::unordered_set<std::string>
Follow(
const std::string& arg);
276 std::unordered_set<std::string>
278 const std::vector<std::string>& consequent);
328 std::unordered_map<std::string, std::unordered_set<std::string>>
332 std::unordered_map<std::string, std::unordered_set<std::string>>
void PrintTableUsingTabulate()
Print the LL(1) parsing table using the tabulate library.
Definition ll1_parser.cpp:304
void ComputeFollowSets()
Computes the FOLLOW sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:204
std::unordered_set< std::string > Follow(const std::string &arg)
Computes the FOLLOW set for a given non-terminal symbol in the grammar.
Definition ll1_parser.cpp:256
bool print_table_format_
True if new format is used when printing the table.
Definition ll1_parser.hpp:348
bool MatchTerminal(const std::string &top_symbol, const std::string ¤t_symbol)
Matches a terminal symbol from the stack with the current input symbol.
Definition ll1_parser.cpp:96
ll1_table ll1_t_
The LL(1) parsing table, mapping non-terminals and terminals to productions.
Definition ll1_parser.hpp:322
void ComputeFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:173
LL1Parser(Grammar gr, std::string text_file, bool table_format=true)
Constructs an LL1Parser with a grammar object and an input file.
Definition ll1_parser.cpp:19
bool UpdateFollow(const std::string &symbol, const std::string &lhs, const production &rhs, size_t i)
Updates the FOLLOW set for a non-terminal based on a production.
Definition ll1_parser.cpp:227
const size_t kTraceSize
Size limit for symbol history trace, defaults to 5.
Definition ll1_parser.hpp:318
void PrintSymbolHist()
Prints the last kTraceSize symbols processed.
Definition ll1_parser.cpp:87
std::unordered_set< std::string > PredictionSymbols(const std::string &antecedent, const std::vector< std::string > &consequent)
Computes the prediction symbols for a given production rule.
Definition ll1_parser.cpp:265
std::string text_file_
Path to the input text file to be parsed.
Definition ll1_parser.hpp:345
bool Parse()
Parses an input string or file using the LL(1) parsing algorithm.
Definition ll1_parser.cpp:122
std::string grammar_file_
Path to the grammar file used in this parser.
Definition ll1_parser.hpp:342
bool CreateLL1Table()
Creates the LL(1) parsing table for the grammar.
Definition ll1_parser.cpp:49
Grammar gr_
Grammar object associated with this parser.
Definition ll1_parser.hpp:325
std::deque< std::string > trace_
Deque for tracking the most recent kTraceSize symbols parsed.
Definition ll1_parser.hpp:339
void PrintTable()
Print the LL(1) parsing table to standard output.
Definition ll1_parser.cpp:277
std::unordered_map< std::string, std::unordered_set< std::string > > first_sets_
FIRST sets for each non-terminal in the grammar.
Definition ll1_parser.hpp:329
std::unordered_map< std::string, std::unordered_set< std::string > > follow_sets_
FOLLOW sets for each non-terminal in the grammar.
Definition ll1_parser.hpp:333
void PrintStackTrace()
Prints the remaining symbols in the parsing stack after the parsing process.
Definition ll1_parser.cpp:78
std::stack< std::string > symbol_stack_
Stack for managing parsing symbols.
Definition ll1_parser.hpp:336
bool ProcessNonTerminal(const std::string &top_symbol, const std::string ¤t_symbol)
Processes a non-terminal symbol by expanding it according to the LL(1) parsing table.
Definition ll1_parser.cpp:106
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 ll1_parser.cpp:146