forked from rperlste/UniversalCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammarAnalyzer.cpp
223 lines (195 loc) · 5.1 KB
/
GrammarAnalyzer.cpp
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
#include "GrammarAnalyzer.h"
GrammarAnalyzer::GrammarAnalyzer( std::fstream* grammarFileIn ){
grammarFile = grammarFileIn;
grammarFile->clear();
grammarFile->seekg(0);
error = false;
}
void GrammarAnalyzer::analyzeGrammar(){
char charStream;
std::string currentGrammar;
std::string LHS_val;
std::string RHS_val;
while( !grammarFile->eof() ){
Production production;
LHS_val = _LAMBDA;
RHS_val = _LAMBDA;
RightHandSide RHS;
currentGrammar = _LAMBDA;
// read LHS first
charStream = grammarFile->get();
// remove leading whitespace
if( charStream == ' ' ){
while( charStream == ' ' )
charStream = grammarFile->get();
}
currentGrammar += charStream;
while( charStream != '>' ){
charStream = grammarFile->get();
currentGrammar += charStream;
}
// Complete LHS
LHS_val = currentGrammar;
production.setLHS( LHS_val );
this->grammar.nonterminalSet.insert( LHS_val );
// Clear trailing whitespace
while( charStream != '-'
&& charStream != '—'){
charStream = grammarFile->get();
}
// Test for implies symbol
charStream = grammarFile->get();
if( charStream != '>' ){
analyzeError( "Lacks implies symbol." );
return;
}
// Clear leading whitespace
currentGrammar = _LAMBDA;
charStream = grammarFile->get();
while( charStream == ' ' ){
charStream = grammarFile->get();
}
// Extract RHS(s) of productions
while( charStream != '\n'
&& !grammarFile->eof() ){
currentGrammar = _LAMBDA;
// nonterminal
if( charStream == '<' ){
//currentGrammar += charStream;
RHS_val += charStream;
while( charStream != '>' ){
charStream = grammarFile->get();
//currentGrammar += charStream;
RHS_val += charStream;
}
RHS.add( RHS_val );
RHS_val = _LAMBDA;
charStream = grammarFile->get();
}
// terminal
else if( isalpha( charStream ) ){
while( isalpha( charStream ) ){
currentGrammar += charStream;
RHS_val += charStream;
charStream = grammarFile->get();
}
RHS.add( RHS_val );
RHS_val = _LAMBDA;
this->grammar.terminalSet.insert( currentGrammar );
}
// test for multiple productions
else if( charStream == '|' ){
if( RHS.size() == 0 ){
this->grammar.terminalSet.insert( _LAMBDA );
RHS.add( _LAMBDA );
}
production.setRHS( RHS );
grammar.productions.add( production );
RHS.clear();
RHS_val = _LAMBDA;
charStream = grammarFile->get();
if( charStream == ' ' ){
while( charStream == ' ' )
charStream = grammarFile->get();
}
continue;
}
// test for whitespace
else if( charStream == ' ' ){
//RHS += charStream;
charStream = grammarFile->get();
continue;
}
// test for EOL
else if( charStream == '\n' ){
if( RHS.size() == 0 ){
this->grammar.terminalSet.insert( _LAMBDA );
RHS.add( _LAMBDA );
}
break;
}
// Symbol or unknown value
else{
RHS_val += charStream;
charStream = grammarFile->get();
RHS.add( RHS_val );
this->grammar.terminalSet.insert( RHS_val );
RHS_val = _LAMBDA;
}
}
if( RHS.size() == 0 ){
RHS.add( _LAMBDA );
this->grammar.terminalSet.insert( _LAMBDA );
}
production.setRHS( RHS );
grammar.productions.add( production );
}
}
Grammar GrammarAnalyzer::getGrammar(){
return grammar;
}
void GrammarAnalyzer::printProductions(){
std::cout << "\n\n::: PRODUCTIONS :::";
try{
for( int i = 0; i < grammar.productions.size(); i++ ){
std::cout << "\n" << grammar.productions.get(i).getLHS() << " ->";
for( int k = 0; k < grammar.productions.get(i).getRHS().size(); k++ ){
std::cout << " " << grammar.productions.get(i).getRHS().get(k);
}
}
}
catch( IndexOutOfBounds e){
// Do nothing
}
}
void GrammarAnalyzer::printDataTables(){
std::cout << "\n\n::: LHS list :::";
try{
for( int i = 0; i < grammar.productions.size(); i++ ){
std::cout << "\n" << grammar.productions.get(i).getLHS();
}
}
catch( IndexOutOfBounds e){
// Do nothing
}
std::cout << "\n\n::: RHS list :::";
try{
for( int i = 0; i < grammar.productions.size(); i++ ){
std::cout << "\n";
for( int k = 0; k < grammar.productions.get(i).getRHS().size(); k++ ){
std::cout << grammar.productions.get(i).getRHS().get(k) << " ";
}
}
}
catch( IndexOutOfBounds e){
// Do nothing
}
std::cout << "\n\n::: Non-terminal list :::";
try{
for( int i = 0; i < grammar.nonterminalSet.size(); i++ ){
std::cout << "\n" << grammar.nonterminalSet[i];
}
}
catch( IndexOutOfBounds e){
// Do nothing
}
std::cout << "\n\n::: Terminal list :::";
for( int i = 0; i < grammar.terminalSet.size(); i++ ){
std::cout << "\n" << grammar.terminalSet[i];
}
}
void GrammarAnalyzer::analyzeError( std::string error ){
if( error.size() > 0 ){
std::cout << "\nGRAMMAR ANALYSIS ERROR: " << error;
}
}
void GrammarAnalyzer::printInputGrammar(){
grammarFile->clear();
grammarFile->seekg(0, std::ios::beg);
std::cout << "\n\n::: Original Grammar :::";
while( !grammarFile->eof() ){
std::string output;
getline( (*grammarFile), output );
std::cout << "\n" << output;
}
}