Skip to content

Commit c1603f1

Browse files
#TDP - 26 Add class Syntax Analisatro
old syntax analisator rename to lexical analisator In new class add methods: makeSyntaxAnalyze setArguments operators operatorDeclareData operatorEqual operatorCondition operatorFor parseExpression checkToken(in: delegate function) getToken Predicate: isIdentifier,operation,symbol,number Translate class add method: isOpenAnyBracket isCloseAnyBracket
1 parent c48a7a6 commit c1603f1

15 files changed

+867
-317
lines changed

LexicalAnalisator.cpp

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

LexicalAnalisator.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
#ifndef SYNTAXANALISATOR_H
3+
#define SYNTAXANALISATOR_H
4+
5+
#include "Translator.h"
6+
7+
class LexicalAnalisator : public Translator
8+
{
9+
public:
10+
LexicalAnalisator();
11+
protected:
12+
void makeLexicalAnalyze(std::string from_file_C, std::string to_file_lexical);
13+
~LexicalAnalisator();
14+
private:
15+
void addCode(std::string str, std::map<std::string, std::string>& table, int numTable);
16+
int checkStringSingleElem(std::string const& word);
17+
std::string getCodeWordLength_1(std::string word);
18+
std::string getCodeWordLengthGreaterOne(std::string word);
19+
std::string getCodeWord(std::string word);
20+
bool skipAnalyzeComment(bool& readComment, std::string line, __int64& index, std::ofstream& file, std::string& word);
21+
bool skipAnalyzeOneLineComment(bool readComment, std::string line, __int64 index, std::ofstream& file);
22+
bool isLibrary_header(std::string const& word);
23+
};
24+
#endif

MethodsDevelopmentTranslator.vcxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<WarningLevel>Level3</WarningLevel>
103103
<Optimization>Disabled</Optimization>
104104
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
105+
<AdditionalOptions>/utf-8 %(AdditionalOptions)</AdditionalOptions>
105106
</ClCompile>
106107
<Link>
107108
<AdditionalDependencies />
@@ -137,6 +138,7 @@
137138
<ItemGroup>
138139
<ClCompile Include="function.cpp" />
139140
<ClCompile Include="ReversePolishNotation.cpp" />
141+
<ClCompile Include="LexicalAnalisator.cpp" />
140142
<ClCompile Include="SyntaxAnalisator.cpp" />
141143
<ClCompile Include="translate_csharp.cpp" />
142144
<ClCompile Include="Translator.cpp" />
@@ -147,6 +149,7 @@
147149
<ClInclude Include="function.h" />
148150
<ClInclude Include="include.h" />
149151
<ClInclude Include="ReversePolishNotation.h" />
152+
<ClInclude Include="LexicalAnalisator.h" />
150153
<ClInclude Include="SyntaxAnalisator.h" />
151154
<ClInclude Include="translate_csharp.h" />
152155
<ClInclude Include="Translator.h" />

MethodsDevelopmentTranslator.vcxproj.filters

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
<ClCompile Include="function.cpp">
2222
<Filter>Исходные файлы</Filter>
2323
</ClCompile>
24-
<ClCompile Include="SyntaxAnalisator.cpp">
25-
<Filter>Исходные файлы</Filter>
26-
</ClCompile>
2724
<ClCompile Include="Translator.cpp">
2825
<Filter>Исходные файлы</Filter>
2926
</ClCompile>
@@ -36,6 +33,12 @@
3633
<ClCompile Include="TranslatorFromCToCSharp.cpp">
3734
<Filter>Исходные файлы</Filter>
3835
</ClCompile>
36+
<ClCompile Include="LexicalAnalisator.cpp">
37+
<Filter>Исходные файлы</Filter>
38+
</ClCompile>
39+
<ClCompile Include="SyntaxAnalisator.cpp">
40+
<Filter>Исходные файлы</Filter>
41+
</ClCompile>
3942
</ItemGroup>
4043
<ItemGroup>
4144
<ClInclude Include="Translator_LanguageC.h">
@@ -47,9 +50,6 @@
4750
<ClInclude Include="include.h">
4851
<Filter>Файлы заголовков</Filter>
4952
</ClInclude>
50-
<ClInclude Include="SyntaxAnalisator.h">
51-
<Filter>Файлы заголовков</Filter>
52-
</ClInclude>
5353
<ClInclude Include="Translator.h">
5454
<Filter>Файлы заголовков</Filter>
5555
</ClInclude>
@@ -62,6 +62,12 @@
6262
<ClInclude Include="TranslatorFromCToCSharp.h">
6363
<Filter>Файлы заголовков</Filter>
6464
</ClInclude>
65+
<ClInclude Include="LexicalAnalisator.h">
66+
<Filter>Файлы заголовков</Filter>
67+
</ClInclude>
68+
<ClInclude Include="SyntaxAnalisator.h">
69+
<Filter>Файлы заголовков</Filter>
70+
</ClInclude>
6571
</ItemGroup>
6672
<ItemGroup>
6773
<Text Include="C.txt">

0 commit comments

Comments
 (0)