SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
Loading...
Searching...
No Matches
slrwizard.h
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
19#ifndef SLRWIZARD_H
20#define SLRWIZARD_H
21
23#include "slrwizardpage.h"
24#include <QLabel>
25#include <QLineEdit>
26#include <QVBoxLayout>
27#include <QWizard>
28#include <QWizardPage>
29
45class SLRWizard : public QWizard {
46 Q_OBJECT
47 public:
59 SLRWizard(SLR1Parser& parser, const QVector<QVector<QString>>& rawTable,
60 const QStringList& colHeaders,
61 const QVector<QPair<QString, QVector<QString>>>& sortedGrammar,
62 QWidget* parent = nullptr)
63 : QWizard(parent) {
64 setWindowTitle(tr("Ayuda interactiva: Tabla SLR(1)"));
65
66 const int nTerm =
67 parser.gr_.st_.terminals_.contains(parser.gr_.st_.EPSILON_)
68 ? parser.gr_.st_.terminals_.size() - 1
69 : parser.gr_.st_.terminals_.size();
70 SLRWizardPage* last = nullptr;
71 // Generar explicación y páginas
72 int rows = rawTable.size();
73 int cols = colHeaders.size();
74 for (int i = 0; i < rows; ++i) {
75 for (int j = 0; j < cols; ++j) {
76 QString sym = colHeaders[j];
77 QString expected;
78 QString explanation;
79 if (j < nTerm) {
80 auto itAct = parser.actions_.at(i).find(sym.toStdString());
82 (itAct != parser.actions_.at(i).end()
83 ? itAct->second
84 : SLR1Parser::s_action{nullptr,
85 SLR1Parser::Action::Empty});
86 switch (act.action) {
88 unsigned to =
89 parser.transitions_.at(i).at(sym.toStdString());
90 expected = QString("s%1").arg(to);
91 explanation = tr("Estado %1: existe transición δ(%1, "
92 "'%2'). ¿A qué "
93 "estado harías shift?")
94 .arg(i)
95 .arg(sym);
96 break;
97 }
99 int idx = -1;
100 for (int k = 0; k < sortedGrammar.size(); ++k) {
101 auto& rule = sortedGrammar[k];
102 if (rule.first.toStdString() ==
103 act.item->antecedent_ &&
105 rule.second) {
106 idx = k;
107 break;
108 }
109 }
110 expected = QString("r%1").arg(idx);
111 // explicación con FOLLOW
112 std::unordered_set<std::string> F;
113 F = parser.Follow(act.item->antecedent_);
114 QStringList followList;
115 for (auto& t : F)
116 followList << QString::fromStdString(t);
117 explanation = tr("Estado %1: contiene el ítem [%2 → "
118 "...·] y '%3' ∈ "
119 "SIG(%2). ¿Qué regla usas para "
120 "reducir (0, 1, ...)?")
121 .arg(i)
122 .arg(QString::fromStdString(
123 act.item->antecedent_))
124 .arg(colHeaders[j]);
125 break;
126 }
128 expected = "acc";
129 explanation = tr("Estado %1: contiene [S → A · $]. "
130 "¿Qué palabra clave "
131 "usas para aceptar?")
132 .arg(i);
133 break;
135 default:
136 continue;
137 }
138 } else {
139 // GOTO sobre no terminal
140 auto nonT = sym.toStdString();
141 if (!parser.transitions_.contains(i)) {
142 continue;
143 }
144 auto itGo = parser.transitions_.at(i).find(nonT);
145 if (itGo != parser.transitions_.at(i).end()) {
146 expected = QString::number(itGo->second);
147 explanation = tr("Estado %1: δ(%1, '%2') existe. ¿A "
148 "qué estado va "
149 "la transición? (pon solo el número)")
150 .arg(i)
151 .arg(sym);
152 } else {
153 continue;
154 }
155 }
156
157 SLRWizardPage* page =
158 new SLRWizardPage(i, sym, explanation, expected, this);
159 last = page;
160 addPage(page);
161 }
162 }
163 if (last) {
164 last->setFinalPage(true);
165 }
166 }
167
174 QVector<QString> stdVectorToQVector(const std::vector<std::string>& vec) {
175 QVector<QString> result;
176 result.reserve(vec.size());
177 for (const auto& str : vec) {
178 result.push_back(QString::fromStdString(str));
179 }
180 return result;
181 }
182};
183
184#endif // SLRWIZARD_H
Implements an SLR(1) parser for context-free grammars.
Definition slr1_parser.hpp:38
action_table actions_
The action table used by the parser to determine shift/reduce actions.
Definition slr1_parser.hpp:308
Grammar gr_
The grammar being processed by the parser.
Definition slr1_parser.hpp:296
transition_table transitions_
The transition table used by the parser to determine state transitions.
Definition slr1_parser.hpp:312
@ Shift
Definition slr1_parser.hpp:51
@ Accept
Definition slr1_parser.hpp:51
@ Empty
Definition slr1_parser.hpp:51
@ Reduce
Definition slr1_parser.hpp:51
std::unordered_set< std::string > Follow(const std::string &arg)
Computes the FOLLOW set for a given non-terminal symbol in the grammar.
Definition slr1_parser.cpp:370
A single step in the SLR(1) guided assistant for table construction.
Definition slrwizardpage.h:41
SLRWizard(SLR1Parser &parser, const QVector< QVector< QString > > &rawTable, const QStringList &colHeaders, const QVector< QPair< QString, QVector< QString > > > &sortedGrammar, QWidget *parent=nullptr)
Constructs the SLR(1) wizard with all necessary parsing context.
Definition slrwizard.h:59
QVector< QString > stdVectorToQVector(const std::vector< std::string > &vec)
Converts a std::vector<std::string> to QVector<QString> for UI compatibility.
Definition slrwizard.h:174
@ F
Definition slrtutorwindow.h:69
SymbolTable st_
Symbol table of the grammar.
Definition grammar.hpp:139
std::string antecedent_
The non-terminal on the left-hand side of the production.
Definition lr0_item.hpp:39
std::vector< std::string > consequent_
The sequence of symbols on the right-hand side of the production.
Definition lr0_item.hpp:44
Definition slr1_parser.hpp:64
const Lr0Item * item
Definition slr1_parser.hpp:65
Action action
Definition slr1_parser.hpp:66
std::string EPSILON_
Epsilon symbol, representing empty transitions, initialized as "EPSILON".
Definition symbol_table.hpp:51
std::unordered_set< std::string > terminals_
Set of all terminal symbols (including EOL).
Definition symbol_table.hpp:61