Skip to content

Commit b6b14bc

Browse files
#TDP - 14 Add method
Class Syntax analisator add method analyze Analyze: read , write to file
1 parent 2a07756 commit b6b14bc

File tree

3 files changed

+169
-186
lines changed

3 files changed

+169
-186
lines changed

MethodsDevelopmentTranslator.cpp

Lines changed: 1 addition & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -29,169 +29,7 @@ using namespace std;
2929
int main(int argc, char* argv[])
3030
{
3131
SyntaxAnalisator analisator;
32-
ifstream fileC;
33-
ofstream fileAnalysis("lexical.txt");
34-
fileC.exceptions(ifstream::badbit);
35-
try
36-
{
37-
fileC.open("C.txt");
38-
39-
if (fileC.is_open())
40-
{
41-
bool readComment = false;
42-
string temp = "";
43-
while (!fileC.eof())
44-
{
45-
string stringLanguageC = "";
46-
getline(fileC, stringLanguageC);
47-
for (unsigned int i = 0; i < stringLanguageC.length(); i++)
48-
{
49-
if (isServiceSymbols((int)stringLanguageC[i]) == true)
50-
continue;
51-
if (isComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
52-
readComment = true;
53-
if (readComment == false && isOneStringComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
54-
{
55-
string temp2 = "";
56-
temp2.assign(stringLanguageC, i, stringLanguageC.length() - i);
57-
fileAnalysis << temp2 << " ";
58-
break;
59-
}
60-
if (readComment == true && isComment((int)stringLanguageC[i + 1], (int)stringLanguageC[i]) == true)
61-
{
62-
readComment = false;
63-
temp += stringLanguageC[i];
64-
temp += stringLanguageC[i + 1];
65-
if (temp != "\0" && temp!= "/**/")
66-
fileAnalysis << temp << " ";
67-
temp = "";
68-
i++;
69-
continue;
70-
71-
}
72-
73-
if (readComment == false)
74-
{
75-
if (isSeparators((int)stringLanguageC[i]) == true && temp[0] != '\"')
76-
{
77-
if (temp.length() != 0)
78-
fileAnalysis << analisator.getCodeWord(temp) << " ";
79-
temp = stringLanguageC[i];
80-
fileAnalysis << analisator.getCodeWord(temp) << " ";
81-
temp = "";
82-
continue;
83-
}
84-
85-
// <library.h> and "string"
86-
if (stringLanguageC[i] == '<' || stringLanguageC[i] == '\"')
87-
{
88-
int posClose = 0;
89-
int countSymbols = 0;
90-
if (stringLanguageC[i] == '<')
91-
posClose = stringLanguageC.find(">", 1);
92-
else
93-
posClose = stringLanguageC.rfind('\"');
94-
95-
if (posClose != -1)
96-
{
97-
countSymbols = posClose + 1 - i;
98-
temp.assign(stringLanguageC, i, countSymbols);
99-
if (temp.find(".h") != -1)
100-
{
101-
fileAnalysis << analisator.getCodeWord(temp) << " ";
102-
temp = "";
103-
if (stringLanguageC[posClose + 1] == '\0')
104-
break;
105-
else
106-
i = posClose;
107-
}
108-
else
109-
{
110-
if (temp[0] == '\"')
111-
{
112-
fileAnalysis << analisator.getCodeWord(temp) << " ";
113-
i = posClose+1;
114-
115-
}
116-
}
117-
temp = "";
118-
}
119-
}
120-
121-
if (isOperation((int)stringLanguageC[i]) == true || isLogicalSingleOperation((int)stringLanguageC[i]) == true)
122-
{
123-
if (isIncrement((int)stringLanguageC[i], (int)stringLanguageC[i+1]) == true ||
124-
isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1] == true) ||
125-
isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
126-
{
127-
temp += stringLanguageC[i];
128-
i++;
129-
}
130-
temp += stringLanguageC[i];
131-
fileAnalysis << analisator.getCodeWord(temp) << " ";
132-
temp = "";
133-
continue;
134-
}
135-
136-
if (stringLanguageC[i] != ' ')
137-
{
138-
if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i + 1]) == false && isDigit((int)stringLanguageC[i + 1]) == false))
139-
{
140-
141-
temp += stringLanguageC[i];
142-
fileAnalysis << analisator.getCodeWord(temp) << " ";
143-
temp = "";
144-
continue;
145-
}
146-
else
147-
{
148-
if (stringLanguageC[i] == '#')
149-
{
150-
temp += stringLanguageC[i];
151-
continue;
152-
}
153-
154-
}
155-
temp += stringLanguageC[i];
156-
}
157-
else
158-
{
159-
if (temp == "\0")
160-
continue;
161-
else
162-
{
163-
fileAnalysis << analisator.getCodeWord(temp) << " ";
164-
temp = "";
165-
}
166-
}
167-
}
168-
else
169-
{
170-
temp += stringLanguageC[i];
171-
}
172-
173-
}
174-
if (temp != "\0")
175-
{
176-
if (readComment == false)
177-
fileAnalysis << analisator.getCodeWord(temp);
178-
else
179-
temp += '\n';
180-
}
181-
if(readComment==false)
182-
fileAnalysis << "\n";
183-
}
184-
}
185-
186-
}
187-
catch (const ifstream::failure& exep)
188-
{
189-
cout << " Exception opening/reading file";
190-
cout << exep.what();
191-
}
192-
193-
fileC.close();
194-
fileAnalysis.close();
32+
analisator.analyze("C.txt", "lexical.txt");
19533

