23#include <unordered_map>
43 std::unordered_map<std::string, std::vector<production>>
g_;
56 const std::unordered_map<std::string, std::vector<production>>&
212 const std::unordered_map<std::string, std::unordered_set<std::string>>&
220 std::unordered_set<std::string>
280 std::vector<std::string>
297 const std::vector<std::string>& prefix);
312 const std::string& base);
341 const std::string& target_nt)
const;
356 std::unordered_map<std::string, std::vector<production>>
369 "g",
"h",
"i",
"j",
"k",
"l"};
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