|
| Grammar (std::string filename) |
| Constructs a grammar by reading from the specified file.
|
|
void | ReadFromFile () |
| Reads and loads the grammar from a file.
|
|
void | AddRule (const std::string &antecedent, const std::string &consequent) |
| Adds a rule to the grammar.
|
|
void | SetAxiom (const std::string &axiom) |
| Sets the axiom (entry point) of the grammar.
|
|
bool | HasEmptyProduction (const std::string &antecedent) |
| Checks if a given antecedent has an empty production.
|
|
std::vector< std::pair< const std::string, production > > | FilterRulesByConsequent (const std::string &arg) |
| Filters grammar rules that contain a specific token in their consequent.
|
|
void | Debug () |
| Prints the current grammar structure to standard output.
|
|
|
static std::vector< std::string > | Split (const std::string &s) |
| Splits a production string into individual tokens.
|
|
static bool | HasLeftRecursion (const std::string &antecedent, const std::vector< std::string > &consequent) |
| Checks if a rule exhibits left recursion.
|
|
|
std::unordered_map< std::string, std::vector< production > > | g_ |
| Stores the grammar rules with each antecedent mapped to a list of productions.
|
|
std::string | axiom_ |
| The axiom or entry point of the grammar.
|
|
const std::string | kFilename |
| The filename from which the grammar is read.
|
|
◆ Grammar()
Grammar::Grammar |
( |
std::string | filename | ) |
|
|
explicit |
Constructs a grammar by reading from the specified file.
- Parameters
-
filename | Path to the file containing the grammar definitions. |
Initializes the grammar by setting the filename for input. The actual grammar data is read and processed through the read_from_file
function.
◆ AddRule()
void Grammar::AddRule |
( |
const std::string & | antecedent, |
|
|
const std::string & | consequent ) |
Adds a rule to the grammar.
- Parameters
-
antecedent | The left-hand side (LHS) symbol of the rule. |
consequent | The right-hand side (RHS) of the rule as a string. |
Adds a rule to the grammar by specifying the antecedent symbol and the consequent production. This function processes and adds each rule for parsing.
◆ Debug()
Prints the current grammar structure to standard output.
This function provides a debug view of the grammar by printing out all rules, the axiom, and other relevant details.
◆ FilterRulesByConsequent()
std::vector< std::pair< const std::string, production > > Grammar::FilterRulesByConsequent |
( |
const std::string & | arg | ) |
|
Filters grammar rules that contain a specific token in their consequent.
- Parameters
-
arg | The token to search for within the consequents of the rules. |
- Returns
- std::vector of pairs where each pair contains an antecedent and its respective production that includes the specified token.
Searches for rules in which the specified token is part of the consequent and returns those rules.
◆ HasEmptyProduction()
bool Grammar::HasEmptyProduction |
( |
const std::string & | antecedent | ) |
|
Checks if a given antecedent has an empty production.
- Parameters
-
antecedent | The left-hand side (LHS) symbol to check. |
- Returns
- true if there exists an empty production for the antecedent, otherwise false.
An empty production is represented as <antecedent> -> ;
, indicating that the antecedent can produce an empty string.
◆ HasLeftRecursion()
bool Grammar::HasLeftRecursion |
( |
const std::string & | antecedent, |
|
|
const std::vector< std::string > & | consequent ) |
|
static |
Checks if a rule exhibits left recursion.
- Parameters
-
antecedent | The left-hand side (LHS) symbol of the rule. |
consequent | The right-hand side (RHS) vector of tokens of the rule. |
- Returns
- true if the rule has left recursion (e.g., A -> A + A), otherwise false.
Left recursion is identified when the antecedent of a rule appears as the first symbol in its consequent, which may cause issues in top-down parsing algorithms.
◆ ReadFromFile()
void Grammar::ReadFromFile |
( |
| ) |
|
Reads and loads the grammar from a file.
This function reads the grammar rules from the specified file, parsing each rule and storing it in the grammar structure. The file format requirements are outlined in the README.md.
- Exceptions
-
GrammarError | if there are errors reading symbols, parsing the grammar, or splitting the rules as specified in the input file. |
◆ SetAxiom()
void Grammar::SetAxiom |
( |
const std::string & | axiom | ) |
|
Sets the axiom (entry point) of the grammar.
- Parameters
-
axiom | The entry point or start symbol of the grammar. |
Defines the starting point for the grammar, which is used in parsing algorithms and must be a non-terminal symbol present in the grammar.
◆ Split()
std::vector< std::string > Grammar::Split |
( |
const std::string & | s | ) |
|
|
static |
Splits a production string into individual tokens.
- Parameters
-
s | The production string to split. |
- Returns
- std::vector containing tokens extracted from the production string.
The function decomposes a production string into individual symbols based on the symbol table, allowing terminals and non-terminals to be identified.
The documentation for this struct was generated from the following files: