Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
30typedef std::set<Name> NameSet;
31
32// items
35
36// rules
40
41// states
44
45// arrows
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:
68 state (s), nt (n) {}
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:
106};
107
108class State
109{
110public:
111 State (Grammar *grammar);
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
129};
130
131/////////////////////////////////////////////////////////////
132// digraph
133/////////////////////////////////////////////////////////////
134template <typename _Tp>
135class Node
136{
137public:
138 typedef std::set<Node<_Tp> > Repository;
139 typedef typename Repository::iterator 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;
179
180protected:
181 inline Node () {}
182
183 inline Node (_Tp d):
184 root (true), dfn (0), data (d) {}
185};
186
187template <typename _Tp>
188typename Node<_Tp>::iterator Node<_Tp>::get (_Tp data)
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
211/////////////////////////////////////////////////////////////
212// Grammar
213/////////////////////////////////////////////////////////////
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:
250
256
257 struct TokenInfo {
259 int prec;
260 };
261
265};
266
267class Read
268{
269public:
270 inline Read () {}
271
272 inline Read (StatePointer s, Name n):
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
293 inline Include (StatePointer s, Name n):
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:
312 Automaton (Grammar *g);
313
314 std::pair<StatePointer, bool> internState (const State &state);
315
318
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
333 void buildIncludesDigraph ();
334 void visitIncludeNode (IncludeNode node);
335
336 void buildLookaheads ();
337
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);
347 void dump (QTextStream &out, ReadNode rd);
348 void dump (QTextStream &out, const Lookback &lp);
349
350public: // ### private
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);
369bool operator < (ItemPointer a, ItemPointer b);
370}
371
372QTextStream &operator << (QTextStream &out, const Name &n);
373QTextStream &operator << (QTextStream &out, const Rule &r);
374QTextStream &operator << (QTextStream &out, const Item &item);
375QTextStream &operator << (QTextStream &out, const NameSet &ns);
376
378QTextStream &qerr();
380QT_END_NAMESPACE
381
382#endif // LALR_H
int main(int argc, char *argv[])
[2]
Definition buffer.cpp:77
void buildReadsDigraph()
Definition lalr.cpp:474
Automaton(Grammar *g)
Definition lalr.cpp:236
int id(RulePointer rule)
Definition lalr.cpp:242
void buildDefaultReduceActions()
Definition lalr.cpp:724
IncludesGraph::iterator IncludeNode
Definition lalr.h:320
void buildNullables()
Definition lalr.cpp:277
StatePointer start
Definition lalr.h:353
void build()
Definition lalr.cpp:257
void visitReadNode(ReadNode node)
Definition lalr.cpp:528
void buildReads()
Definition lalr.cpp:509
ReadsGraph::iterator ReadNode
Definition lalr.h:317
void dump(QTextStream &out, IncludeNode incl)
Definition lalr.cpp:752
void buildIncludesDigraph()
Definition lalr.cpp:586
std::pair< StatePointer, bool > internState(const State &state)
Definition lalr.cpp:299
NameSet nullables
Definition lalr.h:354
void buildIncludesAndFollows()
Definition lalr.cpp:565
Grammar * _M_grammar
Definition lalr.h:351
void buildLookaheads()
Definition lalr.cpp:693
void buildDirectReads()
Definition lalr.cpp:445
StateList states
Definition lalr.h:352
Node< Include > IncludesGraph
Definition lalr.h:319
void buildLookbackSets()
Definition lalr.cpp:391
void visitIncludeNode(IncludeNode node)
Definition lalr.cpp:647
void dump(QTextStream &out, const Lookback &lp)
Definition lalr.cpp:762
Node< Read > ReadsGraph
Definition lalr.h:316
QMultiMap< ItemPointer, Lookback > lookbacks
Definition lalr.h:355
void closure(StatePointer state)
Definition lalr.cpp:327
QMap< ItemPointer, NameSet > lookaheads
Definition lalr.h:356
CppGenerator(const Recognizer &p, Grammar &grammar, Automaton &aut, bool verbose)
void setWarningsAreErrors(bool e)
void setDebugInfo(bool d)
void setCopyright(bool t)
void operator()(Automaton *a)
Definition dotgraph.cpp:15
QMap< Name, QString > spells
Definition lalr.h:241
Assoc
Definition lalr.h:251
@ Left
Definition lalr.h:253
@ Right
Definition lalr.h:254
@ 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
void buildRuleMap()
Definition lalr.cpp:193
Grammar()
Definition lalr.cpp:166
RulePointer goal
Definition lalr.h:244
RuleMap rule_map
Definition lalr.h:243
QString decl_file_name
Definition lalr.h:234
void buildExtendedGrammar()
Definition lalr.cpp:213
Name intern(const QString &id)
Definition lalr.cpp:183
NameSet non_terminals
Definition lalr.h:240
int expected_shift_reduce
Definition lalr.h:248
bool operator<(const Include &other) const
Definition lalr.cpp:58
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
bool operator==(const Include &other) const
Definition lalr.h:296
bool operator!=(const Include &other) const
Definition lalr.h:299
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
The Node class is the base class for all the nodes in QDoc's parse tree.
Definition lalr.h:136
std::set< Node< _Tp > > Repository
Definition lalr.h:138
bool operator<(const Node< _Tp > &other) const
Definition lalr.h:159
std::list< iterator >::iterator edge_iterator
Definition lalr.h:140
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
Returns a pointer to the root of the Tree this node is in.
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
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
static iterator end_nodes()
Definition lalr.h:165
int dfn
Definition lalr.h:176
void operator()(Automaton *a)
Definition lalr.h:268
bool operator!=(const Read &other) const
Definition lalr.h:278
bool operator<(const Read &other) const
Definition lalr.cpp:50
StatePointer state
Definition lalr.h:284
Read(StatePointer s, Name n)
Definition lalr.h:272
bool operator==(const Read &other) const
Definition lalr.h:275
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
QT_FORWARD_DECLARE_CLASS(QTextStream)
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
QTextStream & qout()
Definition qev.cpp:12
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
QTextStream & operator<<(QTextStream &out, const Rule &r)
Definition lalr.cpp:79
debug_infot::iterator RulePointer
Definition lalr.h:38
std::list< Name > NameList
Definition lalr.h:29
QTextStream & operator<<(QTextStream &out, const Item &item)
Definition lalr.cpp:116
bool operator<(Name a, Name b)
Definition lalr.cpp:34
static void help_me()
Definition main.cpp:22
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
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
bool operator==(const State &o) const
State(Grammar *grammar)
Definition lalr.cpp:137
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