1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "parsetable.h"
5
6#include "lalr.h"
7
8#include <QtCore/qtextstream.h>
9
10ParseTable::ParseTable (QTextStream &o):
11 out (o)
12{
13}
14
15void ParseTable::operator () (Automaton *aut)
16{
17 Grammar *g = aut->_M_grammar;
18
19 int rindex = 1;
20 for (RulePointer rule = g->rules.begin (); rule != g->rules.end (); ++rule)
21 out << rindex++ << ")\t" << *rule << Qt::endl;
22 out << Qt::endl << Qt::endl;
23
24 int index = 0;
25 for (StatePointer state = aut->states.begin (); state != aut->states.end (); ++state)
26 {
27 out << "state " << index++ << Qt::endl << Qt::endl;
28
29 for (ItemPointer item = state->kernel.begin (); item != state->kernel.end (); ++item)
30 {
31 out << " * " << *item;
32
33 if (item->dot == item->end_rhs ())
34 out << " " << aut->lookaheads [item];
35
36 out << Qt::endl;
37 }
38
39 bool first = true;
40 for (Bundle::iterator arrow = state->bundle.begin (); arrow != state->bundle.end (); ++arrow)
41 {
42 if (! g->isTerminal (name: arrow.key ()))
43 continue;
44
45 if (first)
46 out << Qt::endl;
47
48 first = false;
49
50 out << " " << *arrow.key () << " shift, and go to state " << std::distance (first: aut->states.begin (), last: *arrow) << Qt::endl;
51 }
52
53 first = true;
54 for (ItemPointer item = state->closure.begin (); item != state->closure.end (); ++item)
55 {
56 if (item->dot != item->end_rhs () || item->rule == state->defaultReduce)
57 continue;
58
59 if (first)
60 out << Qt::endl;
61
62 first = false;
63
64 const auto lookaheads = aut->lookaheads.value(key: item);
65 for (const Name &la : lookaheads)
66 out << " " << *la << " reduce using rule " << aut->id (rule: item->rule) << " (" << *item->rule->lhs << ")" << Qt::endl;
67 }
68
69 first = true;
70 for (Bundle::iterator arrow = state->bundle.begin (); arrow != state->bundle.end (); ++arrow)
71 {
72 if (! g->isNonTerminal (name: arrow.key ()))
73 continue;
74
75 if (first)
76 out << Qt::endl;
77
78 first = false;
79
80 out << " " << *arrow.key () << " go to state " << std::distance (first: aut->states.begin (), last: *arrow) << Qt::endl;
81 }
82
83 if (state->defaultReduce != g->rules.end ())
84 {
85 out << Qt::endl
86 << " $default reduce using rule " << aut->id (rule: state->defaultReduce) << " (" << *state->defaultReduce->lhs << ")" << Qt::endl;
87 }
88
89 out << Qt::endl;
90 }
91}
92

source code of qtbase/src/tools/qlalr/parsetable.cpp