-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto-cnf.C
482 lines (407 loc) · 12.7 KB
/
to-cnf.C
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/****************************************************************
to-cnf.C
This is open-source software, governed by the ARTISTIC LICENSE
(see www.opensource.org).
****************************************************************/
#include <iostream>
#include <fstream>
#include "BOOM/String.H"
#include "BOOM/CommandLine.H"
#include "BOOM/Exceptions.H"
#include "BOOM/SubsetEnumerator.H"
#include "SCFG.H"
#include "DerivesEpsilon.H"
#include "Terminates.H"
#include "ReachableFromStart.H"
using namespace std;
using namespace BOOM;
const int HASH_SIZE=499;
class Application {
void addNewStart(SCFG &);
String findUniqueID(GrammarAlphabet &);
GrammarSymbol *generateUnique(GrammarAlphabet &);
void eliminateUseless(SCFG &,Terminates &,ReachableFromStart &);
void factorNullable(SCFG &,DerivesEpsilon &);
void deleteNullProductions(SCFG &);
void delSymsWithNoProds(SCFG &);
void replaceUnitProductions(SCFG &);
void expandUnitProduction(SCFG &,Production &unit);
int countNT(Vector<GrammarSymbol*> &);
void getNullableIndices(Vector<int> &indices,Vector<GrammarSymbol*> &rhs,
DerivesEpsilon &);
void factorLong(SCFG &);
void factorTerminals(SCFG &);
void simplify(SCFG &G);
bool identical(Vector<Production*> &xProds,Vector<Production*> &yProds);
public:
Application();
int main(int argc,char *argv[]);
};
int main(int argc,char *argv[]) {
try
{
Application app;
return app.main(argc,argv);
}
catch(RootException &e)
{
cerr<<e.getMessage()<<endl;
}
catch(const char *p)
{
cerr << p << endl;
}
catch(const string &msg)
{
cerr << msg.c_str() << endl;
}
catch(const exception &e)
{
cerr << "STL exception caught in main:\n" << e.what() << endl;
}
catch(...)
{
cerr << "Unknown exception caught in main" << endl;
}
return -1;
}
Application::Application()
{
// ctor
}
int Application::main(int argc,char *argv[])
{
// Process command line
CommandLine cmd(argc,argv,"");
if(cmd.numArgs()!=1)
throw String("to-cnf <infile>");
const String infile=cmd.arg(0);
// Load grammar
SCFG G(infile);
G.normalize();
// **************************************************************
// Step 1: Add a new start symbol
addNewStart(G);
// **************************************************************
// Step 2: Compute P(derives eps) and P(derives a nonempty terminal string)
const float tolerance=1e-8;
ReachableFromStart reachable(G);
DerivesEpsilon nullable(G,tolerance);
Terminates terminates(G);
// **************************************************************
// Step 3: Eliminate null productions: X->epsilon (...combinatorial...)
deleteNullProductions(G);
factorNullable(G,nullable);
// **************************************************************
// Step 4: Eliminate unit productions: X->Y
replaceUnitProductions(G);
// **************************************************************
// Step 5: Eliminate symbols that do NOT derive a nonempty terminal string
eliminateUseless(G,terminates,reachable);
// **************************************************************
// Step 6: Factor terminals: A->cBe becomes A->CBE, C->c, E->e
factorTerminals(G);
// **************************************************************
// Step 7: Factor long RHS's: A->XYZ becomes A->XB, B->YZ
factorLong(G);
// **************************************************************
// Step 8: Simplify
simplify(G);
G.normalize();
cout<<G<<endl;
return 0;
}
String Application::findUniqueID(GrammarAlphabet &alpha)
{
String S="S";
int z=0;
while(1) {
if(!alpha.lookupLexeme(S)) return S;
++z;
S=String("S")+z;
}
}
void Application::addNewStart(SCFG &G)
{
GrammarAlphabet &alpha=G.getNonterminals();
String lexeme=findUniqueID(alpha);
GrammarSymbol *newStart=alpha.findOrCreate(lexeme,NONTERMINAL_SYMBOL);
GrammarSymbol *oldStart=G.getStart();
Production *prod=new Production;
prod->setLHS(newStart);
prod->getRHS().push_back(oldStart);
prod->setProbability(1);
G.getProductions().push_back(prod);
G.setStart(newStart);
}
void Application::eliminateUseless(SCFG &G,Terminates &terminates,
ReachableFromStart &reachable)
{
// Delete any production having a useless symbol on the LHS or RHS
Vector<Production*> &productions=G.getProductions();
int numProds=productions.size();
for(int i=0 ; i<numProds ; ++i) {
Production *prod=productions[i];
bool prodOK=true;
GrammarSymbol *lhs=prod->getLHS();
if(!terminates[lhs] || !reachable[lhs]) prodOK=false;
else {
Vector<GrammarSymbol*> &rhs=prod->getRHS();
for(Vector<GrammarSymbol*>::iterator cur=rhs.begin(), end=rhs.end() ;
cur!=end ; ++cur)
if((*cur)->isNonterminal() &&
(!terminates[*cur] || !reachable[*cur]))
{ prodOK=false; break; }
}
if(!prodOK) { delete prod; productions.cut(i); --i; --numProds; }
}
// Delete useless symbols from the alphabet
GrammarAlphabet &nonterminals=G.getNonterminals();
int N=nonterminals.size();
for(int i=0 ; i<N ; ++i) {
if(!terminates[nonterminals[i]] || !reachable[nonterminals[i]])
nonterminals.deleteIthEntry(i);
}
nonterminals.compact();
}
void Application::deleteNullProductions(SCFG &G)
{
Vector<Production*> &productions=G.getProductions();
int numProds=productions.size();
for(int i=0 ; i<numProds ; ++i) {
Production *prod=productions[i];
if(prod->isNull()) { delete prod; productions.cut(i); --i; --numProds; }
}
}
void Application::delSymsWithNoProds(SCFG &G)
{
ProductionSet &productions=*G.getProductionSet();
GrammarAlphabet &nonterminals=G.getNonterminals();
int numNT=nonterminals.size();
for(int i=0 ; i<numNT ; ++i) {
GrammarSymbol *s=nonterminals[i];
if(!s) continue;
if(productions.lookup(s).size()==0)
nonterminals.deleteIthEntry(i);
}
delete &productions;
nonterminals.compact();
}
void Application::replaceUnitProductions(SCFG &G)
{
while(1) {
bool changes=false;
Vector<Production*> &productions=G.getProductions();
int numProds=productions.size();
for(int i=0 ; i<numProds ; ++i) {
Production *prod=productions[i];
Vector<GrammarSymbol*> &rhs=prod->getRHS();
if(rhs.size()==1 && rhs[0]->isNonterminal()) {
expandUnitProduction(G,*prod);
delete prod;
productions.cut(i);
--i; --numProds;
changes=true;
}
}
if(!changes) break;
}
}
void Application::expandUnitProduction(SCFG &G,Production &unit)
{
const float unitProb=unit.getProbability();
GrammarSymbol *lhs=unit.getLHS(), *rhs=unit.getRHS()[0];
ProductionSet *prodSet=G.getProductionSet();
Vector<Production*> &rhsProds=prodSet->lookup(rhs);
for(Vector<Production*>::iterator cur=rhsProds.begin(), end=rhsProds.end() ;
cur!=end ; ++cur) {
Production *newProd=new Production;
newProd->setLHS(lhs);
newProd->getRHS()=(*cur)->getRHS();
const float rhsProb=(*cur)->getProbability();
newProd->setProbability(unitProb*rhsProb);
G.getProductions().push_back(newProd);
}
delete prodSet;
}
int Application::countNT(Vector<GrammarSymbol*> &V)
{
int c=0;
for(Vector<GrammarSymbol*>::iterator cur=V.begin(), end=V.end() ;
cur!=end ; ++cur)
if((*cur)->isNonterminal()) ++c;
return c;
}
void Application::getNullableIndices(Vector<int> &indices,
Vector<GrammarSymbol*> &rhs,
DerivesEpsilon &nullable)
{
int L=rhs.size();
for(int i=0 ; i<L ; ++i) {
GrammarSymbol *s=rhs[i];
if(s->isNonterminal() && nullable[s]>0)
indices.push_back(i);
}
}
void Application::factorNullable(SCFG &G,DerivesEpsilon &nullable)
{
Vector<Production*> &productions=G.getProductions(), newProds;
int numProd=productions.size();
for(int i=0 ; i<numProd ; ++i) {
Production *prod=productions[i];
Vector<GrammarSymbol*> &rhs=prod->getRHS();
Vector<int> indices;
getNullableIndices(indices,rhs,nullable);
const int numNullable=indices.size();
if(!numNullable) continue;
SubsetEnumerator iter(numNullable);
Set<int> subset;
while(iter.getNext(subset)) {
Set<int> keep;
for(Set<int>::iterator cur=subset.begin(), end=subset.end() ;
cur!=end ; ++cur) keep.insert(indices[*cur]);
Production *newProd=new Production;
float P=prod->getProbability();
newProd->setLHS(prod->getLHS());
Vector<GrammarSymbol*> rhs, &oldRhs=prod->getRHS();
int L=oldRhs.size();
for(int i=0 ; i<L ; ++i) {
GrammarSymbol *s=oldRhs[i];
if(s->isTerminal() || nullable[s]==0) rhs.push_back(s);
else if(keep.isMember(i)) { rhs.push_back(s); P*=(1-nullable[s]); }
else P*=nullable[s];
}
if(rhs.isEmpty() || P==0) { delete newProd; continue; }
newProd->getRHS()=rhs;
newProd->setProbability(P);
newProds.push_back(newProd);
}
delete prod;
productions.cut(i);
--i; --numProd;
}
productions.append(newProds);
}
void Application::factorLong(SCFG &G)
{
// Factor long RHS's: A->CDE becomes A->CF, F->DE
// Precondition: any RHS>1 contains only nonterminals
GrammarAlphabet &nonterminals=G.getNonterminals();
Vector<Production*> &productions=G.getProductions(), newProds;
int numProds=productions.size();
for(int i=0 ; i<numProds ; ++i) {
Production *prod=productions[i];
Vector<GrammarSymbol*> &rhs=prod->getRHS();
const int L=rhs.size();
if(L<3) continue;
GrammarSymbol *lhs=prod->getLHS();
for(int j=0 ; j<L-1 ; ++j) {
Production *newProd=new Production;
newProd->setLHS(lhs);
newProd->setProbability(j==0 ? prod->getProbability() : 1.0);
Vector<GrammarSymbol*> &newRHS=newProd->getRHS();
newRHS.push_back(rhs[j]);
GrammarSymbol *nextS=j<L-2 ? generateUnique(nonterminals) : rhs[L-1];
newRHS.push_back(nextS);
lhs=nextS;
newProds.push_back(newProd);
}
delete prod;
productions.cut(i);
--i; --numProds;
}
productions.append(newProds);
}
void Application::factorTerminals(SCFG &G)
{
// Factor terminals: A->cB becomes A->CB, C->c
Vector<Production*> &productions=G.getProductions(), newProds;
GrammarAlphabet &nonterminals=G.getNonterminals(),
&terminals=G.getTerminals();
HashMap<GrammarSymbol,GrammarSymbol*> alias(HASH_SIZE);
const int T=terminals.size();
for(int i=0 ; i<T ; ++i) {
GrammarSymbol *term=terminals[i], *newNT=generateUnique(nonterminals);
alias[*term]=newNT;
Production *prod=new Production;
prod->setProbability(1);
prod->setLHS(newNT);
prod->getRHS().push_back(term);
newProds.push_back(prod);
}
int numProds=productions.size();
for(int i=0 ; i<numProds ; ++i) {
Production *prod=productions[i];
Vector<GrammarSymbol*> &rhs=prod->getRHS();
const int L=rhs.size();
if(L==1) continue;
for(int j=0 ; j<L ; ++j) {
GrammarSymbol *s=rhs[j];
if(s->isTerminal()) rhs[j]=alias[*s];
}
}
productions.append(newProds);
}
GrammarSymbol *Application::generateUnique(GrammarAlphabet &alpha)
{
String lexeme=findUniqueID(alpha);
return alpha.findOrCreate(lexeme,NONTERMINAL_SYMBOL);
}
void Application::simplify(SCFG &G)
{
HashMap<GrammarSymbol,GrammarSymbol*> alias(HASH_SIZE);
GrammarAlphabet &nonterminals=G.getNonterminals();
ProductionSet &prodSet=*G.getProductionSet();
int numNT=nonterminals.size();
for(int i=0 ; i<numNT ; ++i) {
GrammarSymbol *X=nonterminals[i];
if(alias.isDefined(*X)) continue;
Vector<Production*> &xProds=prodSet.lookup(X);
for(int j=i+1 ; j<numNT ; ++j) {
GrammarSymbol *Y=nonterminals[j];
Vector<Production*> &yProds=prodSet.lookup(Y);
if(identical(xProds,yProds)) alias[*Y]=X;
}
}
delete &prodSet;
Vector<Production*> &productions=G.getProductions();
int numProds=productions.size();
for(int i=0 ; i<numProds ; ++i) {
Production *prod=productions[i];
if(alias.isDefined(*prod->getLHS())) {
delete prod;
productions.cut(i);
--i; --numProds;
continue;
}
Vector<GrammarSymbol*> &rhs=prod->getRHS();
int L=rhs.size();
for(int i=0 ; i<L ; ++i)
if(alias.isDefined(*rhs[i]))
rhs[i]=alias[*rhs[i]];
}
for(int i=0 ; i<numNT ; ++i) {
GrammarSymbol *X=nonterminals[i];
if(alias.isDefined(*X)) nonterminals.deleteIthEntry(i);
}
nonterminals.compact();
}
bool Application::identical(Vector<Production*> &xProds,
Vector<Production*> &yProds)
{
int numX=xProds.size(), numY=yProds.size();
if(numX!=numY) return false;
for(int i=0 ; i<numX ; ++i) {
Production *xProd=xProds[i];
float P=xProd->getProbability();
Vector<GrammarSymbol*> &rhs=xProd->getRHS();
bool found=false;
for(int j=0 ; j<numY ; ++j) {
Production *yProd=yProds[j];
if(yProd->getProbability()==P && yProd->getRHS()==rhs) found=true;
}
if(!found) return false;
}
return true;
}