-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCFG.C
130 lines (92 loc) · 2.31 KB
/
SCFG.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
/****************************************************************
SCFG.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 <fstream>
#include "BOOM/GenericScanner.H"
#include "BOOM/GenericTokenStream.H"
#include "SCFG.H"
#include "SCFG_Parser.H"
using namespace std;
using namespace BOOM;
SCFG::SCFG()
{
// ctor
}
SCFG::SCFG(const String &filename)
{
load(filename);
}
GrammarSymbol *SCFG::getStart()
{
return start;
}
void SCFG::setStart(GrammarSymbol *s)
{
start=s;
}
void SCFG::deleteAll()
{
terminals.deleteAll();
nonterminals.deleteAll();
for(Vector<Production*>::iterator cur=productions.begin(),
end=productions.end() ; cur!=end ; ++cur)
delete *cur;
}
void SCFG::load(const String &filename)
{
ifstream is(filename.c_str());
if(!is.good()) throw String("Error opening file ")+filename;
GenericScanner scanner(is);
GenericTokenStream tokenStream(&scanner);
SCFG_Parser parser(tokenStream);
productions=parser.getProductions();
terminals=parser.getTerminals();
nonterminals=parser.getNonterminals();
if(terminals.size()<1) throw String("no terminal symbols found in grammar");
if(nonterminals.size()<1)
throw String("no nonterminal symbols found in grammar");
start=parser.getStartSymbol();
}
GrammarAlphabet &SCFG::getTerminals()
{
return terminals;
}
GrammarAlphabet &SCFG::getNonterminals()
{
return nonterminals;
}
Vector<Production*> &SCFG::getProductions()
{
return productions;
}
void SCFG::printOn(ostream &os) const
{
os<<"start "<<start->getLexeme()<<endl;
for(Vector<Production*>::const_iterator cur=productions.begin(),
end=productions.end() ; cur!=end ; ++cur)
os<<**cur<<endl;
}
ostream &operator<<(ostream &os,const SCFG &scfg)
{
scfg.printOn(os);
return os;
}
ProductionSet *SCFG::getProductionSet()
{
ProductionSet *S=new ProductionSet;
for(Vector<Production*>::iterator cur=productions.begin(),
end=productions.end() ; cur!=end ; ++cur)
S->add(*cur);
S->reindex();
return S;
}
void SCFG::normalize()
{
ProductionSet *S=getProductionSet();
S->normalize(nonterminals);
delete S;
}