MethodsDevelopmentTranslator

Форк
0
/
LexicalAnalisator.cpp 
322 строки · 8.1 Кб
1
#include "Translator_LanguageC.h"
2
#include "LexicalAnalisator.h"
3
#include "function.h"
4

5
LexicalAnalisator::LexicalAnalisator()
6
{
7
}
8

9

10
LexicalAnalisator::~LexicalAnalisator()
11
{
12
}
13

14
bool isEqual = false;
15

16
void LexicalAnalisator::addCode(std::string str, std::map<std::string, std::string>& table, int numTable)
17
{
18
	std::string result = fillTable(str, table, numTable);
19
	if (result.find("Error") != std::string::npos)
20
	{
21
		System::String^ temp = StlWStringToString(str);
22
		System::String^ tempResult = StlWStringToString(result);
23
		System::Windows::Forms::MessageBox::Show(tempResult, temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
24
	}
25
}
26

27
int LexicalAnalisator::checkStringSingleElem(std::string const& word)
28
{
29
	if (isDigit((int)word[0]) == true)
30
		return 1;
31
	if (isOperation((int)word[0]) == true || isLogicalSingleOperation((int)word[0]) == true)
32
		return 2;
33
	if (isSeparators((int)word[0]) == true)
34
		return 3;
35
	if (isLetter((int)word[0]) == true)
36
		return 4;
37
	return 0;
38
}
39

40
std::string LexicalAnalisator::getCodeWordLength_1(std::string word)
41
{
42
	switch (checkStringSingleElem(word))
43
	{
44
	case 1:
45
		if (getCodeByName(numberConst,word) == "\0")
46
			addCode(word, numberConst, 2);
47
		return getCodeByName(numberConst, word);
48
	case 2:
49
		return getOperations(word,true);
50
	case 3:
51
		return getSeparators(word,true);
52
	case 4:
53
		if (getCodeByName(identifier, word) == "\0")
54
		{
55
			if (isEqual == false)
56
				addCode(word, identifier, 1);
57
			else
58
			{
59
				problemDetected("Expected known identifier!");
60
				return "error!";
61
			}
62
		}
63
		return getCodeByName(identifier, word);
64
	default:
65
		return "";
66
	}
67
}
68

69

70
std::string LexicalAnalisator::getCodeWordLengthGreaterOne(std::string word)
71
{
72
	std::string code = getServiceWord(word,true);
73
	if (code == "\0")
74
		code = getOperations(word,true);
75
	if (code == "\0")
76
	{
77
		if (isNumber(word) == true)
78
		{
79
			if (getCodeByName(numberConst, word) == "\0")
80
				addCode(word, numberConst, 2);
81
			return getCodeByName(numberConst, word);
82
		}
83
		else
84
		{
85
			if ((int)word[0] == 34)// \"
86
			{
87
				if (isLibrary_header(word) == false)
88
				{
89
					if (getCodeByName(symbolsConst, word) == "\0")
90
						addCode(word, symbolsConst, 3);
91
					return getCodeByName(symbolsConst, word);
92
				}
93
			}
94
			if (getCodeByName(identifier, word) == "\0")
95
			{
96
				if(isEqual == false)
97
					addCode(word, identifier, 1);
98
				else
99
				{
100
					problemDetected("Expected known identifier!");
101
					return "error!";
102
				}
103
			}
104
			return getCodeByName(identifier, word);
105
		}
106
	}
107
	else
108
		return code;
109
}
110

111
std::string LexicalAnalisator::getCodeWord(std::string word)
112
{
113
	if (word.length() == 1)
114
	{
115
		std::string temp = getCodeWordLength_1(word);
116
		if (temp == "R7")
117
			isEqual = false;
118
		return temp;
119
	}
120
	else
121
		return getCodeWordLengthGreaterOne(word);
122
}
123

124
bool LexicalAnalisator::skipAnalyzeOneLineComment(bool readComment, std::string line, __int64 index,std::ofstream& file)
125
{
126
	if (readComment == false && isOneStringComment((int)line[index], (int)line[index + 1]) == true)
127
	{
128
		std::string oneLineComment = "";
129
		oneLineComment.assign(line, index, line.length() - index);
130
		file << oneLineComment << " ";
131
		return true;
132
	}
133
	return false;
134
}
135

136
bool LexicalAnalisator::skipAnalyzeComment(bool& readComment, std::string line, __int64& index, std::ofstream& file, std::string& word)
137
{
138
	if (readComment == true && isComment((int)line[index + 1], (int)line[index]) == true)
139
	{
140
		readComment = false;
141
		word += line[index];
142
		word += line[index + 1];
143
		if (word != "\0" && word != "")
144
			file << word << " ";
145
		word = "";
146
		index++;
147
		return true;
148
	}
149
	return false;
150
}
151
bool LexicalAnalisator::isLibrary_header(std::string const& word)
152
{
153
	return (int)word[0] == 34 && (int)word[word.length() - 1] == 34 && (int)word[word.length() - 2] == 104 && (int)word[word.length() - 3] == 46 ? true : false;
154
}
155

156
void LexicalAnalisator::makeLexicalAnalyze(std::string filePathOrName_C, std::string fileName_Path_SaveAnalis)
157
{
158
	std::ifstream fileC;
159
	std::ofstream fileAnalysis(fileName_Path_SaveAnalis);
160
	fileC.exceptions(std::ifstream::badbit);
161
	try
162
	{
163
		isEqual = false;
164
		fileC.open(filePathOrName_C);
165
		if (fileC.is_open())
166
		{
167
			bool readComment = false;
168
			std::string word = "";
169
			while (!fileC.eof())
170
			{
171
				std::string stringLanguageC = "";
172
				getline(fileC, stringLanguageC);
173
				for (__int64 i = 0; i < stringLanguageC.length(); i++)
174
				{
175
					if (isServiceSymbols((int)stringLanguageC[i]) == true)
176
						continue;
177
					if (isComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
178
						readComment = true;
179
					if (skipAnalyzeOneLineComment(readComment,stringLanguageC,i,fileAnalysis)==true)
180
						break;
181

182
					if (skipAnalyzeComment(readComment, stringLanguageC, i, fileAnalysis,word))
183
						continue;
184

185
					if (readComment == false)
186
					{
187
						if (isSeparators((int)stringLanguageC[i]) == true && word[0] != '\"')
188
						{
189
							if (word.length() != 0)
190
								fileAnalysis << getCodeWord(word) << " ";
191
							word = stringLanguageC[i];
192
							fileAnalysis << getCodeWord(word) << " ";
193
							word = "";
194
							continue;
195
						}
196

197
						// <library.h> and "string"
198
						if (stringLanguageC[i] == '<' || stringLanguageC[i] == '\"')
199
						{
200
							int posClose = 0;
201
							int countSymbols = 0;
202
							if (stringLanguageC[i] == '<')
203
								posClose = stringLanguageC.find(">", 1);
204
							else
205
								posClose = stringLanguageC.rfind('\"');
206

207
							if (posClose != -1)
208
							{
209
								countSymbols = posClose + 1 - i;
210
								word.assign(stringLanguageC, i, countSymbols);
211
								if (word.find(".h") != -1)
212
								{
213
									fileAnalysis << getCodeWord(word) << " ";
214
									word = "";
215
									if (stringLanguageC[static_cast<__int64>(posClose) + 1] == '\0')
216
										break;
217
									else
218
										i = posClose;
219
								}
220
								else
221
								{
222
									if (word[0] == '\"')
223
									{
224
										fileAnalysis << getCodeWord(word) << " ";
225
										i = static_cast<__int64>(posClose) + 1;
226

227
									}
228
								}
229
								word = "";
230
							}
231
						}
232

233
						if (isOperation((int)stringLanguageC[i]) == true || isLogicalSingleOperation((int)stringLanguageC[i]) == true)
234
						{
235
							if (isIncrement((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true ||
236
								isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true ||
237
								isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
238
							{
239
								word += stringLanguageC[i];
240
								i++;
241
							}
242
							word += stringLanguageC[i];
243
							if (word == "=")
244
								isEqual = true;
245
							fileAnalysis << getCodeWord(word) << " ";
246
							word = "";
247
							continue;
248
						}
249

250
						if (stringLanguageC[i] != ' ')
251
						{
252
							if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i + 1]) == false && isDigit((int)stringLanguageC[i + 1]) == false))
253
							{
254
								word += stringLanguageC[i];
255
								if (isTypeDeclaration(word) && (stringLanguageC[i + 1] == '*' || stringLanguageC[i + 2] == '*'))
256
								{
257
									word += '*';
258
									if (stringLanguageC[i + 2] == '*')
259
										i += 2;
260
									else
261
										i++;
262
								}
263
								std::string check_err = getCodeWord(word);
264
								if (check_err.find("error!")!=std::string::npos)
265
									return;
266

267

268
								fileAnalysis << check_err << " ";
269
								word = "";
270
								continue;
271
							}
272
							else
273
							{
274
								if (stringLanguageC[i] == '#')
275
								{
276
									word += stringLanguageC[i];
277
									continue;
278
								}
279

280
							}
281
							word += stringLanguageC[i];
282
						}
283
						else
284
						{
285
							if (word == "\0")
286
								continue;
287
							else
288
							{
289
								fileAnalysis << getCodeWord(word) << " ";
290
								word = "";
291
							}
292
						}
293
					}
294
					else
295
					{
296
						word += stringLanguageC[i];
297
					}
298

299
				}
300
				if (word != "\0")
301
				{
302
					if (readComment == false)
303
						fileAnalysis << getCodeWord(word);
304
					else
305
						word += '\n';
306
				}
307
				if (readComment == false)
308
					fileAnalysis << "\n";
309
			}
310
		}
311

312
	}
313
	catch (const std::ifstream::failure & exep)
314
	{
315
		std::cout << " Exception opening/reading file";
316
		std::cout << exep.what();
317
		System::Windows::Forms::MessageBox::Show("File don't open", "error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
318
	}
319

320
	fileC.close();
321
	fileAnalysis.close();
322
}
323

324

325

326

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.