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/*
2 * SyntaxTutor - Interactive Tutorial About Syntax Analyzers
3 * Copyright (C) 2025 Jose R. (jose-rzm)
4 *
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18#pragma once
19#include "symbol_table.hpp"
20#include <string>
21#include <unordered_map>
22#include <vector>
23
34using production = std::vector<std::string>;
35
46struct Grammar {
47
49 explicit Grammar(
50 const std::unordered_map<std::string, std::vector<production>>&
51 grammar);
52
61 void SetAxiom(const std::string& axiom);
62
73 bool HasEmptyProduction(const std::string& antecedent) const;
74
86 std::vector<std::pair<const std::string, production>>
87 FilterRulesByConsequent(const std::string& arg) const;
88
95 void Debug() const; // NOSONAR
96
109 void AddProduction(const std::string& antecedent,
110 const std::vector<std::string>& consequent);
111
123 std::vector<std::string> Split(const std::string& s);
124
129 std::unordered_map<std::string, std::vector<production>> g_;
130
134 std::string axiom_;
135
140};
std::vector< std::string > production
Represents the right-hand side of a grammar rule.
Definition grammar.hpp:34
void SetAxiom(const std::string &axiom)
Sets the axiom (entry point) of the grammar.
Definition grammar.cpp:50
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:62
bool HasEmptyProduction(const std::string &antecedent) const
Checks if a given antecedent has an empty production.
Definition grammar.cpp:54
std::string axiom_
The axiom or entry point of the grammar.
Definition grammar.hpp:134
void Debug() const
Prints the current grammar structure to standard output.
Definition grammar.cpp:76
SymbolTable st_
Symbol table of the grammar.
Definition grammar.hpp:139
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:93
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:129
std::vector< std::string > Split(const std::string &s)
Splits a string into grammar symbols using the current symbol table.
Definition grammar.cpp:98
Stores and manages grammar symbols, including their classification and special markers.
Definition symbol_table.hpp:45