2#include "symbol_table.hpp"
4#include <unordered_map>
6using production = std::vector<symbol_table::TokenID>;
18 explicit Grammar(std::string filename);
42 void AddRule(symbol_table::TokenID antecedent,
43 const production& consequent);
53 void SetAxiom(symbol_table::TokenID axiom);
66 std::vector<std::pair<symbol_table::TokenID, production>>
88 static std::vector<symbol_table::TokenID>
Split(
const std::string& s);
103 std::unordered_map<symbol_table::TokenID, std::vector<production>>
g_;
void AddRule(symbol_table::TokenID antecedent, const production &consequent)
Adds a rule to the grammar.
Definition grammar.cpp:166
std::string GenerateNewNonTerminal(const std::string &base)
Generate a new non terminal symbol by appending ' to base until it is not in symbol table.
Definition grammar.cpp:158
Grammar(std::string filename)
Constructs a grammar by reading from the specified file.
Definition grammar.cpp:11
void SetAxiom(symbol_table::TokenID axiom)
Sets the axiom (entry point) of the grammar.
Definition grammar.cpp:171
symbol_table::TokenID axiom_
The axiom or entry point of the grammar.
Definition grammar.hpp:113
const std::string kFilename
The filename from which the grammar is read.
Definition grammar.hpp:118
std::vector< symbol_table::TokenID > nt_order_
Keeps the insertion order of non-terminal symbols.
Definition grammar.hpp:108
std::vector< std::pair< symbol_table::TokenID, production > > FilterRulesByConsequent(symbol_table::TokenID arg)
Filters grammar rules that contain a specific token in their consequent.
Definition grammar.cpp:176
void ReadFromFile()
Reads and loads the grammar from a file.
Definition grammar.cpp:15
std::unordered_map< symbol_table::TokenID, std::vector< production > > g_
Stores the grammar rules with each antecedent mapped to a list of productions.
Definition grammar.hpp:103
static std::vector< symbol_table::TokenID > Split(const std::string &s)
Splits a production string into individual tokens.
Definition grammar.cpp:121
void Debug()
Prints the current grammar structure to standard output.
Definition grammar.cpp:188