1+ #include " Translator_LanguageC.h"
12#include " SyntaxAnalisator.h"
23#include " function.h"
34
@@ -9,6 +10,14 @@ SyntaxAnalisator::SyntaxAnalisator()
910SyntaxAnalisator::~SyntaxAnalisator ()
1011{
1112}
13+
14+ System::String^ StlWStringToString(std::string const & os)
15+ {
16+ System::String^ str = gcnew System::String (os.c_str ());
17+ // String^ str = gcnew String("");
18+ return str;
19+ }
20+
1221std::string SyntaxAnalisator::getServiceWordCode (std::string str)
1322{
1423 for (int i = 0 ; i < SIZE_serviceWord; i++)
@@ -57,6 +66,7 @@ std::string SyntaxAnalisator::getSymbolsConstCode(std::string str)
5766 return " \0 " ;
5867}
5968
69+
6070void SyntaxAnalisator::addCode (std::string str, std::map<std::string, std::string> & table, int numTable)
6171{
6272 int indexCode = 0 ;
@@ -66,9 +76,28 @@ void SyntaxAnalisator::addCode(std::string str, std::map<std::string, std::strin
6676 }
6777 indexCode++;
6878 if (numTable == 1 )
69- table.insert (std::pair<std::string, std::string>(str, " I" + std::to_string (indexCode)));
79+ {
80+ if (isIdentifier (str)==true )
81+ table.insert (std::pair<std::string, std::string>(str, " I" + std::to_string (indexCode)));
82+ else
83+ {
84+
85+ System::String^ temp = StlWStringToString (str);
86+ System::Windows::Forms::MessageBox::Show (" Error with identifier" , temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
87+ return ;
88+ }
89+ }
7090 if (numTable == 2 )
71- table.insert (std::pair<std::string, std::string>(str, " N" + std::to_string (indexCode)));
91+ {
92+ if (isNumber (str)==true )
93+ table.insert (std::pair<std::string, std::string>(str, " N" + std::to_string (indexCode)));
94+ else
95+ {
96+ System::String^ temp = StlWStringToString (str);
97+ System::Windows::Forms::MessageBox::Show (" Error with number" , temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
98+ return ;
99+ }
100+ }
72101 if (numTable == 3 )
73102 table.insert (std::pair<std::string, std::string>(str, " C" + std::to_string (indexCode)));
74103}
@@ -149,6 +178,35 @@ std::string SyntaxAnalisator::getCodeWord(std::string word)
149178 return getCodeWordLengthGreaterOne (word);
150179}
151180
181+ bool SyntaxAnalisator::skipAnalyzeOneLineComment (bool readComment, std::string line, __int64 index,std::ofstream& file)
182+ {
183+ if (readComment == false && isOneStringComment ((int )line[index], (int )line[index + 1 ]) == true )
184+ {
185+ std::string oneLineComment = " " ;
186+ oneLineComment.assign (line, index, line.length () - index);
187+ file << oneLineComment << " " ;
188+ return true ;
189+ }
190+ return false ;
191+ }
192+
193+ bool skipAnalyzeComment (bool & readComment, std::string line, __int64& index, std::ofstream& file, std::string& word)
194+ {
195+ if (readComment == true && isComment ((int )line[index + 1 ], (int )line[index]) == true )
196+ {
197+ readComment = false ;
198+ word += line[index];
199+ word += line[index + 1 ];
200+ if (word != " \0 " && word != " " )
201+ file << word << " " ;
202+ word = " " ;
203+ index++;
204+ return true ;
205+ }
206+ return false ;
207+ }
208+
209+
152210void SyntaxAnalisator::analyze (std::string filePathOrName_C, std::string fileName_Path_SaveAnalis)
153211{
154212 std::ifstream fileC;
@@ -161,45 +219,32 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
161219 if (fileC.is_open ())
162220 {
163221 bool readComment = false ;
164- std::string temp = " " ;
222+ std::string word = " " ;
165223 while (!fileC.eof ())
166224 {
167225 std::string stringLanguageC = " " ;
168226 getline (fileC, stringLanguageC);
169- for (unsigned int i = 0 ; i < stringLanguageC.length (); i++)
227+ for (__int64 i = 0 ; i < stringLanguageC.length (); i++)
170228 {
171229 if (isServiceSymbols ((int )stringLanguageC[i]) == true )
172230 continue ;
173231 if (isComment ((int )stringLanguageC[i], (int )stringLanguageC[i + 1 ]) == true )
174232 readComment = true ;
175- if (readComment == false && isOneStringComment ((int )stringLanguageC[i], (int )stringLanguageC[i + 1 ]) == true )
176- {
177- std::string oneLineComment = " " ;
178- oneLineComment.assign (stringLanguageC, i, stringLanguageC.length () - i);
179- fileAnalysis << oneLineComment << " " ;
233+ if (skipAnalyzeOneLineComment (readComment,stringLanguageC,i,fileAnalysis)==true )
180234 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++;
235+
236+ if (skipAnalyzeComment (readComment, stringLanguageC, i, fileAnalysis,word))
191237 continue ;
192- }
193238
194239 if (readComment == false )
195240 {
196- if (isSeparators ((int )stringLanguageC[i]) == true && temp [0 ] != ' \" ' )
241+ if (isSeparators ((int )stringLanguageC[i]) == true && word [0 ] != ' \" ' )
197242 {
198- if (temp .length () != 0 )
199- fileAnalysis << getCodeWord (temp ) << " " ;
200- temp = stringLanguageC[i];
201- fileAnalysis << getCodeWord (temp ) << " " ;
202- temp = " " ;
243+ if (word .length () != 0 )
244+ fileAnalysis << getCodeWord (word ) << " " ;
245+ word = stringLanguageC[i];
246+ fileAnalysis << getCodeWord (word ) << " " ;
247+ word = " " ;
203248 continue ;
204249 }
205250
@@ -216,26 +261,26 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
216261 if (posClose != -1 )
217262 {
218263 countSymbols = posClose + 1 - i;
219- temp .assign (stringLanguageC, i, countSymbols);
220- if (temp .find (" .h" ) != -1 )
264+ word .assign (stringLanguageC, i, countSymbols);
265+ if (word .find (" .h" ) != -1 )
221266 {
222- fileAnalysis << getCodeWord (temp ) << " " ;
223- temp = " " ;
224- if (stringLanguageC[posClose + 1 ] == ' \0 ' )
267+ fileAnalysis << getCodeWord (word ) << " " ;
268+ word = " " ;
269+ if (stringLanguageC[static_cast <__int64>( posClose) + 1 ] == ' \0 ' )
225270 break ;
226271 else
227272 i = posClose;
228273 }
229274 else
230275 {
231- if (temp [0 ] == ' \" ' )
276+ if (word [0 ] == ' \" ' )
232277 {
233- fileAnalysis << getCodeWord (temp ) << " " ;
234- i = posClose + 1 ;
278+ fileAnalysis << getCodeWord (word ) << " " ;
279+ i = static_cast <__int64>( posClose) + 1 ;
235280
236281 }
237282 }
238- temp = " " ;
283+ word = " " ;
239284 }
240285 }
241286
@@ -245,64 +290,63 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
245290 isDoubleOperation ((int )stringLanguageC[i], (int )stringLanguageC[i + 1 ] == true ) ||
246291 isLogicalDoubleOperation ((int )stringLanguageC[i], (int )stringLanguageC[i + 1 ]) == true )
247292 {
248- temp += stringLanguageC[i];
293+ word += stringLanguageC[i];
249294 i++;
250295 }
251- temp += stringLanguageC[i];
252- fileAnalysis << getCodeWord (temp ) << " " ;
253- temp = " " ;
296+ word += stringLanguageC[i];
297+ fileAnalysis << getCodeWord (word ) << " " ;
298+ word = " " ;
254299 continue ;
255300 }
256301
257302 if (stringLanguageC[i] != ' ' )
258303 {
259304 if (isLetter ((int )stringLanguageC[i]) == true && (isLetter ((int )stringLanguageC[i + 1 ]) == false && isDigit ((int )stringLanguageC[i + 1 ]) == false ))
260305 {
261-
262- temp += stringLanguageC[i];
263- if ((temp == " int" || temp == " float" || temp == " double" || temp == " char" ) && stringLanguageC[i + 1 ] == ' *' )
306+ word += stringLanguageC[i];
307+ if (isType (word) && stringLanguageC[i + 1 ] == ' *' )
264308 {
265- temp += stringLanguageC[i + 1 ];
309+ word += stringLanguageC[i + 1 ];
266310 i++;
267311 }
268- fileAnalysis << getCodeWord (temp ) << " " ;
269- temp = " " ;
312+ fileAnalysis << getCodeWord (word ) << " " ;
313+ word = " " ;
270314 continue ;
271315 }
272316 else
273317 {
274318 if (stringLanguageC[i] == ' #' )
275319 {
276- temp += stringLanguageC[i];
320+ word += stringLanguageC[i];
277321 continue ;
278322 }
279323
280324 }
281- temp += stringLanguageC[i];
325+ word += stringLanguageC[i];
282326 }
283327 else
284328 {
285- if (temp == " \0 " )
329+ if (word == " \0 " )
286330 continue ;
287331 else
288332 {
289- fileAnalysis << getCodeWord (temp ) << " " ;
290- temp = " " ;
333+ fileAnalysis << getCodeWord (word ) << " " ;
334+ word = " " ;
291335 }
292336 }
293337 }
294338 else
295339 {
296- temp += stringLanguageC[i];
340+ word += stringLanguageC[i];
297341 }
298342
299343 }
300- if (temp != " \0 " )
344+ if (word != " \0 " )
301345 {
302346 if (readComment == false )
303- fileAnalysis << getCodeWord (temp );
347+ fileAnalysis << getCodeWord (word );
304348 else
305- temp += ' \n ' ;
349+ word += ' \n ' ;
306350 }
307351 if (readComment == false )
308352 fileAnalysis << " \n " ;
@@ -314,6 +358,7 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
314358 {
315359 std::cout << " Exception opening/reading file" ;
316360 std::cout << exep.what ();
361+ System::Windows::Forms::MessageBox::Show (" File don't open" , " error" , System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
317362 }
318363
319364 fileC.close ();
0 commit comments