SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
 
Loading...
Searching...
No Matches
grammar.hpp
Go to the documentation of this file.
1#pragma once
2#include "symbol_table.hpp"
3#include <string>
4#include <unordered_map>
5#include <vector>
6
17using production = std::vector<std::string>;
18
27struct Grammar {
28
30 explicit Grammar(
31 const std::unordered_map<std::string, std::vector<production>>&
32 grammar);
33
34
43 void SetAxiom(const std::string& axiom);
44
55 bool HasEmptyProduction(const std::string& antecedent) const;
56
68 std::vector<std::pair<const std::string, production>> FilterRulesByConsequent(
69 const std::string& arg) const;
70
77 void Debug() const; //NOSONAR
78
89 void AddProduction(const std::string& antecedent,
90 const std::vector<std::string>& consequent);
91
102 std::vector<std::string> Split(const std::string& s);
103
108 std::unordered_map<std::string, std::vector<production>> g_;
109
113 std::string axiom_;
114
119};
std::vector< std::string > production
Represents the right-hand side of a grammar rule.
Definition grammar.hpp:17
void SetAxiom(const std::string &axiom)
Sets the axiom (entry point) of the grammar.
Definition grammar.cpp:32
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.
Definition grammar.cpp:45
bool HasEmptyProduction(const std::string &antecedent) const
Checks if a given antecedent has an empty production.
Definition grammar.cpp:37
std::string axiom_
The axiom or entry point of the grammar.
Definition grammar.hpp:113
void Debug() const
Prints the current grammar structure to standard output.
Definition grammar.cpp:61
SymbolTable st_
Symbol table of the grammar.
Definition grammar.hpp:118
void AddProduction(const std::string &antecedent, const std::vector< std::string > &consequent)
Adds a production rule to the grammar and updates the symbol table.
Definition grammar.cpp:78
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:108
std::vector< std::string > Split(const std::string &s)
Splits a string into grammar symbols using the current symbol table.
Definition grammar.cpp:83
Stores and manages grammar symbols, including their classification and special markers.
Definition symbol_table.hpp:26