LL1Checker 5.0
“Tool for verifying LL(1) grammars and validating input strings.”
Loading...
Searching...
No Matches
ll1_parser.hpp
1#pragma once
2#include "grammar.hpp"
3#include "lexer.hpp"
4#include "symbol_table.hpp"
5#include <deque>
6#include <queue>
7#include <span>
8#include <stack>
9#include <string>
10#include <unordered_map>
11#include <unordered_set>
12#include <vector>
13
14class LL1Parser {
15 using ll1_table = std::unordered_map<
16 symbol_table::TokenID,
17 std::unordered_map<symbol_table::TokenID, std::vector<production>>>;
18
19public:
21 struct ParseNode {
22 std::string symbol;
23 std::vector<std::unique_ptr<ParseNode>> children;
24 };
25
26 using ParseTree = std::unique_ptr<ParseNode>;
33 LL1Parser(Grammar gr, std::string text_file, bool table_format = true,
34 bool text_is_raw = false);
35
42 LL1Parser(const std::string& grammar_file, std::string text_file,
43 bool table_format = true, bool text_is_raw = false);
44
72 bool Parse();
73
77 ParseTree ParseWithTree(const std::string& file);
78
93 bool MatchTerminal(symbol_table::TokenID top_symbol,
94 symbol_table::TokenID current_symbol);
95
118 bool ProcessNonTerminal(symbol_table::TokenID top_symbol,
119 symbol_table::TokenID current_symbol,
120 std::stack<symbol_table::TokenID>& symbol_stack);
121
132 void PrintTable();
133
135 void ExportTreeAsDot(const ParseTree& tree, const std::string& filename);
136
137private:
148 void ReportParseError(const std::string& input, size_t err_pos,
149 symbol_table::TokenID expected,
150 symbol_table::TokenID found);
151
178 void First(std::span<const symbol_table::TokenID> rule,
179 std::unordered_set<symbol_table::TokenID>& result);
180
191 void ComputeFirstSets();
192
220 void ComputeFollowSets();
221
239 bool UpdateFollow(symbol_table::TokenID symbol, symbol_table::TokenID lhs,
240 const production& rhs, size_t i);
241
259 std::unordered_set<symbol_table::TokenID> Follow(symbol_table::TokenID arg);
260
283 std::unordered_set<symbol_table::TokenID>
284 PredictionSymbols(symbol_table::TokenID antecedent,
285 const production& consequent);
286
308 bool CreateLL1Table();
309
323
326 ll1_table ll1_t_;
327
330
332 std::unordered_map<symbol_table::TokenID,
333 std::unordered_set<symbol_table::TokenID>>
335
337 std::unordered_map<symbol_table::TokenID,
338 std::unordered_set<symbol_table::TokenID>>
340
342 std::string grammar_file_;
343
345 std::string text_file_;
346
348 bool text_is_raw_{false};
349
352};
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
Definition grammar.hpp:8
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