-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrammarAlphabet.C
More file actions
104 lines (70 loc) · 1.82 KB
/
GrammarAlphabet.C
File metadata and controls
104 lines (70 loc) · 1.82 KB
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
/****************************************************************
GrammarAlphabet.C
william.majoros@duke.edu
This is open-source software, governed by the ARTISTIC LICENSE
(see www.opensource.org).
****************************************************************/
#include <iostream>
#include "GrammarAlphabet.H"
using namespace std;
using namespace BOOM;
GrammarAlphabet::GrammarAlphabet()
{
// ctor
}
void GrammarAlphabet::deleteAll()
{
for(Vector<GrammarSymbol*>::iterator cur=symbols.begin(), end=symbols.end() ;
cur!=end ; ++cur)
delete *cur;
}
void GrammarAlphabet::addSymbol(GrammarSymbol *symbol)
{
symbols.push_back(symbol);
}
int GrammarAlphabet::size() const
{
return symbols.size();
}
GrammarSymbol *GrammarAlphabet::operator[](int i)
{
return symbols[i];
}
GrammarSymbol *GrammarAlphabet::lookupLexeme(const BOOM::String &lexeme)
{
if(symbolIndex.isDefined(lexeme)) return symbolIndex[lexeme];
else return NULL;
}
void GrammarAlphabet::deleteIthEntry(int i)
{
delete symbols[i];
symbols[i]=NULL;
}
void GrammarAlphabet::compact()
{
BOOM::Vector<GrammarSymbol*> A;
for(BOOM::Vector<GrammarSymbol*>::iterator cur=symbols.begin(),
end=symbols.end() ; cur!=end ; ++cur)
if(*cur) A.push_back(*cur);
symbols=A;
}
GrammarSymbol *GrammarAlphabet::findOrCreate(const String &lexeme,
SymbolType t)
{
if(symbolIndex.isDefined(lexeme)) return symbolIndex[lexeme];
GrammarSymbol *s=new GrammarSymbol(t,lexeme);
symbols.push_back(s);
symbolIndex[lexeme]=s;
return s;
}
void GrammarAlphabet::printOn(ostream &os) const
{
for(BOOM::Vector<GrammarSymbol*>::const_iterator cur=symbols.begin(),
end=symbols.end() ; cur!=end ; ++cur)
os<<(*cur)->getLexeme()<<endl;
}
ostream &operator<<(ostream &os,const GrammarAlphabet &alpha)
{
alpha.printOn(os);
return os;
}