1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the utils of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "parsetable.h"
30
31#include "lalr.h"
32
33#include <QtCore/qtextstream.h>
34
35ParseTable::ParseTable (QTextStream &o):
36 out (o)
37{
38}
39
40void ParseTable::operator () (Automaton *aut)
41{
42 Grammar *g = aut->_M_grammar;
43
44 int rindex = 1;
45 for (RulePointer rule = g->rules.begin (); rule != g->rules.end (); ++rule)
46 out << rindex++ << ")\t" << *rule << Qt::endl;
47 out << Qt::endl << Qt::endl;
48
49 int index = 0;
50 for (StatePointer state = aut->states.begin (); state != aut->states.end (); ++state)
51 {
52 out << "state " << index++ << Qt::endl << Qt::endl;
53
54 for (ItemPointer item = state->kernel.begin (); item != state->kernel.end (); ++item)
55 {
56 out << " * " << *item;
57
58 if (item->dot == item->end_rhs ())
59 out << " " << aut->lookaheads [item];
60
61 out << Qt::endl;
62 }
63
64 bool first = true;
65 for (Bundle::iterator arrow = state->bundle.begin (); arrow != state->bundle.end (); ++arrow)
66 {
67 if (! g->isTerminal (name: arrow.key ()))
68 continue;
69
70 if (first)
71 out << Qt::endl;
72
73 first = false;
74
75 out << " " << *arrow.key () << " shift, and go to state " << std::distance (first: aut->states.begin (), last: *arrow) << Qt::endl;
76 }
77
78 first = true;
79 for (ItemPointer item = state->closure.begin (); item != state->closure.end (); ++item)
80 {
81 if (item->dot != item->end_rhs () || item->rule == state->defaultReduce)
82 continue;
83
84 if (first)
85 out << Qt::endl;
86
87 first = false;
88
89 const auto lookaheads = aut->lookaheads.value(akey: item);
90 for (const Name &la : lookaheads)
91 out << " " << *la << " reduce using rule " << aut->id (rule: item->rule) << " (" << *item->rule->lhs << ")" << Qt::endl;
92 }
93
94 first = true;
95 for (Bundle::iterator arrow = state->bundle.begin (); arrow != state->bundle.end (); ++arrow)
96 {
97 if (! g->isNonTerminal (name: arrow.key ()))
98 continue;
99
100 if (first)
101 out << Qt::endl;
102
103 first = false;
104
105 out << " " << *arrow.key () << " go to state " << std::distance (first: aut->states.begin (), last: *arrow) << Qt::endl;
106 }
107
108 if (state->defaultReduce != g->rules.end ())
109 {
110 out << Qt::endl
111 << " $default reduce using rule " << aut->id (rule: state->defaultReduce) << " (" << *state->defaultReduce->lhs << ")" << Qt::endl;
112 }
113
114 out << Qt::endl;
115 }
116}
117

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