author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 |
#include <QtCore/QCoreApplication> |
|
43 |
#include <QtCore/QFile> |
|
44 |
#include <QtCore/QStringList> |
|
45 |
#include <QtCore/QtDebug> |
|
46 |
||
47 |
#include <cstdlib> |
|
48 |
||
49 |
#include "lalr.h" |
|
50 |
#include "dotgraph.h" |
|
51 |
#include "parsetable.h" |
|
52 |
#include "cppgenerator.h" |
|
53 |
#include "recognizer.h" |
|
54 |
||
55 |
#define QLALR_NO_DEBUG_TABLE |
|
56 |
#define QLALR_NO_DEBUG_DOT |
|
57 |
||
58 |
static void help_me () |
|
59 |
{ |
|
60 |
qerr << "Usage: qlalr [options] [input file name]" << endl |
|
61 |
<< endl |
|
62 |
<< " --help, -h\t\tdisplay this help and exit" << endl |
|
63 |
<< " --verbose, -v\t\tverbose output" << endl |
|
64 |
<< " --no-debug\t\tno debug information" << endl |
|
65 |
<< " --no-lines\t\tno #line directives" << endl |
|
66 |
<< " --dot\t\t\tgenerate a graph" << endl |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
<< " --qt\t\tadd the Qt copyright header and Qt-specific types and macros" << endl |
0 | 68 |
<< endl; |
69 |
exit (0); |
|
70 |
} |
|
71 |
||
72 |
int main (int argc, char *argv[]) |
|
73 |
{ |
|
74 |
QCoreApplication app (argc, argv); |
|
75 |
||
76 |
bool generate_dot = false; |
|
77 |
bool generate_report = false; |
|
78 |
bool no_lines = false; |
|
79 |
bool debug_info = true; |
|
80 |
bool troll_copyright = false; |
|
81 |
QString file_name = 0; |
|
82 |
||
83 |
QStringList args = app.arguments (); |
|
84 |
args.removeFirst (); |
|
85 |
||
86 |
foreach (QString arg, args) |
|
87 |
{ |
|
88 |
if (arg == QLatin1String ("-h") || arg == QLatin1String ("--help")) |
|
89 |
help_me (); |
|
90 |
||
91 |
else if (arg == QLatin1String ("-v") || arg == QLatin1String ("--verbose")) |
|
92 |
generate_report = true; |
|
93 |
||
94 |
else if (arg == QLatin1String ("--dot")) |
|
95 |
generate_dot = true; |
|
96 |
||
97 |
else if (arg == QLatin1String ("--no-lines")) |
|
98 |
no_lines = true; |
|
99 |
||
100 |
else if (arg == QLatin1String ("--no-debug")) |
|
101 |
debug_info = false; |
|
102 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
103 |
else if (arg == QLatin1String ("--qt")) |
0 | 104 |
troll_copyright = true; |
105 |
||
106 |
else if (file_name.isEmpty ()) |
|
107 |
file_name = arg; |
|
108 |
||
109 |
else |
|
110 |
qerr << "*** Warning. Ignore argument `" << arg << "'" << endl; |
|
111 |
} |
|
112 |
||
113 |
if (file_name.isEmpty ()) |
|
114 |
{ |
|
115 |
help_me (); |
|
116 |
exit (EXIT_SUCCESS); |
|
117 |
} |
|
118 |
||
119 |
Grammar grammar; |
|
120 |
Recognizer p (&grammar, no_lines); |
|
121 |
||
122 |
if (! p.parse (file_name)) |
|
123 |
exit (EXIT_FAILURE); |
|
124 |
||
125 |
if (grammar.rules.isEmpty ()) |
|
126 |
{ |
|
127 |
qerr << "*** Fatal. No rules!" << endl; |
|
128 |
exit (EXIT_FAILURE); |
|
129 |
} |
|
130 |
||
131 |
else if (grammar.start == grammar.names.end ()) |
|
132 |
{ |
|
133 |
qerr << "*** Fatal. No start symbol!" << endl; |
|
134 |
exit (EXIT_FAILURE); |
|
135 |
} |
|
136 |
||
137 |
grammar.buildExtendedGrammar (); |
|
138 |
grammar.buildRuleMap (); |
|
139 |
||
140 |
Automaton aut (&grammar); |
|
141 |
aut.build (); |
|
142 |
||
143 |
CppGenerator gen (p, grammar, aut, generate_report); |
|
144 |
gen.setDebugInfo (debug_info); |
|
145 |
gen.setCopyright (troll_copyright); |
|
146 |
gen (); |
|
147 |
||
148 |
if (generate_dot) |
|
149 |
{ |
|
150 |
DotGraph genDotFile (qout); |
|
151 |
genDotFile (&aut); |
|
152 |
} |
|
153 |
||
154 |
else if (generate_report) |
|
155 |
{ |
|
156 |
ParseTable genParseTable (qout); |
|
157 |
genParseTable(&aut); |
|
158 |
} |
|
159 |
||
160 |
return EXIT_SUCCESS; |
|
161 |
} |
|
162 |
||
163 |
QString Recognizer::expand (const QString &text) const |
|
164 |
{ |
|
165 |
QString code = text; |
|
166 |
||
167 |
if (_M_grammar->start != _M_grammar->names.end ()) |
|
168 |
{ |
|
169 |
code = code.replace (QLatin1String("$start_id"), QString::number (std::distance (_M_grammar->names.begin (), _M_grammar->start))); |
|
170 |
code = code.replace (QLatin1String("$start"), *_M_grammar->start); |
|
171 |
} |
|
172 |
||
173 |
code = code.replace (QLatin1String("$header"), _M_grammar->table_name.toLower () + QLatin1String("_p.h")); |
|
174 |
||
175 |
code = code.replace (QLatin1String("$table"), _M_grammar->table_name); |
|
176 |
code = code.replace (QLatin1String("$parser"), _M_grammar->table_name); |
|
177 |
||
178 |
if (_M_current_rule != _M_grammar->rules.end ()) |
|
179 |
{ |
|
180 |
code = code.replace (QLatin1String("$rule_number"), QString::number (std::distance (_M_grammar->rules.begin (), _M_current_rule))); |
|
181 |
code = code.replace (QLatin1String("$rule"), *_M_current_rule->lhs); |
|
182 |
} |
|
183 |
||
184 |
return code; |
|
185 |
} |