-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLlgenGen.cpp
205 lines (182 loc) · 6.13 KB
/
LlgenGen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the EbnfStudio application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "LlgenGen.h"
#include "SynTreeGen.h"
#include "GenUtils.h"
#include "EbnfAnalyzer.h"
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QDir>
#include <QtDebug>
bool LlgenGen::generate(const QString& atgPath, EbnfSyntax* syn, FirstFollowSet* tbl)
{
if( syn == 0 || syn->getOrderedDefs().isEmpty() )
return false;
const Ast::Definition* root = syn->getOrderedDefs()[0];
QFile f(atgPath);
f.open( QIODevice::WriteOnly );
QTextStream out(&f);
out.setCodec("utf-8");
out << "// This file was automatically generated by EbnfStudio; don't modify it!" << endl << endl;
out << "%options \"generate-lexer-wrapper=no\";" << endl << endl;
out << "%token" << endl;
const QStringList tokens = GenUtils::orderedTokenList( EbnfAnalyzer::collectAllTerminalStrings( syn ) ) +
EbnfAnalyzer::collectAllTerminalProductions( syn );
for( int t = 0; t < tokens.size(); t++ )
{
if( t != 0 )
out << "," << endl;
out << "\t" << tokenName(tokens[t]);
}
out << ";" << endl << endl;
out << endl;
out << "%start parser, " << ruleName(root->d_tok.d_val.toStr()) << ";" << endl << endl;
for( int i = 0; i < syn->getOrderedDefs().size(); i++ )
{
const Ast::Definition* d = syn->getOrderedDefs()[i];
if( d->d_tok.d_op == EbnfToken::Skip || ( i != 0 && d->d_usedBy.isEmpty() ) )
continue;
if( d->d_node == 0 )
continue;
out << ruleName( d->d_tok.d_val.toStr() ) << " : " << endl << " ";
writeNode( out, d->d_node, true, tbl );
out << endl << " ;" << endl << endl;
}
return true;
}
void LlgenGen::writeNode(QTextStream& out, Ast::Node* node, bool topLevel, FirstFollowSet* tbl)
{
if( node == 0 )
return;
if( node->d_tok.d_op == EbnfToken::Skip )
return;
if( node->d_def && node->d_def->d_tok.d_op == EbnfToken::Skip )
return;
switch( node->d_quant )
{
case Ast::Node::One:
if( !topLevel && node->d_type == Ast::Node::Alternative )
out << "[ ";
else if( !topLevel && node->d_type == Ast::Node::Sequence && !node->d_tok.d_val.isEmpty() )
out << "[ ";
break;
case Ast::Node::ZeroOrOne:
case Ast::Node::ZeroOrMore:
out << "[ ";
break;
}
switch( node->d_type )
{
case Ast::Node::Terminal:
out << tokenName( node->d_tok.d_val.toStr() ) << " ";
break;
case Ast::Node::Nonterminal:
if( node->d_def == 0 || node->d_def->d_node == 0 )
out << tokenName(node->d_tok.d_val.toStr()) << " ";
else
out << ruleName(node->d_tok.d_val.toStr()) << " ";
break;
case Ast::Node::Alternative:
for( int i = 0; i < node->d_subs.size(); i++ )
{
if( i != 0 )
{
if( topLevel )
out << endl << " | ";
else
out << "| ";
}
writeNode( out, node->d_subs[i], false, tbl );
}
break;
case Ast::Node::Sequence:
for( int i = 0; i < node->d_subs.size(); i++ )
{
if( node->d_subs[i]->d_type == Ast::Node::Predicate )
; // handlePredicate( out, node->d_subs[i], node, tbl );
else
writeNode( out, node->d_subs[i], false, tbl );
}
break;
default:
break;
}
switch( node->d_quant )
{
case Ast::Node::One:
if( !topLevel && node->d_type == Ast::Node::Alternative )
out << "] ";
else if( !topLevel && node->d_type == Ast::Node::Sequence && !node->d_tok.d_val.isEmpty() )
out << "] ";
break;
case Ast::Node::ZeroOrOne:
out << "]? ";
break;
case Ast::Node::ZeroOrMore:
out << "]* ";
break;
}
}
QString LlgenGen::tokenName(const QString& str)
{
QString tok = GenUtils::symToString(str).toUpper();
if( !tok.isEmpty() && tok[0].isDigit() )
tok = QChar('T') + tok;
return tok;
}
QString LlgenGen::ruleName(const QString& str)
{
return GenUtils::escapeDollars( str ).toLower();
}
void LlgenGen::handlePredicate(QTextStream& out, Ast::Node* pred, Ast::Node* sequence, FirstFollowSet* tbl)
{
const QString val = pred->d_tok.d_val.toStr();
if( val.startsWith("LL:") )
{
const int ll = val.mid(3).toInt();
if( ll <= 1 )
return;
EbnfAnalyzer::LlkNodes llkNodes;
//EbnfAnalyzer::calcLlkFirstSet2( ll, llkNodes,sequence, tbl );
EbnfAnalyzer::calcLlkFirstSet( ll, llkNodes,sequence, tbl );
out << "%if( ";
for( int i = 0; i < llkNodes.size(); i++ )
{
if( i != 0 )
out << "&& ";
if( llkNodes[i].size() > 1 )
out << "( ";
Ast::NodeRefSet::const_iterator j;
for( j = llkNodes[i].begin(); j != llkNodes[i].end(); ++j )
{
if( j != llkNodes[i].begin() )
out << "|| ";
out << "peek(" << i+1 << ") == _" << tokenName( (*j).d_node->d_tok.d_val.toStr()) << " ";
}
if( llkNodes[i].size() > 1 )
out << ") ";
}
out << ") ";
}else
qWarning() << "LlgenGen unknown predicate" << sequence->d_tok.d_val.toStr();
}
LlgenGen::LlgenGen()
{
}