LL1Checker 5.0
“Tool for verifying LL(1) grammars and validating input strings.”
Loading...
Searching...
No Matches
grammar.hpp
1#pragma once
2#include "symbol_table.hpp"
3#include <string>
4#include <unordered_map>
5#include <vector>
6using production = std::vector<symbol_table::TokenID>;
7
8struct Grammar {
9
18 explicit Grammar(std::string filename);
19
30 void ReadFromFile();
31
42 void AddRule(symbol_table::TokenID antecedent,
43 const production& consequent);
44
53 void SetAxiom(symbol_table::TokenID axiom);
54
66 std::vector<std::pair<symbol_table::TokenID, production>>
67 FilterRulesByConsequent(symbol_table::TokenID arg);
68
75 void Debug();
76
88 static std::vector<symbol_table::TokenID> Split(const std::string& s);
89
97 std::string GenerateNewNonTerminal(const std::string& base);
98
103 std::unordered_map<symbol_table::TokenID, std::vector<production>> g_;
104
108 std::vector<symbol_table::TokenID> nt_order_{};
109
113 symbol_table::TokenID axiom_{};
114
118 const std::string kFilename;
119};
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