19634
return 0;
19735
}

SyntaxAnalisator.cpp

Lines changed: 166 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "SyntaxAnalisator.h"
22
#include "function.h"
33

4-
54
SyntaxAnalisator::SyntaxAnalisator()
65
{
76
}
@@ -106,27 +105,6 @@ string SyntaxAnalisator::getCodeWordLength_1(string word)
106105
default:
107106
return "";
108107
}
109-
/*
110-
string code = getOperationsCode(word);
111-
if (code == "\0")
112-
code = getSeparatorsCode(word);
113-
if (code=="\0")
114-
{
115-
if (isDigit((int)word[0]) == true)
116-
{
117-
if (getNumberConstCode(word) == "\0")
118-
addCode(word, numberConst, 2);
119-
return getNumberConstCode(word);
120-
}
121-
if (isLetter((int)word[0]) == true)
122-
{
123-
if(getIdentifierCode(word) =="\0")
124-
addCode(word,identifier,1);
125-
return getIdentifierCode(word);
126-
}
127-
}
128-
else
129-
return code;*/
130108
}
131109

132110

@@ -169,4 +147,170 @@ string SyntaxAnalisator::getCodeWord(string word)
169147
return getCodeWordLength_1(word);
170148
else
171149
return getCodeWordLengthGreaterOne(word);
150+
}
151+
152+
void SyntaxAnalisator::analyze(string filePathOrName_C, string fileName_Path_SaveAnalis)
153+
{
154+
ifstream fileC;
155+
ofstream fileAnalysis(fileName_Path_SaveAnalis);
156+
fileC.exceptions(ifstream::badbit);
157+
try
158+
{
159+
fileC.open(filePathOrName_C);
160+
161+
if (fileC.is_open())
162+
{
163+
bool readComment = false;
164+
string temp = "";
165+
while (!fileC.eof())
166+
{
167+
string stringLanguageC = "";
168+
getline(fileC, stringLanguageC);
169+
for (unsigned int i = 0; i < stringLanguageC.length(); i++)
170+
{
171+
if (isServiceSymbols((int)stringLanguageC[i]) == true)
172+
continue;
173+
if (isComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
174+
readComment = true;
175+
if (readComment == false && isOneStringComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
176+
{
177+
string temp2 = "";
178+
temp2.assign(stringLanguageC, i, stringLanguageC.length() - i);
179+
fileAnalysis << temp2 << " ";
180+
break;
181+
}
182+
if (readComment == true && isComment((int)stringLanguageC[i + 1], (int)stringLanguageC[i]) == true)
183+
{
184+
readComment = false;
185+
temp += stringLanguageC[i];
186+
temp += stringLanguageC[i + 1];
187+
if (temp != "\0" && temp != "/**/")
188+
fileAnalysis << temp << " ";
189+
temp = "";
190+
i++;
191+
continue;
192+
}
193+
194+
if (readComment == false)
195+
{
196+
if (isSeparators((int)stringLanguageC[i]) == true && temp[0] != '\"')
197+
{
198+
if (temp.length() != 0)
199+
fileAnalysis << getCodeWord(temp) << " ";
200+
temp = stringLanguageC[i];
201+
fileAnalysis << getCodeWord(temp) << " ";
202+
temp = "";
203+
continue;
204+
}
205+
206+
// <library.h> and "string"
207+
if (stringLanguageC[i] == '<' || stringLanguageC[i] == '\"')
208+
{
209+
int posClose = 0;
210+
int countSymbols = 0;
211+
if (stringLanguageC[i] == '<')
212+
posClose = stringLanguageC.find(">", 1);
213+
else
214+
posClose = stringLanguageC.rfind('\"');
215+
216+
if (posClose != -1)
217+
{
218+
countSymbols = posClose + 1 - i;
219+
temp.assign(stringLanguageC, i, countSymbols);
220+
if (temp.find(".h") != -1)
221+
{
222+
fileAnalysis << getCodeWord(temp) << " ";
223+
temp = "";
224+
if (stringLanguageC[posClose + 1] == '\0')
225+
break;
226+
else
227+
i = posClose;
228+
}
229+
else
230+
{
231+
if (temp[0] == '\"')
232+
{
233+
fileAnalysis << getCodeWord(temp) << " ";
234+
i = posClose + 1;
235+
236+
}
237+
}
238+
temp = "";
239+
}
240+
}
241+
242+
if (isOperation((int)stringLanguageC[i]) == true || isLogicalSingleOperation((int)stringLanguageC[i]) == true)
243+
{
244+
if (isIncrement((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true ||
245+
isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1] == true) ||
246+
isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
247+
{
248+
temp += stringLanguageC[i];
249+
i++;
250+
}
251+
temp += stringLanguageC[i];
252+
fileAnalysis << getCodeWord(temp) << " ";
253+
temp = "";
254+
continue;
255+
}
256+
257+
if (stringLanguageC[i] != ' ')
258+
{
259+
if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i + 1]) == false && isDigit((int)stringLanguageC[i + 1]) == false))
260+
{
261+
262+
temp += stringLanguageC[i];
263+
fileAnalysis << getCodeWord(temp) << " ";
264+
temp = "";
265+
continue;
266+
}
267+
else
268+
{
269+
if (stringLanguageC[i] == '#')
270+
{
271+
temp += stringLanguageC[i];
272+
continue;
273+
}
274+
275+
}
276+
temp += stringLanguageC[i];
277+
}
278+
else
279+
{
280+
if (temp == "\0")
281+
continue;
282+
else
283+
{
284+
fileAnalysis << getCodeWord(temp) << " ";
285+
temp = "";
286+
}
287+
}
288+
}
289+
else
290+
{
291+
temp += stringLanguageC[i];
292+
}
293+
294+
}
295+
if (temp != "\0")
296+
{
297+
if (readComment == false)
298+
fileAnalysis << getCodeWord(temp);
299+
else
300+
temp += '\n';
301+
}
302+
if (readComment == false)
303+
fileAnalysis << "\n";
304+
}
305+
}
306+
307+
}
308+
catch (const ifstream::failure & exep)
309+
{
310+
cout << " Exception opening/reading file";
311+
cout << exep.what();
312+
}
313+
314+
fileC.close();
315+
fileAnalysis.close();
172316
}

SyntaxAnalisator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SyntaxAnalisator :
99
public:
1010
SyntaxAnalisator();
1111
~SyntaxAnalisator();
12-
string getCodeWord(string word);
12+
void analyze(string filePathOrName_C, string fileName_Path_SaveAnalis);
1313
private:
1414
string getServiceWordCode(string str);
1515
string getOperationsCode(string str);
@@ -21,5 +21,6 @@ class SyntaxAnalisator :
2121
int checkStringSingleElem(string const& word);
2222
string getCodeWordLength_1(string word);
2323
string getCodeWordLengthGreaterOne(string word);
24+
string getCodeWord(string word);
2425
};
2526
#endif

0 commit comments

Comments
 (0)