3#include <unordered_map>
6using production = std::vector<std::string>;
18 explicit Grammar(std::string filename);
42 void AddRule(
const std::string& antecedent,
const std::string& consequent);
52 void SetAxiom(
const std::string& axiom);
77 std::vector<std::pair<const std::string, production>>
99 static std::vector<std::string>
Split(
const std::string& s);
114 const std::vector<std::string>& consequent);
120 std::unordered_map<std::string, std::vector<production>>
g_;
void SetAxiom(const std::string &axiom)
Sets the axiom (entry point) of the grammar.
Definition grammar.cpp:132
Grammar(std::string filename)
Constructs a grammar by reading from the specified file.
Definition grammar.cpp:11
void AddRule(const std::string &antecedent, const std::string &consequent)
Adds a rule to the grammar.
Definition grammar.cpp:123
static bool HasLeftRecursion(const std::string &antecedent, const std::vector< std::string > &consequent)
Checks if a rule exhibits left recursion.
Definition grammar.cpp:170
bool HasEmptyProduction(const std::string &antecedent)
Checks if a given antecedent has an empty production.
Definition grammar.cpp:136
std::string axiom_
The axiom or entry point of the grammar.
Definition grammar.hpp:125
std::vector< std::pair< const std::string, production > > FilterRulesByConsequent(const std::string &arg)
Filters grammar rules that contain a specific token in their consequent.
Definition grammar.cpp:144
const std::string kFilename
The filename from which the grammar is read.
Definition grammar.hpp:130
std::unordered_map< std::string, std::vector< production > > g_
Stores the grammar rules with each antecedent mapped to a list of productions.
Definition grammar.hpp:120
void ReadFromFile()
Reads and loads the grammar from a file.
Definition grammar.cpp:15
static std::vector< std::string > Split(const std::string &s)
Splits a production string into individual tokens.
Definition grammar.cpp:87
void Debug()
Prints the current grammar structure to standard output.
Definition grammar.cpp:157