4#include "symbol_table.hpp"
10#include <unordered_map>
11#include <unordered_set>
15 using ll1_table = std::unordered_map<
16 symbol_table::TokenID,
17 std::unordered_map<symbol_table::TokenID, std::vector<production>>>;
23 std::vector<std::unique_ptr<ParseNode>>
children;
26 using ParseTree = std::unique_ptr<ParseNode>;
34 bool text_is_raw =
false);
42 LL1Parser(
const std::string& grammar_file, std::string text_file,
43 bool table_format =
true,
bool text_is_raw =
false);
94 symbol_table::TokenID current_symbol);
119 symbol_table::TokenID current_symbol,
120 std::stack<symbol_table::TokenID>& symbol_stack);
135 void ExportTreeAsDot(
const ParseTree& tree,
const std::string& filename);
149 symbol_table::TokenID expected,
150 symbol_table::TokenID found);
178 void First(std::span<const symbol_table::TokenID> rule,
179 std::unordered_set<symbol_table::TokenID>& result);
239 bool UpdateFollow(symbol_table::TokenID symbol, symbol_table::TokenID lhs,
240 const production& rhs,
size_t i);
259 std::unordered_set<symbol_table::TokenID>
Follow(symbol_table::TokenID arg);
283 std::unordered_set<symbol_table::TokenID>
285 const production& consequent);
332 std::unordered_map<symbol_table::TokenID,
333 std::unordered_set<symbol_table::TokenID>>
337 std::unordered_map<symbol_table::TokenID,
338 std::unordered_set<symbol_table::TokenID>>
void PrintTableUsingTabulate()
Print the LL(1) parsing table using the tabulate library.
Definition ll1_parser.cpp:370
void ComputeFollowSets()
Computes the FOLLOW sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:252
void First(std::span< const symbol_table::TokenID > rule, std::unordered_set< symbol_table::TokenID > &result)
Calculates the FIRST set for a given production rule in a grammar.
Definition ll1_parser.cpp:200
bool print_table_format_
True if new format is used when printing the table.
Definition ll1_parser.hpp:351
ll1_table ll1_t_
The LL(1) parsing table, mapping non-terminals and terminals to productions.
Definition ll1_parser.hpp:326
void ComputeFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:225
std::unordered_set< symbol_table::TokenID > Follow(symbol_table::TokenID arg)
Computes the FOLLOW set for a given non-terminal symbol in the grammar.
Definition ll1_parser.cpp:307
bool MatchTerminal(symbol_table::TokenID top_symbol, symbol_table::TokenID current_symbol)
Matches a terminal symbol from the stack with the current input symbol.
Definition ll1_parser.cpp:70
LL1Parser(Grammar gr, std::string text_file, bool table_format=true, bool text_is_raw=false)
Constructs an LL1Parser with a grammar object and an input file.
Definition ll1_parser.cpp:21
ParseTree ParseWithTree(const std::string &file)
Parse a file and return the parse tree.
Definition ll1_parser.cpp:137
std::unordered_set< symbol_table::TokenID > PredictionSymbols(symbol_table::TokenID antecedent, const production &consequent)
Computes the prediction symbols for a given production rule.
Definition ll1_parser.cpp:316
std::unordered_map< symbol_table::TokenID, std::unordered_set< symbol_table::TokenID > > first_sets_
FIRST sets for each non-terminal in the grammar.
Definition ll1_parser.hpp:334
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:105
std::string grammar_file_
Path to the grammar file used in this parser.
Definition ll1_parser.hpp:342
bool UpdateFollow(symbol_table::TokenID symbol, symbol_table::TokenID lhs, const production &rhs, size_t i)
Updates the FOLLOW set for a non-terminal based on a production.
Definition ll1_parser.cpp:275
bool CreateLL1Table()
Creates the LL(1) parsing table for the grammar.
Definition ll1_parser.cpp:43
bool text_is_raw_
If true, treat text_file_ as raw input rather than a filename.
Definition ll1_parser.hpp:348
Grammar gr_
Grammar object associated with this parser.
Definition ll1_parser.hpp:329
void ExportTreeAsDot(const ParseTree &tree, const std::string &filename)
Export the parse tree in GraphViz dot format.
Definition ll1_parser.cpp:436
void PrintTable()
Print the LL(1) parsing table to standard output.
Definition ll1_parser.cpp:328
bool ProcessNonTerminal(symbol_table::TokenID top_symbol, symbol_table::TokenID current_symbol, std::stack< symbol_table::TokenID > &symbol_stack)
Processes a non-terminal symbol by expanding it according to the LL(1) parsing table.
Definition ll1_parser.cpp:75
void ReportParseError(const std::string &input, size_t err_pos, symbol_table::TokenID expected, symbol_table::TokenID found)
Reports a parsing error printing the problematic line and a context window of 2 lines.
Definition ll1_parser.cpp:92
std::unordered_map< symbol_table::TokenID, std::unordered_set< symbol_table::TokenID > > follow_sets_
FOLLOW sets for each non-terminal in the grammar.
Definition ll1_parser.hpp:339
Node used to build a parse tree.
Definition ll1_parser.hpp:21
std::vector< std::unique_ptr< ParseNode > > children
Child nodes.
Definition ll1_parser.hpp:23
std::string symbol
Grammar symbol.
Definition ll1_parser.hpp:22