This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RG_Grammar.cpp
332 lines (297 loc) · 8.03 KB
/
RG_Grammar.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* @file RG_Grammar.cpp
* @author Yacine Smaoui, Florian Hemmelgarn
*
* @brief implementation of the Grammar class
*/
#include "RG_Grammar.h"
#include "FSA_FSAtoREConverter.hpp"
#include <stdlib.h>
#include <sstream>
/**
* @brief Constructor.
*/
Grammar::Grammar()
{
//cout << "****Grammar constructor called" << endl ;
}
/**
* @brief Destructor
*/
Grammar::~Grammar()
{
//cout << "**Grammar destructor called: destructing Grammar" << endl ;
}
/**
* @brief adds a production to the Grammar's Productions container.
* @param prod a pointer on a Production Object.
*/
void Grammar::addProduction(Production* prod)
{
this->Productions.add(prod);
}
/**
* @brief A Setter Method for the Start Symbol.
* @param s the Start Symbol.
*/
void Grammar::setStartSymbol(string s)
{
this->StartSymbol = s ;
}
/**
* @brief adds a Terminal Symbol to the Grammar's Terminals container.
* @param s a Terminal Symbol.
*/
void Grammar::addTerminal(string s)
{
this->Terminals.add(s);
}
/**
* @brief adds a Non-Terminal Symbol to the Grammars's Terminals container.
* @param s
*/
void Grammar::addNonTerminal(string s)
{
this->NonTerminals.add(s);
}
/**
* @brief processes the Grammar Productions
*
* This is the last called method in RG_Reader.read() .
* the read method stores a Production at first as a whole string, and this Method decompose it
* into a left side and its substitution in the right side
*/
void Grammar::processGrammarProductions()
{
unsigned int i;
for(i=0; i<this->Productions.getLength(); i++)
{
Productions[i]->getSubstitution()->decode(this->Terminals, this->NonTerminals);
/*
cout << "substitution decoded" << endl ;
*/
}
}
/**
* @brief a getter Method
* @return all the Grammar Terminals stored in a DynArray
*/
DynArray<string> Grammar::getTerminals()
{
return this->Terminals ;
}
/**
* @brief a getter Method
* @return all the Grammar Non-Terminals stored in a DynArray
*/
DynArray<string> Grammar::getNonTerminals()
{
return this->NonTerminals ;
}
/**
* @brief a getter Method
* @return the Start Symbol of the Grammar
*/
string Grammar::getStartSymbol()
{
return this->StartSymbol ;
}
/**
* @brief a getter Method
* @return all the Grammar Productions stored in a DynArray
*/
DynArray<Production*> Grammar::getProductions()
{
return this->Productions;
}
/**
* @brief checks if the Grammar is regular or not. the information is stored in isRegular .
*
* we use in the project a right-linear regular Grammar.
*
* a Grammar is said to be right-linear if every production in P is of the form:
*
* A --> x B or
*
* A --> x
*
* where A and B are Non-Terminals
* and x is any string of Terminals or the empty string
*
*/
void Grammar::checkIfRegular()
{
this->isRegular = 1 ;
for(unsigned int i=0 ; i < this->getProductions().getLength(); i++ ) // For every Production
{
/**
* check if the First Symbol of the Substitution is a Terminal
*/
if(this->getProductions()[i]->getSubstitution()->symbolIsTerminal(0) != 1 ) //First Symbol in Substitution is not a Terminal
{
this->isRegular = 0 ;
/*
this->getProductions()[i]->printProduction();
cout << "Grammar is not regular: First Substitution Symbol of Production "<<i<<" is not a Terminal" << endl;
*/
return;
}
/**
* check if the Form of the Substitution corresponds to a Regular Grammar Form
*/
for(int j=1 ; j< this->getProductions()[i]->getSubstitution()->getDecodedSubstitutionLength(); j++) // For every symbol in a Substitution
{
if(this->getProductions()[i]->getSubstitution()->symbolIsTerminal(j) == 0 //the actual symbol is not a Terminal
&& this->getProductions()[i]->getSubstitution()->getDecodedSubstitutionLength()-1 > j) // there is more symbols after this NonTerminal
{
this->isRegular = 0 ;
/*
this->getProductions()[i]->printProduction();
cout << "Grammar is not regular: Substitution of Production "<<i<<" doesn't correspond to regular grammar form" << endl;
*/
return;
}
}
}
}
/**
* @brief a getter method
* @return how many Productions in the Grammar
*/
int Grammar::getNumberOfProductions()
{
return this->getProductions().getLength() ;
}
/**
* @brief a getter method
* @param index Productions are stored in a Dynamic Array, index is the index of the needed Production
* @return
*/
Production* Grammar::getProduction(int index)
{
return this->getProductions()[index];
}
/**
* @brief compares the startSymbol of the Grammar with the parameter symbol
* @param symbol is one string used in the grammar
* @return
*/
int Grammar::isStartSymbol(string symbol)
{
if(this->getStartSymbol().compare(symbol)==0)
{
return 1;
}
return 0;
}
/**
* @brief checks if Grammar is a regular Grammar, if it is not the conversion cannot be proceeded.
*/
void Grammar::initConvert()
{
this->checkIfRegular();
cout<< "IS REGULAR? :" << this->isRegular<< endl;
if(this->isRegular == 0)
{
cerr << "Grammar is not Regular, cannot Convert" << endl ;
exit(1);
}
}
/**
* @brief converts a Regular Grammar to a Finite States Automaton
*
*
*
* @return the FSA
*/
FiniteStateAutomaton* Grammar::convertToFSA()
{
this->initConvert();
FiniteStateAutomaton* automat = new FiniteStateAutomaton();
/**First end of the Transition */
State* pFromState;
for(int i=0; i< this->getNumberOfProductions() ; i++)
{
Production* pProd = this->getProduction(i);
Substitution* pSubs = pProd->getSubstitution();
string leftSide = pProd->getLeftSide();
if (!automat->bStateExists(leftSide))
{
automat->addState(leftSide);
}
if (this->isStartSymbol(leftSide))
{
automat->getState(leftSide)->setStartState(true);
}
pFromState = automat->getState(leftSide);
/** if Production has the form: A --> <epsilon> */
if(pSubs->getSymbolstring(0).compare(EMPTY_STRING) == 0)
{
pFromState->setFinalState(true);
}
else
{
int substitutionlength= pSubs->getDecodedSubstitutionLength();
/** For every symbol in the Substitution */
for(int j=0 ; j < substitutionlength; j++)
{
/** the current symbol is a Terminal */
if(pSubs->symbolIsTerminal(j))
{
/** the current Symbol is the last one in the Substitution, and it is a Terminal */
if(j+1 == substitutionlength )
{
if (!automat->bStateExists("endState"))
{
automat->addState("endState");
automat->getState("endState")->setFinalState(true);
}
automat->addTransition(pFromState->output(),pSubs->getSymbolstring(j),"endState");
}
/** the current symbol is not the last one in the substitution */
else
{
/** the next symbol in the Substitution is a NonTerminal */
if(!pSubs->symbolIsTerminal(j+1))
{
if (!automat->bStateExists(pSubs->getSymbolstring(j+1)))
{
automat->addState(pSubs->getSymbolstring(j+1));
}
automat->addTransition(pFromState->output(),pSubs->getSymbolstring(j),pSubs->getSymbolstring(j+1));
}
/** the next symbol in the Substitution is a Terminal */
else
{
stringstream ss;
string stateName;
ss << "TransitionState" << i << j ;
ss >> stateName ;
automat->addState(stateName);
automat->addTransition(pFromState->output(),pSubs->getSymbolstring(j),stateName);
pFromState = automat->getState(stateName);
}
}
}
}
}
}
automat->getFinalStates();
FiniteStateAutomaton* deterministic_automat = automat->fsaConvertNEAtoDEA();
delete automat;
return deterministic_automat;
}
/**
* @brief converts a Grammar to a regular expression
*
* uses the conversion from FSA to RE
*
* @return
*/
/*****************************************
RegularExpression* Grammar::convertToRE()
{
this->initConvert();
return FSAtoREConverter::toRE(this->convertToFSA());
}
******************************************/