SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
Loading...
Searching...
No Matches
grammar_factory.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 "symbol_table.hpp"
22#include <string>
23#include <unordered_map>
24#include <vector>
25
32
38 struct FactoryItem {
43 std::unordered_map<std::string, std::vector<production>> g_;
44
49
55 explicit FactoryItem(
56 const std::unordered_map<std::string, std::vector<production>>&
57 grammar);
58 };
59
64 void Init();
65
72 Grammar PickOne(int level);
73
80 Grammar GenLL1Grammar(int level);
87 Grammar GenSLR1Grammar(int level);
88
93 Grammar Lv1();
94
99 Grammar Lv2();
100
106 Grammar Lv3();
107
118 Grammar Lv4();
119
130 Grammar Lv5();
131
142 Grammar Lv6();
143
154 Grammar Lv7();
155
165
172 bool HasUnreachableSymbols(Grammar& grammar) const;
173
189 bool IsInfinite(Grammar& grammar) const;
190
197 bool HasDirectLeftRecursion(const Grammar& grammar) const;
198
204 bool HasIndirectLeftRecursion(const Grammar& grammar) const;
205
211 bool HasCycle(
212 const std::unordered_map<std::string, std::unordered_set<std::string>>&
213 graph) const;
214
220 std::unordered_set<std::string>
221 NullableSymbols(const Grammar& grammar) const;
222
242
243 void RemoveLeftRecursion(Grammar& grammar);
244
266
267 void LeftFactorize(Grammar& grammar);
268
280 std::vector<std::string>
281 LongestCommonPrefix(const std::vector<production>& productions);
282
296 bool StartsWith(const production& prod,
297 const std::vector<std::string>& prefix);
298
311 std::string GenerateNewNonTerminal(Grammar& grammar,
312 const std::string& base);
313
326 void NormalizeNonTerminals(FactoryItem& item, const std::string& nt) const;
327
340 void AdjustTerminals(FactoryItem& base, const FactoryItem& cmb,
341 const std::string& target_nt) const;
342
356 std::unordered_map<std::string, std::vector<production>>
357 Merge(const FactoryItem& base, const FactoryItem& cmb) const;
358
363 std::vector<FactoryItem> items;
364
368 std::vector<std::string> terminal_alphabet_{"a", "b", "c", "d", "e", "f",
369 "g", "h", "i", "j", "k", "l"};
370
374 std::vector<std::string> non_terminal_alphabet_{"A", "B", "C", "D",
375 "E", "F", "G"};
376};
std::vector< std::string > production
Represents the right-hand side of a grammar rule.
Definition grammar.hpp:34
Represents an individual grammar item with its associated symbol table.
Definition grammar_factory.hpp:38
std::unordered_map< std::string, std::vector< production > > g_
Stores the grammar rules where each key is a non-terminal symbol and each value is a vector of produc...
Definition grammar_factory.hpp:43
SymbolTable st_
Symbol table associated with this grammar item.
Definition grammar_factory.hpp:48
FactoryItem(const std::unordered_map< std::string, std::vector< production > > &grammar)
Constructor that initializes a FactoryItem with the provided grammar.
Definition grammar_factory.cpp:555
Responsible for creating and managing grammar items and performing checks on grammars.
Definition grammar_factory.hpp:31
FactoryItem CreateLv2Item()
Creates a Level 2 grammar item for use in grammar generation.
Definition grammar_factory.cpp:221
Grammar GenLL1Grammar(int level)
Generates a LL(1) random grammar based on the specified difficulty level.
Definition grammar_factory.cpp:92
void LeftFactorize(Grammar &grammar)
Performs left factorization. A grammar can be left factorized if it has productions with the same pre...
Definition grammar_factory.cpp:456
Grammar Lv6()
Generates a Level 6 grammar by combining Level 5 and Level 1 items.
Definition grammar_factory.cpp:186
std::vector< std::string > terminal_alphabet_
A vector of terminal symbols (alphabet) used in the grammar.
Definition grammar_factory.hpp:368
bool HasUnreachableSymbols(Grammar &grammar) const
Checks if a grammar contains unreachable symbols (non-terminals that cannot be derived from the start...
Definition grammar_factory.cpp:243
void AdjustTerminals(FactoryItem &base, const FactoryItem &cmb, const std::string &target_nt) const
Adjusts the terminal symbols between two grammar items.
Definition grammar_factory.cpp:591
std::unordered_map< std::string, std::vector< production > > Merge(const FactoryItem &base, const FactoryItem &cmb) const
Merges the grammar rules of two grammar items into a single grammar.
Definition grammar_factory.cpp:643
Grammar Lv1()
Generates a Level 1 grammar.
Definition grammar_factory.cpp:123
std::vector< FactoryItem > items
A vector of FactoryItem objects representing different level 1 grammar items created by the Init meth...
Definition grammar_factory.hpp:363
void RemoveLeftRecursion(Grammar &grammar)
Removes direct left recursion in a grammar. A grammar has direct left recursion when one of its produ...
Definition grammar_factory.cpp:414
Grammar Lv2()
Generates a Level 2 grammar by combining Level 1 items.
Definition grammar_factory.cpp:130
bool HasCycle(const std::unordered_map< std::string, std::unordered_set< std::string > > &graph) const
Checks if directed graph has a cycle using topological sort.
Definition grammar_factory.cpp:341
Grammar PickOne(int level)
Picks a random grammar based on the specified difficulty level (1, 2, or 3).
Definition grammar_factory.cpp:66
bool HasDirectLeftRecursion(const Grammar &grammar) const
Checks if a grammar contains direct left recursion (a non-terminal can produce itself on the left sid...
Definition grammar_factory.cpp:306
Grammar Lv5()
Generates a Level 5 grammar by combining Level 4 and Level 1 items.
Definition grammar_factory.cpp:168
bool IsInfinite(Grammar &grammar) const
Checks if a grammar is infinite, meaning there are non-terminal symbols that can never derive a termi...
Definition grammar_factory.cpp:273
void Init()
Initializes the GrammarFactory and populates the items vector with initial grammar items.
Definition grammar_factory.cpp:28
void NormalizeNonTerminals(FactoryItem &item, const std::string &nt) const
Replaces all non-terminal symbols in a grammar item with a single target non-terminal.
Definition grammar_factory.cpp:573
Grammar Lv4()
Generates a Level 4 grammar by combining Level 3 and Level 1 items.
Definition grammar_factory.cpp:150
Grammar GenSLR1Grammar(int level)
Generates a SLR(1) random grammar based on the specified difficulty lefel.
Definition grammar_factory.cpp:112
std::unordered_set< std::string > NullableSymbols(const Grammar &grammar) const
Find nullable symbols in a grammar.
Definition grammar_factory.cpp:379
Grammar Lv3()
Generates a Level 3 grammar by combining a Level 2 item and a Level 1 item.
Definition grammar_factory.cpp:134
bool StartsWith(const production &prod, const std::vector< std::string > &prefix)
Checks if a production starts with a given prefix.
Definition grammar_factory.cpp:533
std::vector< std::string > LongestCommonPrefix(const std::vector< production > &productions)
Finds the longest common prefix among a set of productions.
Definition grammar_factory.cpp:514
std::string GenerateNewNonTerminal(Grammar &grammar, const std::string &base)
Generates a new non-terminal symbol that is unique in the grammar.
Definition grammar_factory.cpp:545
Grammar Lv7()
Generates a Level 7 grammar by combining Level 6 and Level 1 items.
Definition grammar_factory.cpp:204
std::vector< std::string > non_terminal_alphabet_
A vector of non-terminal symbols (alphabet) used in the grammar.
Definition grammar_factory.hpp:374
bool HasIndirectLeftRecursion(const Grammar &grammar) const
Checks if a grammar contains indirect left recursion.
Definition grammar_factory.cpp:317
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
Definition grammar.hpp:46
Stores and manages grammar symbols, including their classification and special markers.
Definition symbol_table.hpp:45