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);
130 void First(std::span<const std::string> rule,
131 std::unordered_set<std::string>& result);
163 std::unordered_set<std::string>
Follow(
const std::string& arg);
187 std::unordered_set<std::string>
189 const std::vector<std::string>& consequent);
214 std::unordered_set<std::string>& visited,
215 std::unordered_set<std::string>& next_symbols);
265 std::unordered_map<std::string, std::unordered_set<std::string>>
first_sets;
void PrintTableUsingTabulate()
Print the LL(1) parsing table using the tabulate library.
Definition ll1_parser.cpp:232
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:179
bool print_table_format_
True if new format is used when printing the table.
Definition ll1_parser.hpp:280
ll1_table ll1_t_
The LL(1) parsing table, mapping non-terminals and terminals to productions.
Definition ll1_parser.hpp:259
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
const size_t kTraceSize
Size limit for symbol history trace, defaults to 5.
Definition ll1_parser.hpp:255
void PrintSymbolHist()
Prints the last kTraceSize symbols processed.
Definition ll1_parser.cpp:82
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:193
std::string text_file_
Path to the input text file to be parsed.
Definition ll1_parser.hpp:277
bool Parse()
Parses an input string or file using the LL(1) parsing algorithm.
Definition ll1_parser.cpp:91
std::string grammar_file_
Path to the grammar file used in this parser.
Definition ll1_parser.hpp:274
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:262
std::deque< std::string > trace_
Deque for tracking the most recent kTraceSize symbols parsed.
Definition ll1_parser.hpp:271
void PrintTable()
Print the LL(1) parsing table to standard output.
Definition ll1_parser.cpp:205
void ComputFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:158
void PrintStackTrace()
Prints the remaining symbols in the parsing stack after the parsing process.
Definition ll1_parser.cpp:73
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:265
void FollowUtil(const std::string &arg, std::unordered_set< std::string > &visited, std::unordered_set< std::string > &next_symbols)
Recursive utility function to compute the FOLLOW set for a non-terminal.
Definition ll1_parser.cpp:291
std::stack< std::string > symbol_stack_
Stack for managing parsing symbols.
Definition ll1_parser.hpp:268
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:131