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#ifndef SLRWIZARD_H
2#define SLRWIZARD_H
3
4#include <QLabel>
5#include <QLineEdit>
6#include <QVBoxLayout>
7#include <QWizard>
8#include <QWizardPage>
10#include "slrwizardpage.h"
11
25class SLRWizard : public QWizard
26{
27 Q_OBJECT
28public:
39 const QVector<QVector<QString>> &rawTable,
40 const QStringList &colHeaders,
41 const QVector<QPair<QString, QVector<QString>>> &sortedGrammar,
42 QWidget *parent = nullptr)
43 : QWizard(parent)
44 {
45 setWindowTitle(tr("Ayuda interactiva: Tabla SLR(1)"));
46
47 const int nTerm = parser.gr_.st_.terminals_.contains(parser.gr_.st_.EPSILON_)
48 ? parser.gr_.st_.terminals_.size() - 1
49 : parser.gr_.st_.terminals_.size();
50 SLRWizardPage *last = nullptr;
51 // Generar explicación y páginas
52 int rows = rawTable.size();
53 int cols = colHeaders.size();
54 for (int i = 0; i < rows; ++i) {
55 for (int j = 0; j < cols; ++j) {
56 QString sym = colHeaders[j];
57 QString expected;
58 QString explanation;
59 if (j < nTerm) {
60 auto itAct = parser.actions_.at(i).find(sym.toStdString());
62 = (itAct != parser.actions_.at(i).end()
63 ? itAct->second
64 : SLR1Parser::s_action{nullptr, SLR1Parser::Action::Empty});
65 switch (act.action) {
67 unsigned to = parser.transitions_.at(i).at(sym.toStdString());
68 expected = QString("s%1").arg(to);
69 explanation = tr("Estado %1: existe transición δ(%1, '%2'). ¿A qué "
70 "estado harías shift?")
71 .arg(i)
72 .arg(sym);
73 break;
74 }
76 int idx = -1;
77 for (int k = 0; k < sortedGrammar.size(); ++k) {
78 auto &rule = sortedGrammar[k];
79 if (rule.first.toStdString() == act.item->antecedent_
80 && stdVectorToQVector(act.item->consequent_) == rule.second) {
81 idx = k;
82 break;
83 }
84 }
85 expected = QString("r%1").arg(idx);
86 // explicación con FOLLOW
87 std::unordered_set<std::string> F;
88 F = parser.Follow(act.item->antecedent_);
89 QStringList followList;
90 for (auto &t : F)
91 followList << QString::fromStdString(t);
92 explanation = tr("Estado %1: contiene el ítem [%2 → ...·] y '%3' ∈ "
93 "SIG(%2). ¿Qué regla usas para reducir (0, 1, ...)?")
94 .arg(i)
95 .arg(QString::fromStdString(act.item->antecedent_))
96 .arg(colHeaders[j]);
97 break;
98 }
100 expected = "acc";
101 explanation = tr("Estado %1: contiene [S → A · $]. ¿Qué palabra clave "
102 "usas para aceptar?")
103 .arg(i);
104 break;
106 default:
107 continue;
108 }
109 } else {
110 // GOTO sobre no terminal
111 auto nonT = sym.toStdString();
112 if (!parser.transitions_.contains(i)) {
113 continue;
114 }
115 auto itGo = parser.transitions_.at(i).find(nonT);
116 if (itGo != parser.transitions_.at(i).end()) {
117 expected = QString::number(itGo->second);
118 explanation = tr("Estado %1: δ(%1, '%2') existe. ¿A qué estado va "
119 "la transición? (pon solo el número)")
120 .arg(i)
121 .arg(sym);
122 } else {
123 continue;
124 }
125 }
126
127 SLRWizardPage *page = new SLRWizardPage(i, sym, explanation, expected, this);
128 last = page;
129 addPage(page);
130 }
131 }
132 if (last) {
133 last->setFinalPage(true);
134 }
135 }
136
142 QVector<QString> stdVectorToQVector(const std::vector<std::string> &vec)
143 {
144 QVector<QString> result;
145 result.reserve(vec.size());
146 for (const auto &str : vec) {
147 result.push_back(QString::fromStdString(str));
148 }
149 return result;
150 }
151};
152
153#endif // SLRWIZARD_H
Implements an SLR(1) parser for context-free grammars.
Definition slr1_parser.hpp:20
action_table actions_
The action table used by the parser to determine shift/reduce actions.
Definition slr1_parser.hpp:292
Grammar gr_
The grammar being processed by the parser.
Definition slr1_parser.hpp:280
transition_table transitions_
The transition table used by the parser to determine state transitions.
Definition slr1_parser.hpp:296
@ Shift
Definition slr1_parser.hpp:33
@ Accept
Definition slr1_parser.hpp:33
@ Empty
Definition slr1_parser.hpp:33
@ Reduce
Definition slr1_parser.hpp:33
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:352
A single step in the SLR(1) guided assistant for table construction.
Definition slrwizardpage.h:22
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:38
QVector< QString > stdVectorToQVector(const std::vector< std::string > &vec)
Converts a std::vector<std::string> to QVector<QString> for UI compatibility.
Definition slrwizard.h:142
@ F
Definition slrtutorwindow.h:51
SymbolTable st_
Symbol table of the grammar.
Definition grammar.hpp:118
std::string antecedent_
The non-terminal on the left-hand side of the production.
Definition lr0_item.hpp:18
std::vector< std::string > consequent_
The sequence of symbols on the right-hand side of the production.
Definition lr0_item.hpp:23
Definition slr1_parser.hpp:46
const Lr0Item * item
Definition slr1_parser.hpp:47
Action action
Definition slr1_parser.hpp:48
std::string EPSILON_
Epsilon symbol, representing empty transitions, initialized as "EPSILON".
Definition symbol_table.hpp:32
std::unordered_set< std::string > terminals_
Set of all terminal symbols (including EOL).
Definition symbol_table.hpp:42