forked from rperlste/UniversalCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProduction.h
142 lines (115 loc) · 3.28 KB
/
Production.h
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
#ifndef _Production
#define _Production
#include <string>
#include <algorithm>
#include "IndexedList.h"
#include "CompilerExceptions.h"
typedef std::string Symbol;
class RightHandSide : public IndexedList<Symbol>{};
class Production {
public:
inline bool contains( const Symbol& value ) { return RHS.contains( value ); }
inline void setLHS( const Symbol& LHS ) { this->LHS = LHS; }
inline Symbol getLHS() const { return LHS; }
inline void addRHSValue( const Symbol& symbol ) { RHS.push_back( symbol ); }
inline void setRHS( const RightHandSide& RHS ) { this->RHS.set( RHS ); }
inline RightHandSide getRHS() { return RHS; }
inline RightHandSide getRHS() const { return RHS; }
inline Symbol getRHSValueByIndex( const int& index ) const { return RHS[index]; }
inline Symbol operator [] ( const int& index ) { return RHS[index]; }
inline bool operator < ( const Production& production ) { return this->getLHS() < production.getLHS(); }
inline const bool operator < ( const Production& production ) const { return this->getLHS() < production.getLHS(); }
bool operator == ( const Production& production ) {
if( LHS.compare( production.getLHS() ) != 0 ){
return false;
}
else if( RHS.size() != production.RHS.size() ){
return false;
}
else{
for( int i = 0; i < RHS.size(); i++ ){
if( RHS[i].compare( production.getRHSValueByIndex(i)) != 0 ){
return false;
}
}
}
return true;
}
const bool operator == ( const Production& production ) const {
if( LHS.compare( production.getLHS() ) != 0 ){
return false;
}
else if( RHS.size() != production.RHS.size() ){
return false;
}
else{
for( int i = 0; i < RHS.size(); i++ ){
if( RHS[i].compare( production.getRHSValueByIndex(i)) != 0 ){
return false;
}
}
}
return true;
}
private:
Symbol LHS;
RightHandSide RHS;
};
class Productions : public IndexedList<Production> {
public:
void add( Production production ){
if( size() > 0 ){
for( std::list<Production>::iterator itProd = begin();
itProd != end();
itProd++ ){
// Test to see if the left hand side is being repeated.
// Behaves like a set
if( !contains(production) ){
push_back( production );
return;
}
}
}
push_back( production );
}
Productions getProductionSubset( Symbol nonterminal ){
Productions productionSubset;
if( size() > 0 ){
for( std::list<Production>::iterator itProd = begin();
itProd != end();
itProd++ ){
// Test to see if the left hand side is being repeated.
if( itProd->getLHS() == nonterminal ){
productionSubset.push_back(*itProd);
}
}
}
if( productionSubset.size() == 0 )
throw ProductionNotFound( nonterminal );
return productionSubset;
}
int getUniqueProductionCount( Symbol nonterminal ){
int count = 0;
for( std::list<Production>::iterator it = begin();
it != end();
it++ ){
if( (*it).getLHS() == nonterminal )
count++;
}
return count;
}
int getProductionIndexFirstOf( Symbol nonterminal ){
int index = 0;
for( std::list<Production>::iterator itProd = begin();
itProd != end();
itProd++ ){
// Test to see if the left hand side is being repeated.
if( (*itProd).getLHS() == nonterminal ){
return index;
}
index++;
}
throw ProductionNotFound( nonterminal );
}
};
#endif