SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
 
Loading...
Searching...
No Matches
ll1_parser.hpp
Go to the documentation of this file.
1#pragma once
2#include "grammar.hpp"
3#include <span>
4#include <stack>
5#include <string>
6#include <unordered_map>
7#include <unordered_set>
8#include <vector>
9
10class LL1Parser {
11
27 using ll1_table = std::unordered_map<
28 std::string, std::unordered_map<std::string, std::vector<production>>>;
29
30 public:
31 LL1Parser() = default;
37 explicit LL1Parser(Grammar gr);
38
60 bool CreateLL1Table();
61
88 void First(std::span<const std::string> rule,
89 std::unordered_set<std::string>& result);
90
101 void ComputeFirstSets();
102
130 void ComputeFollowSets();
131
146 std::unordered_set<std::string> Follow(const std::string& arg);
147
170 std::unordered_set<std::string>
171 PredictionSymbols(const std::string& antecedent,
172 const std::vector<std::string>& consequent);
173
176 ll1_table ll1_t_;
177
180
182 std::unordered_map<std::string, std::unordered_set<std::string>>
184
186 std::unordered_map<std::string, std::unordered_set<std::string>>
188};
void ComputeFollowSets()
Computes the FOLLOW sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:115
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:168
ll1_table ll1_t_
The LL(1) parsing table, mapping non-terminals and terminals to productions.
Definition ll1_parser.hpp:176
void ComputeFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:84
LL1Parser()=default
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:176
bool CreateLL1Table()
Creates the LL(1) parsing table for the grammar.
Definition ll1_parser.cpp:18
Grammar gr_
Grammar object associated with this parser.
Definition ll1_parser.hpp:179
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:183
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:187
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:45
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
Definition grammar.hpp:27