-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductionSet.C
101 lines (72 loc) · 1.93 KB
/
ProductionSet.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
/****************************************************************
ProductionSet.C
Copyright (C)2013 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
****************************************************************/
#include <iostream>
#include "ProductionSet.H"
using namespace std;
using namespace BOOM;
ProductionSet::ProductionSet()
: index(101)
{
// ctor
}
void ProductionSet::add(Production *prod)
{
productions.push_back(prod);
}
void ProductionSet::reindex()
{
index.clear();
for(Vector<Production*>::iterator cur=productions.begin(),
end=productions.end() ; cur!=end ; ++cur) {
Production *prod=*cur;
GrammarSymbol *lhs=prod->getLHS();
lookup(lhs).push_back(prod);
}
}
int ProductionSet::size() const
{
return productions.size();
}
Production *ProductionSet::operator[](int i)
{
return productions[i];
}
void ProductionSet::removeProduction(int i)
{
productions.cut(i);
}
Vector<Production*> &ProductionSet::lookup(GrammarSymbol *s)
{
if(!index.isDefined(*s)) index[*s]=Vector<Production*>();
return index[*s];
}
void ProductionSet::printOn(ostream &os) const
{
for(Vector<Production*>::const_iterator cur=productions.begin(),
end=productions.end() ; cur!=end ; ++cur)
os<<**cur<<endl;
}
ostream &operator<<(ostream &os,const ProductionSet &S)
{
S.printOn(os);
return os;
}
void ProductionSet::normalize(GrammarAlphabet &alpha)
{
int n=alpha.size();
for(int i=0 ; i<n ; ++i) {
GrammarSymbol *s=alpha[i];
Vector<Production*> &prods=lookup(s);
float sum=0;
for(Vector<Production*>::iterator cur=prods.begin(), end=prods.end() ;
cur!=end ; ++cur)
sum+=(*cur)->getProbability();
for(Vector<Production*>::iterator cur=prods.begin(), end=prods.end() ;
cur!=end ; ++cur)
(*cur)->setProbability((*cur)->getProbability()/sum);
}
}