LL1Checker 3.0
“Tool for verifying LL(1) grammars and validating input strings.”
Loading...
Searching...
No Matches
grammar.hpp
1#pragma once
2#include <string>
3#include <unordered_map>
4#include <vector>
5
6using production = std::vector<std::string>;
7
8struct Grammar {
9
18 explicit Grammar(std::string filename);
19
30 void ReadFromFile();
31
42 void AddRule(const std::string& antecedent, const std::string& consequent);
43
52 void SetAxiom(const std::string& axiom);
53
64 bool HasEmptyProduction(const std::string& antecedent);
65
77 std::vector<std::pair<const std::string, production>>
78 FilterRulesByConsequent(const std::string& arg);
79
86 void Debug();
87
99 static std::vector<std::string> Split(const std::string& s);
100
113 static bool HasLeftRecursion(const std::string& antecedent,
114 const std::vector<std::string>& consequent);
115
120 std::unordered_map<std::string, std::vector<production>> g_;
121
125 std::string axiom_;
126
130 const std::string kFilename;
131};
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