LL1Checker 2.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 <deque>
4#include <queue>
5#include <span>
6#include <stack>
7#include <string>
8#include <unordered_map>
9#include <unordered_set>
10#include <vector>
11
12class LL1Parser {
13 using ll1_table = std::unordered_map<
14 std::string, std::unordered_map<std::string, std::vector<production>>>;
15
16 public:
23 LL1Parser(Grammar gr, std::string text_file, bool table_format = true);
24
31 LL1Parser(const std::string& grammar_file, std::string text_file,
32 bool table_format = true);
33
39 explicit LL1Parser(const std::string& grammar_file,
40 bool table_format = true);
41
69 bool Parse();
70
85 bool MatchTerminal(const std::string& top_symbol,
86 const std::string& current_symbol);
87
109 bool ProcessNonTerminal(const std::string& top_symbol,
110 const std::string& current_symbol);
111
122 void PrintTable();
123
134 void PrintStackTrace();
135
142 void PrintSymbolHist();
143
144 private:
171 void First(std::span<const std::string> rule,
172 std::unordered_set<std::string>& result);
173
184 void ComputeFirstSets();
185
213 void ComputeFollowSets();
214
232 bool UpdateFollow(const std::string& symbol, const std::string& lhs,
233 const production& rhs, size_t i);
234
252 std::unordered_set<std::string> Follow(const std::string& arg);
253
276 std::unordered_set<std::string>
277 PredictionSymbols(const std::string& antecedent,
278 const std::vector<std::string>& consequent);
279
301 bool CreateLL1Table();
302
316
318 const size_t kTraceSize{5};
319
322 ll1_table ll1_t_;
323
326
328 std::unordered_map<std::string, std::unordered_set<std::string>>
330
332 std::unordered_map<std::string, std::unordered_set<std::string>>
334
336 std::stack<std::string> symbol_stack_;
337
339 std::deque<std::string> trace_;
340
342 std::string grammar_file_;
343
345 std::string text_file_;
346
349};
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 &current_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 &current_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
Definition grammar.hpp:8