6#include <unordered_map>
26 std::unordered_map<std::string, std::vector<production>>
g_;
38 explicit FactoryItem(
const std::unordered_map<std::string, std::vector<production>> &grammar);
190 bool HasCycle(
const std::unordered_map<std::string, std::unordered_set<std::string>>& graph)
const;
234 std::vector<std::string>
251 const std::vector<std::string>& prefix);
266 const std::string& base);
292 const std::string& target_nt)
const;
304 std::unordered_map<std::string, std::vector<production>>
317 "g",
"h",
"i",
"j",
"k",
"l"};
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