author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the utils of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtCore/QtCore> |
|
44 |
||
45 |
#include "cppgenerator.h" |
|
46 |
#include "lalr.h" |
|
47 |
#include "recognizer.h" |
|
48 |
||
49 |
QString CppGenerator::copyrightHeader() const |
|
50 |
{ |
|
51 |
return QLatin1String( |
|
52 |
"/****************************************************************************\n" |
|
53 |
"**\n" |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
54 |
"** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).\n" |
0 | 55 |
"** All rights reserved.\n" |
56 |
"** Contact: Nokia Corporation (qt-info@nokia.com)\n" |
|
57 |
"**\n" |
|
58 |
"** This file is part of the QtCore module of the Qt Toolkit.\n" |
|
59 |
"**\n" |
|
60 |
"** $QT_BEGIN_LICENSE:LGPL$\n" |
|
61 |
"** No Commercial Usage\n" |
|
62 |
"** This file contains pre-release code and may not be distributed.\n" |
|
63 |
"** You may use this file in accordance with the terms and conditions\n" |
|
64 |
"** contained in the Technology Preview License Agreement accompanying\n" |
|
65 |
"** this package.\n" |
|
66 |
"**\n" |
|
67 |
"** GNU Lesser General Public License Usage\n" |
|
68 |
"** Alternatively, this file may be used under the terms of the GNU Lesser\n" |
|
69 |
"** General Public License version 2.1 as published by the Free Software\n" |
|
70 |
"** Foundation and appearing in the file LICENSE.LGPL included in the\n" |
|
71 |
"** packaging of this file. Please review the following information to\n" |
|
72 |
"** ensure the GNU Lesser General Public License version 2.1 requirements\n" |
|
73 |
"** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.\n" |
|
74 |
"**\n" |
|
75 |
"** In addition, as a special exception, Nokia gives you certain additional\n" |
|
76 |
"** rights. These rights are described in the Nokia Qt LGPL Exception\n" |
|
77 |
"** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.\n" |
|
78 |
"**\n" |
|
79 |
"** If you have questions regarding the use of this file, please contact\n" |
|
80 |
"** Nokia at qt-info@nokia.com.\n" |
|
81 |
"**\n" |
|
82 |
"**\n" |
|
83 |
"**\n" |
|
84 |
"**\n" |
|
85 |
"**\n" |
|
86 |
"**\n" |
|
87 |
"**\n" |
|
88 |
"**\n" |
|
89 |
"** $QT_END_LICENSE$\n" |
|
90 |
"**\n" |
|
91 |
"****************************************************************************/\n" |
|
92 |
"\n"); |
|
93 |
} |
|
94 |
||
95 |
QString CppGenerator::privateCopyrightHeader() const |
|
96 |
{ |
|
97 |
return QLatin1String( |
|
98 |
"//\n" |
|
99 |
"// W A R N I N G\n" |
|
100 |
"// -------------\n" |
|
101 |
"//\n" |
|
102 |
"// This file is not part of the Qt API. It exists for the convenience\n" |
|
103 |
"// of other Qt classes. This header file may change from version to\n" |
|
104 |
"// version without notice, or even be removed.\n" |
|
105 |
"//\n" |
|
106 |
"// We mean it.\n" |
|
107 |
"//\n"); |
|
108 |
} |
|
109 |
||
110 |
QString CppGenerator::startIncludeGuard(const QString &fileName) |
|
111 |
{ |
|
112 |
const QString normalized(QString(fileName).replace(QLatin1Char('.'), QLatin1Char('_')).toUpper()); |
|
113 |
||
114 |
return QString::fromLatin1("#ifndef %1\n" |
|
115 |
"#define %2\n").arg(normalized, normalized); |
|
116 |
} |
|
117 |
||
118 |
QString CppGenerator::endIncludeGuard(const QString &fileName) |
|
119 |
{ |
|
120 |
const QString normalized(QString(fileName).replace(QLatin1Char('.'), QLatin1Char('_')).toUpper()); |
|
121 |
||
122 |
return QString::fromLatin1("#endif // %1\n").arg(normalized); |
|
123 |
} |
|
124 |
||
125 |
void CppGenerator::operator () () |
|
126 |
{ |
|
127 |
// action table... |
|
128 |
state_count = aut.states.size (); |
|
129 |
terminal_count = grammar.terminals.size (); |
|
130 |
non_terminal_count = grammar.non_terminals.size (); |
|
131 |
||
132 |
#define ACTION(i, j) table [(i) * terminal_count + (j)] |
|
133 |
#define GOTO(i, j) pgoto [(i) * non_terminal_count + (j)] |
|
134 |
||
135 |
int *table = new int [state_count * terminal_count]; |
|
136 |
::memset (table, 0, state_count * terminal_count * sizeof (int)); |
|
137 |
||
138 |
int *pgoto = new int [state_count * non_terminal_count]; |
|
139 |
::memset (pgoto, 0, state_count * non_terminal_count * sizeof (int)); |
|
140 |
||
141 |
accept_state = -1; |
|
142 |
int shift_reduce_conflict_count = 0; |
|
143 |
int reduce_reduce_conflict_count = 0; |
|
144 |
||
145 |
for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state) |
|
146 |
{ |
|
147 |
int q = aut.id (state); |
|
148 |
||
149 |
for (Bundle::iterator a = state->bundle.begin (); a != state->bundle.end (); ++a) |
|
150 |
{ |
|
151 |
int symbol = aut.id (a.key ()); |
|
152 |
int r = aut.id (a.value ()); |
|
153 |
||
154 |
Q_ASSERT (r < state_count); |
|
155 |
||
156 |
if (grammar.isNonTerminal (a.key ())) |
|
157 |
{ |
|
158 |
Q_ASSERT (symbol >= terminal_count && symbol < grammar.names.size ()); |
|
159 |
GOTO (q, symbol - terminal_count) = r; |
|
160 |
} |
|
161 |
||
162 |
else |
|
163 |
ACTION (q, symbol) = r; |
|
164 |
} |
|
165 |
||
166 |
for (ItemPointer item = state->closure.begin (); item != state->closure.end (); ++item) |
|
167 |
{ |
|
168 |
if (item->dot != item->end_rhs ()) |
|
169 |
continue; |
|
170 |
||
171 |
int r = aut.id (item->rule); |
|
172 |
||
173 |
NameSet lookaheads = aut.lookaheads.value (item); |
|
174 |
||
175 |
if (item->rule == grammar.goal) |
|
176 |
accept_state = q; |
|
177 |
||
178 |
foreach (Name s, lookaheads) |
|
179 |
{ |
|
180 |
int &u = ACTION (q, aut.id (s)); |
|
181 |
||
182 |
if (u == 0) |
|
183 |
u = - r; |
|
184 |
||
185 |
else if (u < 0) |
|
186 |
{ |
|
187 |
if (verbose) |
|
188 |
qout << "*** Warning. Found a reduce/reduce conflict in state " << q << " on token ``" << s << "'' between rule " |
|
189 |
<< r << " and " << -u << endl; |
|
190 |
||
191 |
++reduce_reduce_conflict_count; |
|
192 |
||
193 |
u = qMax (u, -r); |
|
194 |
||
195 |
if (verbose) |
|
196 |
qout << "\tresolved using rule " << -u << endl; |
|
197 |
} |
|
198 |
||
199 |
else if (u > 0) |
|
200 |
{ |
|
201 |
if (item->rule->prec != grammar.names.end() && grammar.token_info.contains (s)) |
|
202 |
{ |
|
203 |
Grammar::TokenInfo info_r = grammar.token_info.value (item->rule->prec); |
|
204 |
Grammar::TokenInfo info_s = grammar.token_info.value (s); |
|
205 |
||
206 |
if (info_r.prec > info_s.prec) |
|
207 |
u = -r; |
|
208 |
else if (info_r.prec == info_s.prec) |
|
209 |
{ |
|
210 |
switch (info_r.assoc) { |
|
211 |
case Grammar::Left: |
|
212 |
u = -r; |
|
213 |
break; |
|
214 |
case Grammar::Right: |
|
215 |
// shift... nothing to do |
|
216 |
break; |
|
217 |
case Grammar::NonAssoc: |
|
218 |
u = 0; |
|
219 |
break; |
|
220 |
} // switch |
|
221 |
} |
|
222 |
} |
|
223 |
||
224 |
else |
|
225 |
{ |
|
226 |
++shift_reduce_conflict_count; |
|
227 |
||
228 |
if (verbose) |
|
229 |
qout << "*** Warning. Found a shift/reduce conflict in state " << q << " on token ``" << s << "'' with rule " << r << endl; |
|
230 |
} |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
||
236 |
if (shift_reduce_conflict_count || reduce_reduce_conflict_count) |
|
237 |
{ |
|
238 |
if (shift_reduce_conflict_count != grammar.expected_shift_reduce |
|
239 |
|| reduce_reduce_conflict_count != grammar.expected_reduce_reduce) |
|
240 |
qerr << "*** Conflicts: " << shift_reduce_conflict_count << " shift/reduce, " << reduce_reduce_conflict_count << " reduce/reduce" << endl; |
|
241 |
||
242 |
if (verbose) |
|
243 |
qout << endl << "*** Conflicts: " << shift_reduce_conflict_count << " shift/reduce, " << reduce_reduce_conflict_count << " reduce/reduce" << endl |
|
244 |
<< endl; |
|
245 |
} |
|
246 |
||
247 |
QBitArray used_rules (grammar.rules.count ()); |
|
248 |
||
249 |
int q = 0; |
|
250 |
for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state, ++q) |
|
251 |
{ |
|
252 |
for (int j = 0; j < terminal_count; ++j) |
|
253 |
{ |
|
254 |
int &u = ACTION (q, j); |
|
255 |
||
256 |
if (u < 0) |
|
257 |
used_rules.setBit (-u - 1); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
for (int i = 0; i < used_rules.count (); ++i) |
|
262 |
{ |
|
263 |
if (! used_rules.testBit (i)) |
|
264 |
{ |
|
265 |
RulePointer rule = grammar.rules.begin () + i; |
|
266 |
||
267 |
if (rule != grammar.goal) |
|
268 |
qerr << "*** Warning: Rule ``" << *rule << "'' is useless!" << endl; |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
q = 0; |
|
273 |
for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state, ++q) |
|
274 |
{ |
|
275 |
for (int j = 0; j < terminal_count; ++j) |
|
276 |
{ |
|
277 |
int &u = ACTION (q, j); |
|
278 |
||
279 |
if (u >= 0) |
|
280 |
continue; |
|
281 |
||
282 |
RulePointer rule = grammar.rules.begin () + (- u - 1); |
|
283 |
||
284 |
if (state->defaultReduce == rule) |
|
285 |
u = 0; |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
// ... compress the goto table |
|
290 |
defgoto.resize (non_terminal_count); |
|
291 |
for (int j = 0; j < non_terminal_count; ++j) |
|
292 |
{ |
|
293 |
count.fill (0, state_count); |
|
294 |
||
295 |
int &mx = defgoto [j]; |
|
296 |
||
297 |
for (int i = 0; i < state_count; ++i) |
|
298 |
{ |
|
299 |
int r = GOTO (i, j); |
|
300 |
||
301 |
if (! r) |
|
302 |
continue; |
|
303 |
||
304 |
++count [r]; |
|
305 |
||
306 |
if (count [r] > count [mx]) |
|
307 |
mx = r; |
|
308 |
} |
|
309 |
} |
|
310 |
||
311 |
for (int i = 0; i < state_count; ++i) |
|
312 |
{ |
|
313 |
for (int j = 0; j < non_terminal_count; ++j) |
|
314 |
{ |
|
315 |
int &r = GOTO (i, j); |
|
316 |
||
317 |
if (r == defgoto [j]) |
|
318 |
r = 0; |
|
319 |
} |
|
320 |
} |
|
321 |
||
322 |
compressed_action (table, state_count, terminal_count); |
|
323 |
compressed_goto (pgoto, state_count, non_terminal_count); |
|
324 |
||
325 |
delete[] table; |
|
326 |
table = 0; |
|
327 |
||
328 |
delete[] pgoto; |
|
329 |
pgoto = 0; |
|
330 |
||
331 |
#undef ACTION |
|
332 |
#undef GOTO |
|
333 |
||
334 |
if (! grammar.merged_output.isEmpty()) |
|
335 |
{ |
|
336 |
QFile f(grammar.merged_output); |
|
337 |
if (! f.open (QFile::WriteOnly)) |
|
338 |
{ |
|
339 |
fprintf (stderr, "*** cannot create %s\n", qPrintable(grammar.merged_output)); |
|
340 |
return; |
|
341 |
} |
|
342 |
||
343 |
QTextStream out (&f); |
|
344 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
345 |
// copyright headers must come first, otherwise the headers tests will fail |
0 | 346 |
if (copyright) |
347 |
{ |
|
348 |
out << copyrightHeader() |
|
349 |
<< privateCopyrightHeader() |
|
350 |
<< endl; |
|
351 |
} |
|
352 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
353 |
out << "// This file was generated by qlalr - DO NOT EDIT!\n"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
354 |
|
0 | 355 |
out << startIncludeGuard(grammar.merged_output) << endl; |
356 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
357 |
if (copyright) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
358 |
out << "#if defined(Q_OS_VXWORKS) && defined(ERROR)" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
359 |
<< "# undef ERROR" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
360 |
<< "#endif" << endl << endl; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
361 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
362 |
|
0 | 363 |
generateDecl (out); |
364 |
generateImpl (out); |
|
365 |
out << p.decls(); |
|
366 |
out << p.impls(); |
|
367 |
out << endl; |
|
368 |
||
369 |
out << endIncludeGuard(grammar.merged_output) << endl; |
|
370 |
||
371 |
return; |
|
372 |
} |
|
373 |
||
374 |
// default behaviour |
|
375 |
QString declFileName = grammar.table_name.toLower () + QLatin1String("_p.h"); |
|
376 |
QString bitsFileName = grammar.table_name.toLower () + QLatin1String(".cpp"); |
|
377 |
||
378 |
{ // decls... |
|
379 |
QFile f (declFileName); |
|
380 |
f.open (QFile::WriteOnly); |
|
381 |
QTextStream out (&f); |
|
382 |
||
383 |
QString prot = declFileName.toUpper ().replace (QLatin1Char ('.'), QLatin1Char ('_')); |
|
384 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
385 |
// copyright headers must come first, otherwise the headers tests will fail |
0 | 386 |
if (copyright) |
387 |
{ |
|
388 |
out << copyrightHeader() |
|
389 |
<< privateCopyrightHeader() |
|
390 |
<< endl; |
|
391 |
} |
|
392 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
393 |
out << "// This file was generated by qlalr - DO NOT EDIT!\n"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
|
0 | 395 |
out << "#ifndef " << prot << endl |
396 |
<< "#define " << prot << endl |
|
397 |
<< endl; |
|
398 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
399 |
if (copyright) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
400 |
out << "#include <QtCore/qglobal.h>" << endl << endl; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
401 |
out << "QT_BEGIN_NAMESPACE" << endl << endl; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
402 |
} |
0 | 403 |
generateDecl (out); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
404 |
if (copyright) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
out << "QT_END_NAMESPACE" << endl; |
0 | 406 |
|
407 |
out << "#endif // " << prot << endl << endl; |
|
408 |
} // end decls |
|
409 |
||
410 |
{ // bits... |
|
411 |
QFile f (bitsFileName); |
|
412 |
f.open (QFile::WriteOnly); |
|
413 |
QTextStream out (&f); |
|
414 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
415 |
// copyright headers must come first, otherwise the headers tests will fail |
0 | 416 |
if (copyright) |
417 |
out << copyrightHeader(); |
|
418 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
419 |
out << "// This file was generated by qlalr - DO NOT EDIT!\n"; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
420 |
|
0 | 421 |
out << "#include \"" << declFileName << "\"" << endl << endl; |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
422 |
if (copyright) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
423 |
out << "QT_BEGIN_NAMESPACE" << endl << endl; |
0 | 424 |
generateImpl(out); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
425 |
if (copyright) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
426 |
out << "QT_END_NAMESPACE" << endl; |
0 | 427 |
|
428 |
} // end bits |
|
429 |
||
430 |
if (! grammar.decl_file_name.isEmpty ()) |
|
431 |
{ |
|
432 |
QFile f (grammar.decl_file_name); |
|
433 |
f.open (QFile::WriteOnly); |
|
434 |
QTextStream out (&f); |
|
435 |
out << p.decls(); |
|
436 |
} |
|
437 |
||
438 |
if (! grammar.impl_file_name.isEmpty ()) |
|
439 |
{ |
|
440 |
QFile f (grammar.impl_file_name); |
|
441 |
f.open (QFile::WriteOnly); |
|
442 |
QTextStream out (&f); |
|
443 |
out << p.impls(); |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
QString CppGenerator::debugInfoProt() const |
|
448 |
{ |
|
449 |
QString prot = QLatin1String("QLALR_NO_"); |
|
450 |
prot += grammar.table_name.toUpper(); |
|
451 |
prot += QLatin1String("_DEBUG_INFO"); |
|
452 |
return prot; |
|
453 |
} |
|
454 |
||
455 |
void CppGenerator::generateDecl (QTextStream &out) |
|
456 |
{ |
|
457 |
out << "class " << grammar.table_name << endl |
|
458 |
<< "{" << endl |
|
459 |
<< "public:" << endl |
|
460 |
<< " enum {" << endl; |
|
461 |
||
462 |
foreach (Name t, grammar.terminals) |
|
463 |
{ |
|
464 |
QString name = *t; |
|
465 |
int value = std::distance (grammar.names.begin (), t); |
|
466 |
||
467 |
if (name == QLatin1String ("$end")) |
|
468 |
name = QLatin1String ("EOF_SYMBOL"); |
|
469 |
||
470 |
else if (name == QLatin1String ("$accept")) |
|
471 |
name = QLatin1String ("ACCEPT_SYMBOL"); |
|
472 |
||
473 |
else |
|
474 |
name.prepend (grammar.token_prefix); |
|
475 |
||
476 |
out << " " << name << " = " << value << "," << endl; |
|
477 |
} |
|
478 |
||
479 |
out << endl |
|
480 |
<< " ACCEPT_STATE = " << accept_state << "," << endl |
|
481 |
<< " RULE_COUNT = " << grammar.rules.size () << "," << endl |
|
482 |
<< " STATE_COUNT = " << state_count << "," << endl |
|
483 |
<< " TERMINAL_COUNT = " << terminal_count << "," << endl |
|
484 |
<< " NON_TERMINAL_COUNT = " << non_terminal_count << "," << endl |
|
485 |
<< endl |
|
486 |
<< " GOTO_INDEX_OFFSET = " << compressed_action.index.size () << "," << endl |
|
487 |
<< " GOTO_INFO_OFFSET = " << compressed_action.info.size () << "," << endl |
|
488 |
<< " GOTO_CHECK_OFFSET = " << compressed_action.check.size () << endl |
|
489 |
<< " };" << endl |
|
490 |
<< endl |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
491 |
<< " static const char *const spell [];" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
492 |
<< " static const short lhs [];" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
493 |
<< " static const short rhs [];" << endl; |
0 | 494 |
|
495 |
if (debug_info) |
|
496 |
{ |
|
497 |
QString prot = debugInfoProt(); |
|
498 |
||
499 |
out << endl << "#ifndef " << prot << endl |
|
500 |
<< " static const int rule_index [];" << endl |
|
501 |
<< " static const int rule_info [];" << endl |
|
502 |
<< "#endif // " << prot << endl << endl; |
|
503 |
} |
|
504 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
505 |
out << " static const short goto_default [];" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
506 |
<< " static const short action_default [];" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
<< " static const short action_index [];" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
508 |
<< " static const short action_info [];" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
509 |
<< " static const short action_check [];" << endl |
0 | 510 |
<< endl |
511 |
<< " static inline int nt_action (int state, int nt)" << endl |
|
512 |
<< " {" << endl |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
513 |
<< " const int yyn = action_index [GOTO_INDEX_OFFSET + state] + nt;" << endl |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
514 |
<< " if (yyn < 0 || action_check [GOTO_CHECK_OFFSET + yyn] != nt)" << endl |
0 | 515 |
<< " return goto_default [nt];" << endl |
516 |
<< endl |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
517 |
<< " return action_info [GOTO_INFO_OFFSET + yyn];" << endl |
0 | 518 |
<< " }" << endl |
519 |
<< endl |
|
520 |
<< " static inline int t_action (int state, int token)" << endl |
|
521 |
<< " {" << endl |
|
522 |
<< " const int yyn = action_index [state] + token;" << endl |
|
523 |
<< endl |
|
524 |
<< " if (yyn < 0 || action_check [yyn] != token)" << endl |
|
525 |
<< " return - action_default [state];" << endl |
|
526 |
<< endl |
|
527 |
<< " return action_info [yyn];" << endl |
|
528 |
<< " }" << endl |
|
529 |
<< "};" << endl |
|
530 |
<< endl |
|
531 |
<< endl; |
|
532 |
} |
|
533 |
||
534 |
void CppGenerator::generateImpl (QTextStream &out) |
|
535 |
{ |
|
536 |
int idx = 0; |
|
537 |
||
538 |
out << "const char *const " << grammar.table_name << "::spell [] = {"; |
|
539 |
idx = 0; |
|
540 |
||
541 |
QMap<Name, int> name_ids; |
|
542 |
bool first_nt = true; |
|
543 |
||
544 |
for (Name t = grammar.names.begin (); t != grammar.names.end (); ++t, ++idx) |
|
545 |
{ |
|
546 |
bool terminal = grammar.isTerminal (t); |
|
547 |
||
548 |
if (! (debug_info || terminal)) |
|
549 |
break; |
|
550 |
||
551 |
name_ids.insert (t, idx); |
|
552 |
||
553 |
if (idx) |
|
554 |
out << ", "; |
|
555 |
||
556 |
if (! (idx % 10)) |
|
557 |
out << endl << " "; |
|
558 |
||
559 |
if (terminal) |
|
560 |
{ |
|
561 |
QString spell = grammar.spells.value (t); |
|
562 |
||
563 |
if (spell.isEmpty ()) |
|
564 |
out << "0"; |
|
565 |
else |
|
566 |
out << "\"" << spell << "\""; |
|
567 |
} |
|
568 |
else |
|
569 |
{ |
|
570 |
if (first_nt) |
|
571 |
{ |
|
572 |
first_nt = false; |
|
573 |
QString prot = debugInfoProt(); |
|
574 |
out << endl << "#ifndef " << prot << endl; |
|
575 |
} |
|
576 |
out << "\"" << *t << "\""; |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
if (debug_info) |
|
581 |
out << endl << "#endif // " << debugInfoProt() << endl; |
|
582 |
||
583 |
out << "};" << endl << endl; |
|
584 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
585 |
out << "const short " << grammar.table_name << "::lhs [] = {"; |
0 | 586 |
idx = 0; |
587 |
for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx) |
|
588 |
{ |
|
589 |
if (idx) |
|
590 |
out << ", "; |
|
591 |
||
592 |
if (! (idx % 10)) |
|
593 |
out << endl << " "; |
|
594 |
||
595 |
out << aut.id (rule->lhs); |
|
596 |
} |
|
597 |
out << "};" << endl << endl; |
|
598 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
599 |
out << "const short " << grammar.table_name << "::rhs [] = {"; |
0 | 600 |
idx = 0; |
601 |
for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx) |
|
602 |
{ |
|
603 |
if (idx) |
|
604 |
out << ", "; |
|
605 |
||
606 |
if (! (idx % 10)) |
|
607 |
out << endl << " "; |
|
608 |
||
609 |
out << rule->rhs.size (); |
|
610 |
} |
|
611 |
out << "};" << endl << endl; |
|
612 |
||
613 |
if (debug_info) |
|
614 |
{ |
|
615 |
QString prot = debugInfoProt(); |
|
616 |
||
617 |
out << endl << "#ifndef " << prot << endl; |
|
618 |
out << "const int " << grammar.table_name << "::rule_info [] = {"; |
|
619 |
idx = 0; |
|
620 |
for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx) |
|
621 |
{ |
|
622 |
out << endl << " "; |
|
623 |
||
624 |
if (idx) |
|
625 |
out << ", "; |
|
626 |
else |
|
627 |
out << " "; |
|
628 |
||
629 |
out << name_ids.value(rule->lhs); |
|
630 |
||
631 |
foreach (Name n, rule->rhs) |
|
632 |
out << ", " << name_ids.value (n); |
|
633 |
} |
|
634 |
out << "};" << endl << endl; |
|
635 |
||
636 |
out << "const int " << grammar.table_name << "::rule_index [] = {"; |
|
637 |
idx = 0; |
|
638 |
int offset = 0; |
|
639 |
for (RulePointer rule = grammar.rules.begin (); rule != grammar.rules.end (); ++rule, ++idx) |
|
640 |
{ |
|
641 |
if (idx) |
|
642 |
out << ", "; |
|
643 |
||
644 |
if (! (idx % 10)) |
|
645 |
out << endl << " "; |
|
646 |
||
647 |
out << offset; |
|
648 |
offset += rule->rhs.size () + 1; |
|
649 |
} |
|
650 |
out << "};" << endl |
|
651 |
<< "#endif // " << prot << endl << endl; |
|
652 |
} |
|
653 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
654 |
out << "const short " << grammar.table_name << "::action_default [] = {"; |
0 | 655 |
idx = 0; |
656 |
for (StatePointer state = aut.states.begin (); state != aut.states.end (); ++state, ++idx) |
|
657 |
{ |
|
658 |
if (state != aut.states.begin ()) |
|
659 |
out << ", "; |
|
660 |
||
661 |
if (! (idx % 10)) |
|
662 |
out << endl << " "; |
|
663 |
||
664 |
if (state->defaultReduce != grammar.rules.end ()) |
|
665 |
out << aut.id (state->defaultReduce); |
|
666 |
else |
|
667 |
out << "0"; |
|
668 |
} |
|
669 |
out << "};" << endl << endl; |
|
670 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
671 |
out << "const short " << grammar.table_name << "::goto_default [] = {"; |
0 | 672 |
for (int i = 0; i < defgoto.size (); ++i) |
673 |
{ |
|
674 |
if (i) |
|
675 |
out << ", "; |
|
676 |
||
677 |
if (! (i % 10)) |
|
678 |
out << endl << " "; |
|
679 |
||
680 |
out << defgoto [i]; |
|
681 |
} |
|
682 |
out << "};" << endl << endl; |
|
683 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
684 |
out << "const short " << grammar.table_name << "::action_index [] = {"; |
0 | 685 |
for (int i = 0; i < compressed_action.index.size (); ++i) |
686 |
{ |
|
687 |
if (! (i % 10)) |
|
688 |
out << endl << " "; |
|
689 |
||
690 |
out << compressed_action.index [i] << ", "; |
|
691 |
} |
|
692 |
out << endl; |
|
693 |
for (int i = 0; i < compressed_goto.index.size (); ++i) |
|
694 |
{ |
|
695 |
if (i) |
|
696 |
out << ", "; |
|
697 |
||
698 |
if (! (i % 10)) |
|
699 |
out << endl << " "; |
|
700 |
||
701 |
out << compressed_goto.index [i]; |
|
702 |
} |
|
703 |
out << "};" << endl << endl; |
|
704 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
705 |
out << "const short " << grammar.table_name << "::action_info [] = {"; |
0 | 706 |
for (int i = 0; i < compressed_action.info.size (); ++i) |
707 |
{ |
|
708 |
if (! (i % 10)) |
|
709 |
out << endl << " "; |
|
710 |
||
711 |
out << compressed_action.info [i] << ", "; |
|
712 |
} |
|
713 |
out << endl; |
|
714 |
for (int i = 0; i < compressed_goto.info.size (); ++i) |
|
715 |
{ |
|
716 |
if (i) |
|
717 |
out << ", "; |
|
718 |
||
719 |
if (! (i % 10)) |
|
720 |
out << endl << " "; |
|
721 |
||
722 |
out << compressed_goto.info [i]; |
|
723 |
} |
|
724 |
out << "};" << endl << endl; |
|
725 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
726 |
out << "const short " << grammar.table_name << "::action_check [] = {"; |
0 | 727 |
for (int i = 0; i < compressed_action.check.size (); ++i) |
728 |
{ |
|
729 |
if (! (i % 10)) |
|
730 |
out << endl << " "; |
|
731 |
||
732 |
out << compressed_action.check [i] << ", "; |
|
733 |
} |
|
734 |
out << endl; |
|
735 |
for (int i = 0; i < compressed_goto.check.size (); ++i) |
|
736 |
{ |
|
737 |
if (i) |
|
738 |
out << ", "; |
|
739 |
||
740 |
if (! (i % 10)) |
|
741 |
out << endl << " "; |
|
742 |
||
743 |
out << compressed_goto.check [i]; |
|
744 |
} |
|
745 |
out << "};" << endl << endl; |
|
746 |
} |