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#pragma once
2
3#include "grammar.hpp"
4#include "symbol_table.hpp"
5#include <string>
6#include <unordered_map>
7#include <vector>
8
15
21 struct FactoryItem {
26 std::unordered_map<std::string, std::vector<production>> g_;
27
32
38 explicit FactoryItem(const std::unordered_map<std::string, std::vector<production>> &grammar);
39 };
40
45 void Init();
46
53 Grammar PickOne(int level);
54
61 Grammar GenLL1Grammar(int level);
68 Grammar GenSLR1Grammar(int level);
69
74 Grammar Lv1();
75
80 Grammar Lv2();
81
87 Grammar Lv3();
88
99 Grammar Lv4();
100
111 Grammar Lv5();
112
123 Grammar Lv6();
124
135 Grammar Lv7();
136
146
147 // -------- SANITY CHECKS --------
148
155 bool HasUnreachableSymbols(Grammar& grammar) const;
156
168 bool IsInfinite(Grammar& grammar) const;
169
176 bool HasDirectLeftRecursion(const Grammar& grammar) const;
177
183 bool HasIndirectLeftRecursion(const Grammar& grammar) const;
184
190 bool HasCycle(const std::unordered_map<std::string, std::unordered_set<std::string>>& graph) const;
191
197 std::unordered_set<std::string> NullableSymbols(const Grammar& grammar) const;
198
199 // -------- TRANSFORMATIONS --------
209 void RemoveLeftRecursion(Grammar& grammar);
210
221 void LeftFactorize(Grammar& grammar);
222
234 std::vector<std::string>
235 LongestCommonPrefix(const std::vector<production>& productions);
236
250 bool StartsWith(const production& prod,
251 const std::vector<std::string>& prefix);
252
265 std::string GenerateNewNonTerminal(Grammar& grammar,
266 const std::string& base);
267
278 void NormalizeNonTerminals(FactoryItem& item, const std::string& nt) const;
279
291 void AdjustTerminals(FactoryItem& base, const FactoryItem& cmb,
292 const std::string& target_nt) const;
293
304 std::unordered_map<std::string, std::vector<production>>
305 Merge(const FactoryItem& base, const FactoryItem& cmb) const;
306
311 std::vector<FactoryItem> items;
312
316 std::vector<std::string> terminal_alphabet_{"a", "b", "c", "d", "e", "f",
317 "g", "h", "i", "j", "k", "l"};
318
322 std::vector<std::string> non_terminal_alphabet_{"A", "B", "C", "D",
323 "E", "F", "G"};
324};
std::vector< std::string > production
Represents the right-hand side of a grammar rule.
Definition grammar.hpp:17
Represents an individual grammar item with its associated symbol table.
Definition grammar_factory.hpp:21
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:26
SymbolTable st_
Symbol table associated with this grammar item.
Definition grammar_factory.hpp:31
FactoryItem(const std::unordered_map< std::string, std::vector< production > > &grammar)
Constructor that initializes a FactoryItem with the provided grammar.
Definition grammar_factory.cpp:533
Responsible for creating and managing grammar items and performing checks on grammars.
Definition grammar_factory.hpp:14
FactoryItem CreateLv2Item()
Creates a Level 2 grammar item for use in grammar generation.
Definition grammar_factory.cpp:203
Grammar GenLL1Grammar(int level)
Generates a LL(1) random grammar based on the specified difficulty level.
Definition grammar_factory.cpp:74
void LeftFactorize(Grammar &grammar)
Perfoms left factorization. A grammar could be left factorized if it have productions with the same p...
Definition grammar_factory.cpp:437
Grammar Lv6()
Generates a Level 6 grammar by combining Level 5 and Level 1 items.
Definition grammar_factory.cpp:168
std::vector< std::string > terminal_alphabet_
A vector of terminal symbols (alphabet) used in the grammar.
Definition grammar_factory.hpp:316
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:225
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:569
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:621
Grammar Lv1()
Generates a Level 1 grammar.
Definition grammar_factory.cpp:105
std::vector< FactoryItem > items
A vector of FactoryItem objects representing different level 1 grammar items created by the Init meth...
Definition grammar_factory.hpp:311
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:395
Grammar Lv2()
Generates a Level 2 grammar by combining Level 1 items.
Definition grammar_factory.cpp:112
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:323
Grammar PickOne(int level)
Picks a random grammar based on the specified difficulty level (1, 2, or 3).
Definition grammar_factory.cpp:48
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:288
Grammar Lv5()
Generates a Level 5 grammar by combining Level 4 and Level 1 items.
Definition grammar_factory.cpp:150
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:255
void Init()
Initializes the GrammarFactory and populates the items vector with initial grammar items.
Definition grammar_factory.cpp:10
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:551
Grammar Lv4()
Generates a Level 4 grammar by combining Level 3 and Level 1 items.
Definition grammar_factory.cpp:132
Grammar GenSLR1Grammar(int level)
Generates a SLR(1) random grammar based on the specified difficulty lefel.
Definition grammar_factory.cpp:94
std::unordered_set< std::string > NullableSymbols(const Grammar &grammar) const
Find nullable symbols in a grammar.
Definition grammar_factory.cpp:361
Grammar Lv3()
Generates a Level 3 grammar by combining a Level 2 item and a Level 1 item.
Definition grammar_factory.cpp:116
bool StartsWith(const production &prod, const std::vector< std::string > &prefix)
Checks if a production starts with a given prefix.
Definition grammar_factory.cpp:511
std::vector< std::string > LongestCommonPrefix(const std::vector< production > &productions)
Finds the longest common prefix among a set of productions.
Definition grammar_factory.cpp:492
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:523
Grammar Lv7()
Generates a Level 7 grammar by combining Level 6 and Level 1 items.
Definition grammar_factory.cpp:186
std::vector< std::string > non_terminal_alphabet_
A vector of non-terminal symbols (alphabet) used in the grammar.
Definition grammar_factory.hpp:322
bool HasIndirectLeftRecursion(const Grammar &grammar) const
Checks if a grammar contains indirect left recursion.
Definition grammar_factory.cpp:299
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
Definition grammar.hpp:27
Stores and manages grammar symbols, including their classification and special markers.
Definition symbol_table.hpp:26