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/*
2 * SyntaxTutor - Interactive Tutorial About Syntax Analyzers
3 * Copyright (C) 2025 Jose R. (jose-rzm)
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20#include "grammar.hpp"
21#include <span>
22#include <stack>
23#include <string>
24#include <unordered_map>
25#include <unordered_set>
26#include <vector>
27
28class LL1Parser {
29
45 using ll1_table = std::unordered_map<
46 std::string, std::unordered_map<std::string, std::vector<production>>>;
47
48 public:
49 LL1Parser() = default;
55 explicit LL1Parser(Grammar gr);
56
78 bool CreateLL1Table();
79
106 void First(std::span<const std::string> rule,
107 std::unordered_set<std::string>& result);
108
119 void ComputeFirstSets();
120
146 void ComputeFollowSets();
147
162 std::unordered_set<std::string> Follow(const std::string& arg);
163
185 std::unordered_set<std::string>
186 PredictionSymbols(const std::string& antecedent,
187 const std::vector<std::string>& consequent);
188
191 ll1_table ll1_t_;
192
195
197 std::unordered_map<std::string, std::unordered_set<std::string>>
199
201 std::unordered_map<std::string, std::unordered_set<std::string>>
203};
void ComputeFollowSets()
Computes the FOLLOW sets for all non-terminal symbols in the grammar. The FOLLOW set of a non-termina...
Definition ll1_parser.cpp:133
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:186
ll1_table ll1_t_
The LL(1) parsing table, mapping non-terminals and terminals to productions.
Definition ll1_parser.hpp:191
void ComputeFirstSets()
Computes the FIRST sets for all non-terminal symbols in the grammar.
Definition ll1_parser.cpp:102
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:194
bool CreateLL1Table()
Creates the LL(1) parsing table for the grammar.
Definition ll1_parser.cpp:36
Grammar gr_
Grammar object associated with this parser.
Definition ll1_parser.hpp:194
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:198
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:202
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:63
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
Definition grammar.hpp:46