Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
lalr.h
Go to the documentation of this file.
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#ifndef LALR_H
5#define LALR_H
6
7#include <QtCore/qset.h>
8#include <QtCore/qstack.h>
9#include <QtCore/qmap.h>
10#include <QtCore/qstring.h>
11#include <QtCore/qtextstream.h>
12
13#include <algorithm>
14#include <functional>
15#include <set>
16#include <list>
17
18class Rule;
19class State;
20class Grammar;
21class Item;
22class State;
23class Arrow;
24class Automaton;
25
26
27// names
28typedef std::list<QString>::iterator Name;
29typedef std::list<Name> NameList;
30typedef std::set<Name> NameSet;
31
32// items
33typedef std::list<Item> ItemList;
34typedef ItemList::iterator ItemPointer;
35
36// rules
37typedef std::list<Rule> debug_infot;
38typedef debug_infot::iterator RulePointer;
39typedef QMultiMap<Name, RulePointer> RuleMap;
40
41// states
42typedef std::list<State> StateList;
43typedef StateList::iterator StatePointer;
44
45// arrows
46typedef QMap<Name, StatePointer> Bundle;
47
48class Rule
49{
50public:
51 void clear ()
52 {
53 lhs = Name ();
54 rhs.clear ();
55 prec = Name ();
56 }
57
58public:
62};
63
65{
66public:
69
70 inline bool operator == (const Lookback &other) const
71 { return state == other.state && nt == other.nt; }
72
73 inline bool operator != (const Lookback &other) const
74 { return state != other.state || nt != other.nt; }
75
76 bool operator < (const Lookback &other) const;
77
78public:
81};
82
83class Item
84{
85public:
86 inline NameList::iterator begin_rhs () const
87 { return rule->rhs.begin (); }
88
89 inline NameList::iterator end_rhs () const
90 { return rule->rhs.end (); }
91
92 inline bool operator == (const Item &other) const
93 { return rule == other.rule && dot == other.dot; }
94
95 inline bool operator != (const Item &other) const
96 { return rule != other.rule || dot != other.dot; }
97
98 inline bool isReduceItem () const
99 { return dot == rule->rhs.end (); }
100
101 Item next () const;
102
103public:
105 NameList::iterator dot;
106};
107
108class State
109{
110public:
112
113 inline bool operator == (const State &other) const
114 { return kernel == other.kernel; }
115
116 inline bool operator != (const State &other) const
117 { return kernel != other.kernel; }
118
119 std::pair<ItemPointer, bool> insert(const Item &item);
120 std::pair<ItemPointer, bool> insertClosure(const Item &item);
121
122public: // attributes
126 QMap<Name, NameSet> reads;
127 QMap<Name, NameSet> follows;
129};
130
132// digraph
134template <typename _Tp>
135class Node
136{
137public:
138 typedef std::set<Node<_Tp> > Repository;
139 typedef typename Repository::iterator iterator;
140 typedef typename std::list<iterator>::iterator edge_iterator;
141
142public:
143 static iterator get (_Tp data);
144
145 std::pair<edge_iterator, bool> insertEdge(iterator other) const;
146
147 inline edge_iterator begin () const
148 { return outs.begin (); }
149
150 inline edge_iterator end () const
151 { return outs.end (); }
152
153 inline bool operator == (const Node<_Tp> &other) const
154 { return data == other.data; }
155
156 inline bool operator != (const Node<_Tp> &other) const
157 { return data != other.data; }
158
159 inline bool operator < (const Node<_Tp> &other) const
160 { return data < other.data; }
161
162 static inline iterator begin_nodes ()
163 { return repository ().begin (); }
164
165 static inline iterator end_nodes ()
166 { return repository ().end (); }
167
169 {
170 static Repository r;
171 return r;
172 }
173
174public: // attributes
175 mutable bool root;
176 mutable int dfn;
177 mutable _Tp data;
178 mutable std::list<iterator> outs;
179
180protected:
181 inline Node () {}
182
183 inline Node (_Tp d):
184 root (true), dfn (0), data (d) {}
185};
186
187template <typename _Tp>
189{
190 Node<_Tp> tmp (data);
191 iterator it = repository ().find (tmp);
192
193 if (it != repository ().end ())
194 return it;
195
196 return repository ().insert (tmp).first;
197}
198
199template <typename _Tp>
200std::pair<typename std::list<typename Node<_Tp>::iterator>::iterator, bool> Node<_Tp>::insertEdge(typename Node<_Tp>::iterator other) const
201{
202 edge_iterator it = std::find (outs.begin (), outs.end (), other);
203
204 if (it != outs.end ())
205 return {it, false};
206
207 other->root = false;
208 return {outs.insert (outs.end (), other), true};
209}
210
212// Grammar
215{
216public:
217 Grammar ();
218
219 Name intern (const QString &id);
220 Name intern (const char *id) { return intern(QString::fromUtf8(id)); }
221
222 inline bool isTerminal (Name name) const
223 { return terminals.find (name) != terminals.end (); }
224
225 inline bool isNonTerminal (Name name) const
226 { return non_terminals.find (name) != non_terminals.end (); }
227
228 void buildRuleMap ();
229 void buildExtendedGrammar ();
230
231public:
237 std::list<QString> names;
241 QMap<Name, QString> spells;
250
256
257 struct TokenInfo {
259 int prec;
260 };
261
262 QMap<Name, TokenInfo> token_info;
265};
266
267class Read
268{
269public:
270 inline Read () {}
271
273 state (s), nt (n) {}
274
275 inline bool operator == (const Read &other) const
276 { return state == other.state && nt == other.nt; }
277
278 inline bool operator != (const Read &other) const
279 { return state != other.state || nt != other.nt; }
280
281 bool operator < (const Read &other) const;
282
283public:
286};
287
289{
290public:
291 inline Include () {}
292
294 state (s), nt (n) {}
295
296 inline bool operator == (const Include &other) const
297 { return state == other.state && nt == other.nt; }
298
299 inline bool operator != (const Include &other) const
300 { return state != other.state || nt != other.nt; }
301
302 bool operator < (const Include &other) const;
303
304public:
307};
308
310{
311public:
313
314 std::pair<StatePointer, bool> internState (const State &state);
315
316 typedef Node<Read> ReadsGraph;
318
319 typedef Node<Include> IncludesGraph;
321
322 void build ();
323 void buildNullables ();
324
325 void buildLookbackSets ();
326
327 void buildDirectReads ();
328 void buildReadsDigraph ();
329 void buildReads ();
330 void visitReadNode (ReadNode node);
331
332 void buildIncludesAndFollows ();
333 void buildIncludesDigraph ();
334 void visitIncludeNode (IncludeNode node);
335
336 void buildLookaheads ();
337
338 void buildDefaultReduceActions ();
339
340 void closure (StatePointer state);
341
342 int id (RulePointer rule);
343 int id (StatePointer state);
344 int id (Name name);
345
346 void dump (QTextStream &out, IncludeNode incl);
348 void dump (QTextStream &out, const Lookback &lp);
349
350public: // ### private
355 QMultiMap<ItemPointer, Lookback> lookbacks;
356 QMap<ItemPointer, NameSet> lookaheads;
357
358private:
359 QStack<ReadsGraph::iterator> _M_reads_stack;
360 int _M_reads_dfn;
361
362 QStack<IncludesGraph::iterator> _M_includes_stack;
363 int _M_includes_dfn;
364};
365
366namespace std {
367bool operator < (Name a, Name b);
370}
371
376
381
382#endif // LALR_H
ReadsGraph::iterator ReadNode
Definition lalr.h:317
void dump(QTextStream &out, ReadNode rd)
StatePointer start
Definition lalr.h:353
IncludesGraph::iterator IncludeNode
Definition lalr.h:320
NameSet nullables
Definition lalr.h:354
Grammar * _M_grammar
Definition lalr.h:351
StateList states
Definition lalr.h:352
Node< Read > ReadsGraph
Definition lalr.h:316
Node< Include > IncludesGraph
Definition lalr.h:319
QMultiMap< ItemPointer, Lookback > lookbacks
Definition lalr.h:355
QMap< ItemPointer, NameSet > lookaheads
Definition lalr.h:356
QMap< Name, QString > spells
Definition lalr.h:241
Assoc
Definition lalr.h:251
@ Left
Definition lalr.h:253
@ NonAssoc
Definition lalr.h:252
Name intern(const char *id)
Definition lalr.h:220
int expected_reduce_reduce
Definition lalr.h:249
Name accept_symbol
Definition lalr.h:246
QString merged_output
Definition lalr.h:232
Name tk_end
Definition lalr.h:245
QString impl_file_name
Definition lalr.h:235
Assoc current_assoc
Definition lalr.h:263
QMap< Name, TokenInfo > token_info
Definition lalr.h:262
NameSet terminals
Definition lalr.h:239
NameSet declared_lhs
Definition lalr.h:247
debug_infot rules
Definition lalr.h:242
Name start
Definition lalr.h:238
int current_prec
Definition lalr.h:264
bool isNonTerminal(Name name) const
Definition lalr.h:225
QString token_prefix
Definition lalr.h:236
std::list< QString > names
Definition lalr.h:237
bool isTerminal(Name name) const
Definition lalr.h:222
QString table_name
Definition lalr.h:233
RulePointer goal
Definition lalr.h:244
RuleMap rule_map
Definition lalr.h:243
QString decl_file_name
Definition lalr.h:234
NameSet non_terminals
Definition lalr.h:240
int expected_shift_reduce
Definition lalr.h:248
Include(StatePointer s, Name n)
Definition lalr.h:293
Name nt
Definition lalr.h:306
Include()
Definition lalr.h:291
StatePointer state
Definition lalr.h:305
Definition lalr.h:84
bool isReduceItem() const
Definition lalr.h:98
Item next() const
Definition lalr.cpp:104
NameList::iterator dot
Definition lalr.h:105
bool operator!=(const Item &other) const
Definition lalr.h:95
NameList::iterator end_rhs() const
Definition lalr.h:89
bool operator==(const Item &other) const
Definition lalr.h:92
NameList::iterator begin_rhs() const
Definition lalr.h:86
RulePointer rule
Definition lalr.h:104
StatePointer state
Definition lalr.h:79
Lookback(StatePointer s, Name n)
Definition lalr.h:67
bool operator<(const Lookback &other) const
Definition lalr.cpp:66
bool operator!=(const Lookback &other) const
Definition lalr.h:73
Name nt
Definition lalr.h:80
bool operator==(const Lookback &other) const
Definition lalr.h:70
Definition lalr.h:136
bool operator<(const Node< _Tp > &other) const
Definition lalr.h:159
static Repository & repository()
Definition lalr.h:168
edge_iterator end() const
Definition lalr.h:150
static iterator get(_Tp data)
Definition lalr.h:188
bool root
Definition lalr.h:175
static iterator begin_nodes()
Definition lalr.h:162
edge_iterator begin() const
Definition lalr.h:147
std::list< iterator > outs
Definition lalr.h:178
std::pair< edge_iterator, bool > insertEdge(iterator other) const
Definition lalr.h:200
std::list< iterator >::iterator edge_iterator
Definition lalr.h:140
bool operator==(const Node< _Tp > &other) const
Definition lalr.h:153
bool operator!=(const Node< _Tp > &other) const
Definition lalr.h:156
Node()
Definition lalr.h:181
_Tp data
Definition lalr.h:177
Node(_Tp d)
Definition lalr.h:183
Repository::iterator iterator
Definition lalr.h:139
std::set< Node< _Tp > > Repository
Definition lalr.h:138
static iterator end_nodes()
Definition lalr.h:165
int dfn
Definition lalr.h:176
iterator end()
Definition qset.h:140
iterator find(const T &value)
Definition qset.h:159
iterator insert(const T &value)
Definition qset.h:155
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
\inmodule QtCore
Definition lalr.h:268
StatePointer state
Definition lalr.h:284
Read(StatePointer s, Name n)
Definition lalr.h:272
Name nt
Definition lalr.h:285
Read()
Definition lalr.h:270
Definition lalr.h:49
NameList rhs
Definition lalr.h:60
Name lhs
Definition lalr.h:59
void clear()
Definition lalr.h:51
Name prec
Definition lalr.h:61
QSet< QString >::iterator it
else opt state
[0]
QTextStream & qout()
Definition lalr.cpp:26
QT_BEGIN_NAMESPACE QTextStream & qerr()
Definition lalr.cpp:20
std::list< Item > ItemList
Definition lalr.h:33
std::set< Name > NameSet
Definition lalr.h:30
ItemList::iterator ItemPointer
Definition lalr.h:34
StateList::iterator StatePointer
Definition lalr.h:43
std::list< QString >::iterator Name
Definition lalr.h:28
QMap< Name, StatePointer > Bundle
Definition lalr.h:46
std::list< State > StateList
Definition lalr.h:42
std::list< Rule > debug_infot
Definition lalr.h:37
QMultiMap< Name, RulePointer > RuleMap
Definition lalr.h:39
debug_infot::iterator RulePointer
Definition lalr.h:38
std::list< Name > NameList
Definition lalr.h:29
Combined button and popup list for selecting options.
constexpr bool operator!=(const timespec &t1, const timespec &t2)
DBusConnection const char * rule
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLuint GLuint end
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean GLboolean g
GLuint name
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
@ Right
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
static bool operator<(const QSettingsIniKey &k1, const QSettingsIniKey &k2)
static QString dump(const QByteArray &)
QRandomGenerator64 rd
[10]
QTextStream out(stdout)
[7]
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QSharedPointer< T > other(t)
[5]
QGraphicsItem * item
bool operator!=(const State &other) const
Definition lalr.h:116
std::pair< ItemPointer, bool > insertClosure(const Item &item)
Definition lalr.cpp:152
std::pair< ItemPointer, bool > insert(const Item &item)
Definition lalr.cpp:142
RulePointer defaultReduce
Definition lalr.h:128
QMap< Name, NameSet > reads
Definition lalr.h:126
State(const char *token)
bool operator==(const State &o) const
ItemList closure
Definition lalr.h:124
Bundle bundle
Definition lalr.h:125
QMap< Name, NameSet > follows
Definition lalr.h:127
ItemList kernel
Definition lalr.h:123