MethodsDevelopmentTranslator
/
LexicalAnalisator.cpp
322 строки · 8.1 Кб
1#include "Translator_LanguageC.h"
2#include "LexicalAnalisator.h"
3#include "function.h"
4
5LexicalAnalisator::LexicalAnalisator()
6{
7}
8
9
10LexicalAnalisator::~LexicalAnalisator()
11{
12}
13
14bool isEqual = false;
15
16void LexicalAnalisator::addCode(std::string str, std::map<std::string, std::string>& table, int numTable)
17{
18std::string result = fillTable(str, table, numTable);
19if (result.find("Error") != std::string::npos)
20{
21System::String^ temp = StlWStringToString(str);
22System::String^ tempResult = StlWStringToString(result);
23System::Windows::Forms::MessageBox::Show(tempResult, temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
24}
25}
26
27int LexicalAnalisator::checkStringSingleElem(std::string const& word)
28{
29if (isDigit((int)word[0]) == true)
30return 1;
31if (isOperation((int)word[0]) == true || isLogicalSingleOperation((int)word[0]) == true)
32return 2;
33if (isSeparators((int)word[0]) == true)
34return 3;
35if (isLetter((int)word[0]) == true)
36return 4;
37return 0;
38}
39
40std::string LexicalAnalisator::getCodeWordLength_1(std::string word)
41{
42switch (checkStringSingleElem(word))
43{
44case 1:
45if (getCodeByName(numberConst,word) == "\0")
46addCode(word, numberConst, 2);
47return getCodeByName(numberConst, word);
48case 2:
49return getOperations(word,true);
50case 3:
51return getSeparators(word,true);
52case 4:
53if (getCodeByName(identifier, word) == "\0")
54{
55if (isEqual == false)
56addCode(word, identifier, 1);
57else
58{
59problemDetected("Expected known identifier!");
60return "error!";
61}
62}
63return getCodeByName(identifier, word);
64default:
65return "";
66}
67}
68
69
70std::string LexicalAnalisator::getCodeWordLengthGreaterOne(std::string word)
71{
72std::string code = getServiceWord(word,true);
73if (code == "\0")
74code = getOperations(word,true);
75if (code == "\0")
76{
77if (isNumber(word) == true)
78{
79if (getCodeByName(numberConst, word) == "\0")
80addCode(word, numberConst, 2);
81return getCodeByName(numberConst, word);
82}
83else
84{
85if ((int)word[0] == 34)// \"
86{
87if (isLibrary_header(word) == false)
88{
89if (getCodeByName(symbolsConst, word) == "\0")
90addCode(word, symbolsConst, 3);
91return getCodeByName(symbolsConst, word);
92}
93}
94if (getCodeByName(identifier, word) == "\0")
95{
96if(isEqual == false)
97addCode(word, identifier, 1);
98else
99{
100problemDetected("Expected known identifier!");
101return "error!";
102}
103}
104return getCodeByName(identifier, word);
105}
106}
107else
108return code;
109}
110
111std::string LexicalAnalisator::getCodeWord(std::string word)
112{
113if (word.length() == 1)
114{
115std::string temp = getCodeWordLength_1(word);
116if (temp == "R7")
117isEqual = false;
118return temp;
119}
120else
121return getCodeWordLengthGreaterOne(word);
122}
123
124bool LexicalAnalisator::skipAnalyzeOneLineComment(bool readComment, std::string line, __int64 index,std::ofstream& file)
125{
126if (readComment == false && isOneStringComment((int)line[index], (int)line[index + 1]) == true)
127{
128std::string oneLineComment = "";
129oneLineComment.assign(line, index, line.length() - index);
130file << oneLineComment << " ";
131return true;
132}
133return false;
134}
135
136bool LexicalAnalisator::skipAnalyzeComment(bool& readComment, std::string line, __int64& index, std::ofstream& file, std::string& word)
137{
138if (readComment == true && isComment((int)line[index + 1], (int)line[index]) == true)
139{
140readComment = false;
141word += line[index];
142word += line[index + 1];
143if (word != "\0" && word != "")
144file << word << " ";
145word = "";
146index++;
147return true;
148}
149return false;
150}
151bool LexicalAnalisator::isLibrary_header(std::string const& word)
152{
153return (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
156void LexicalAnalisator::makeLexicalAnalyze(std::string filePathOrName_C, std::string fileName_Path_SaveAnalis)
157{
158std::ifstream fileC;
159std::ofstream fileAnalysis(fileName_Path_SaveAnalis);
160fileC.exceptions(std::ifstream::badbit);
161try
162{
163isEqual = false;
164fileC.open(filePathOrName_C);
165if (fileC.is_open())
166{
167bool readComment = false;
168std::string word = "";
169while (!fileC.eof())
170{
171std::string stringLanguageC = "";
172getline(fileC, stringLanguageC);
173for (__int64 i = 0; i < stringLanguageC.length(); i++)
174{
175if (isServiceSymbols((int)stringLanguageC[i]) == true)
176continue;
177if (isComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
178readComment = true;
179if (skipAnalyzeOneLineComment(readComment,stringLanguageC,i,fileAnalysis)==true)
180break;
181
182if (skipAnalyzeComment(readComment, stringLanguageC, i, fileAnalysis,word))
183continue;
184
185if (readComment == false)
186{
187if (isSeparators((int)stringLanguageC[i]) == true && word[0] != '\"')
188{
189if (word.length() != 0)
190fileAnalysis << getCodeWord(word) << " ";
191word = stringLanguageC[i];
192fileAnalysis << getCodeWord(word) << " ";
193word = "";
194continue;
195}
196
197// <library.h> and "string"
198if (stringLanguageC[i] == '<' || stringLanguageC[i] == '\"')
199{
200int posClose = 0;
201int countSymbols = 0;
202if (stringLanguageC[i] == '<')
203posClose = stringLanguageC.find(">", 1);
204else
205posClose = stringLanguageC.rfind('\"');
206
207if (posClose != -1)
208{
209countSymbols = posClose + 1 - i;
210word.assign(stringLanguageC, i, countSymbols);
211if (word.find(".h") != -1)
212{
213fileAnalysis << getCodeWord(word) << " ";
214word = "";
215if (stringLanguageC[static_cast<__int64>(posClose) + 1] == '\0')
216break;
217else
218i = posClose;
219}
220else
221{
222if (word[0] == '\"')
223{
224fileAnalysis << getCodeWord(word) << " ";
225i = static_cast<__int64>(posClose) + 1;
226
227}
228}
229word = "";
230}
231}
232
233if (isOperation((int)stringLanguageC[i]) == true || isLogicalSingleOperation((int)stringLanguageC[i]) == true)
234{
235if (isIncrement((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true ||
236isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true ||
237isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
238{
239word += stringLanguageC[i];
240i++;
241}
242word += stringLanguageC[i];
243if (word == "=")
244isEqual = true;
245fileAnalysis << getCodeWord(word) << " ";
246word = "";
247continue;
248}
249
250if (stringLanguageC[i] != ' ')
251{
252if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i + 1]) == false && isDigit((int)stringLanguageC[i + 1]) == false))
253{
254word += stringLanguageC[i];
255if (isTypeDeclaration(word) && (stringLanguageC[i + 1] == '*' || stringLanguageC[i + 2] == '*'))
256{
257word += '*';
258if (stringLanguageC[i + 2] == '*')
259i += 2;
260else
261i++;
262}
263std::string check_err = getCodeWord(word);
264if (check_err.find("error!")!=std::string::npos)
265return;
266
267
268fileAnalysis << check_err << " ";
269word = "";
270continue;
271}
272else
273{
274if (stringLanguageC[i] == '#')
275{
276word += stringLanguageC[i];
277continue;
278}
279
280}
281word += stringLanguageC[i];
282}
283else
284{
285if (word == "\0")
286continue;
287else
288{
289fileAnalysis << getCodeWord(word) << " ";
290word = "";
291}
292}
293}
294else
295{
296word += stringLanguageC[i];
297}
298
299}
300if (word != "\0")
301{
302if (readComment == false)
303fileAnalysis << getCodeWord(word);
304else
305word += '\n';
306}
307if (readComment == false)
308fileAnalysis << "\n";
309}
310}
311
312}
313catch (const std::ifstream::failure & exep)
314{
315std::cout << " Exception opening/reading file";
316std::cout << exep.what();
317System::Windows::Forms::MessageBox::Show("File don't open", "error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
318}
319
320fileC.close();
321fileAnalysis.close();
322}
323
324
325
326