Represents a context-free grammar, including its rules, symbol table, and starting symbol. More...
#include <grammar.hpp>
Public Member Functions | |
Grammar () | |
Grammar (const std::unordered_map< std::string, std::vector< production > > &grammar) | |
void | SetAxiom (const std::string &axiom) |
Sets the axiom (entry point) of the grammar. | |
bool | HasEmptyProduction (const std::string &antecedent) const |
Checks if a given antecedent has an empty production. | |
std::vector< std::pair< const std::string, production > > | FilterRulesByConsequent (const std::string &arg) const |
Filters grammar rules that contain a specific token in their consequent. | |
void | Debug () const |
Prints the current grammar structure to standard output. | |
void | AddProduction (const std::string &antecedent, const std::vector< std::string > &consequent) |
Adds a production rule to the grammar and updates the symbol table. | |
std::vector< std::string > | Split (const std::string &s) |
Splits a string into grammar symbols using the current symbol table. | |
Public Attributes | |
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. | |
SymbolTable | st_ |
Symbol table of the grammar. | |
Represents a context-free grammar, including its rules, symbol table, and starting symbol.
This structure encapsulates all components required to define and manipulate a grammar, including production rules, the associated symbol table, and metadata such as the start symbol. It supports construction, transformation, and analysis of grammars.
|
default |
|
explicit |
void Grammar::AddProduction | ( | const std::string & | antecedent, |
const std::vector< std::string > & | consequent ) |
Adds a production rule to the grammar and updates the symbol table.
This function inserts a new production of the form A → α into the grammar, where antecedent
is the non-terminal A and consequent
is the sequence α. It also updates the internal symbol table to reflect any new symbols introduced.
antecedent | The left-hand side non-terminal of the production. |
consequent | The right-hand side sequence of grammar symbols. |
void Grammar::Debug | ( | ) | const |
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.
std::vector< std::pair< const std::string, production > > Grammar::FilterRulesByConsequent | ( | const std::string & | arg | ) | const |
Filters grammar rules that contain a specific token in their consequent.
arg | The token to search for within the consequents of the rules. |
Searches for rules in which the specified token is part of the consequent and returns those rules.
bool Grammar::HasEmptyProduction | ( | const std::string & | antecedent | ) | const |
Checks if a given antecedent has an empty production.
antecedent | The left-hand side (LHS) symbol to check. |
An empty production is represented as <antecedent> -> ;
, indicating that the antecedent can produce an empty string.
void Grammar::SetAxiom | ( | const std::string & | axiom | ) |
Sets the axiom (entry point) of the grammar.
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.
std::vector< std::string > Grammar::Split | ( | const std::string & | s | ) |
Splits a string into grammar symbols using the current symbol table.
This function tokenizes the input string s
into a sequence of grammar symbols based on the known entries in the symbol table. It uses a greedy approach, matching the longest valid symbol at each step.
s | The input string to split. |
std::string Grammar::axiom_ |
The axiom or entry point of the grammar.
std::unordered_map<std::string, std::vector<production> > Grammar::g_ |
Stores the grammar rules with each antecedent mapped to a list of productions.
SymbolTable Grammar::st_ |
Symbol table of the grammar.