Skip to content

Commit 2a07756

Browse files
#TDP - 13 Class
Add Abstract class Translator add heir class Syntax analisator File function.cpp consist of base function - predicat
1 parent 518ef6d commit 2a07756

11 files changed

+321
-178
lines changed

MethodsDevelopmentTranslator.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "function.h"
2+
#include "SyntaxAnalisator.h"
23

34
using namespace std;
45

@@ -27,6 +28,7 @@ using namespace std;
2728

2829
int main(int argc, char* argv[])
2930
{
31+
SyntaxAnalisator analisator;
3032
ifstream fileC;
3133
ofstream fileAnalysis("lexical.txt");
3234
fileC.exceptions(ifstream::badbit);
@@ -73,9 +75,9 @@ int main(int argc, char* argv[])
7375
if (isSeparators((int)stringLanguageC[i]) == true && temp[0] != '\"')
7476
{
7577
if (temp.length() != 0)
76-
fileAnalysis << getCodeWord(temp) << " ";
78+
fileAnalysis << analisator.getCodeWord(temp) << " ";
7779
temp = stringLanguageC[i];
78-
fileAnalysis << getCodeWord(temp) << " ";
80+
fileAnalysis << analisator.getCodeWord(temp) << " ";
7981
temp = "";
8082
continue;
8183
}
@@ -96,7 +98,7 @@ int main(int argc, char* argv[])
9698
temp.assign(stringLanguageC, i, countSymbols);
9799
if (temp.find(".h") != -1)
98100
{
99-
fileAnalysis << getCodeWord(temp) << " ";
101+
fileAnalysis << analisator.getCodeWord(temp) << " ";
100102
temp = "";
101103
if (stringLanguageC[posClose + 1] == '\0')
102104
break;
@@ -107,8 +109,8 @@ int main(int argc, char* argv[])
107109
{
108110
if (temp[0] == '\"')
109111
{
110-
fileAnalysis << getCodeWord(temp) << " ";
111-
i = posClose;
112+
fileAnalysis << analisator.getCodeWord(temp) << " ";
113+
i = posClose+1;
112114

113115
}
114116
}
@@ -126,7 +128,7 @@ int main(int argc, char* argv[])
126128
i++;
127129
}
128130
temp += stringLanguageC[i];
129-
fileAnalysis << getCodeWord(temp) << " ";
131+
fileAnalysis << analisator.getCodeWord(temp) << " ";
130132
temp = "";
131133
continue;
132134
}
@@ -137,7 +139,7 @@ int main(int argc, char* argv[])
137139
{
138140

139141
temp += stringLanguageC[i];
140-
fileAnalysis << getCodeWord(temp) << " ";
142+
fileAnalysis << analisator.getCodeWord(temp) << " ";
141143
temp = "";
142144
continue;
143145
}
@@ -158,7 +160,7 @@ int main(int argc, char* argv[])
158160
continue;
159161
else
160162
{
161-
fileAnalysis << getCodeWord(temp) << " ";
163+
fileAnalysis << analisator.getCodeWord(temp) << " ";
162164
temp = "";
163165
}
164166
}
@@ -172,7 +174,7 @@ int main(int argc, char* argv[])
172174
if (temp != "\0")
173175
{
174176
if (readComment == false)
175-
fileAnalysis << getCodeWord(temp);
177+
fileAnalysis << analisator.getCodeWord(temp);
176178
else
177179
temp += '\n';
178180
}
@@ -185,6 +187,7 @@ int main(int argc, char* argv[])
185187
catch (const ifstream::failure& exep)
186188
{
187189
cout << " Exception opening/reading file";
190+
cout << exep.what();
188191
}
189192

190193
fileC.close();

MethodsDevelopmentTranslator.vcxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,19 @@
153153
<ItemGroup>
154154
<ClCompile Include="function.cpp" />
155155
<ClCompile Include="MethodsDevelopmentTranslator.cpp" />
156+
<ClCompile Include="SyntaxAnalisator.cpp" />
157+
<ClCompile Include="Translator.cpp" />
156158
</ItemGroup>
157159
<ItemGroup>
158160
<Text Include="C.txt" />
161+
<Text Include="lexical.txt" />
159162
</ItemGroup>
160163
<ItemGroup>
161164
<ClInclude Include="function.h" />
165+
<ClInclude Include="SyntaxAnalisator.h" />
162166
<ClInclude Include="table.h" />
163167
<ClInclude Include="include.h" />
168+
<ClInclude Include="Translator.h" />
164169
</ItemGroup>
165170
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
166171
<ImportGroup Label="ExtensionTargets">

MethodsDevelopmentTranslator.vcxproj.filters

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@
2121
<ClCompile Include="function.cpp">
2222
<Filter>Исходные файлы</Filter>
2323
</ClCompile>
24+
<ClCompile Include="Translator.cpp">
25+
<Filter>Исходные файлы</Filter>
26+
</ClCompile>
27+
<ClCompile Include="SyntaxAnalisator.cpp">
28+
<Filter>Исходные файлы</Filter>
29+
</ClCompile>
2430
</ItemGroup>
2531
<ItemGroup>
2632
<Text Include="C.txt">
2733
<Filter>Исходные файлы</Filter>
2834
</Text>
35+
<Text Include="lexical.txt">
36+
<Filter>Файлы ресурсов</Filter>
37+
</Text>
2938
</ItemGroup>
3039
<ItemGroup>
3140
<ClInclude Include="include.h">
@@ -37,5 +46,11 @@
3746
<ClInclude Include="function.h">
3847
<Filter>Файлы заголовков</Filter>
3948
</ClInclude>
49+
<ClInclude Include="Translator.h">
50+
<Filter>Файлы заголовков</Filter>
51+
</ClInclude>
52+
<ClInclude Include="SyntaxAnalisator.h">
53+
<Filter>Файлы заголовков</Filter>
54+
</ClInclude>
4055
</ItemGroup>
4156
</Project>

SyntaxAnalisator.cpp

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#include "SyntaxAnalisator.h"
2+
#include "function.h"
3+
4+
5+
SyntaxAnalisator::SyntaxAnalisator()
6+
{
7+
}
8+
9+
10+
SyntaxAnalisator::~SyntaxAnalisator()
11+
{
12+
}
13+
string SyntaxAnalisator::getServiceWordCode(string str)
14+
{
15+
for (int i = 0; i < SIZE_serviceWord; i++)
16+
if (serviceWord[i][0] == str)
17+
return serviceWord[i][1];
18+
return "\0";
19+
}
20+
21+
string SyntaxAnalisator::getOperationsCode(string str)
22+
{
23+
for (int i = 0; i < SIZE_operation; i++)
24+
if (operations[i][0] == str)
25+
return operations[i][1];
26+
return "\0";
27+
}
28+
29+
string SyntaxAnalisator::getSeparatorsCode(string str)
30+
{
31+
for (int i = 0; i < SIZE_separators; i++)
32+
if (separators[i][0] == str)
33+
return separators[i][1];
34+
return "\0";
35+
}
36+
37+
string SyntaxAnalisator::getIdentifierCode(string str)
38+
{
39+
for (const auto& word : identifier)
40+
if (word.first == str)
41+
return word.second;
42+
return "\0";
43+
}
44+
45+
string SyntaxAnalisator::getNumberConstCode(string str)
46+
{
47+
for (const auto& word : numberConst)
48+
if (word.first == str)
49+
return word.second;
50+
return "\0";
51+
}
52+
53+
string SyntaxAnalisator::getSymbolsConstCode(string str)
54+
{
55+
for (const auto& word : symbolsConst)
56+
if (word.first == str)
57+
return word.second;
58+
return "\0";
59+
}
60+
61+
void SyntaxAnalisator::addCode(string str, map<string, string> & table, int numTable)
62+
{
63+
int indexCode = 0;
64+
for (const auto& word : table)
65+
{
66+
indexCode++;
67+
}
68+
indexCode++;
69+
if (numTable == 1)
70+
table.insert(pair<string, string>(str, "I" + to_string(indexCode)));
71+
if (numTable == 2)
72+
table.insert(pair<string, string>(str, "N" + to_string(indexCode)));
73+
if (numTable == 3)
74+
table.insert(pair<string, string>(str, "C" + to_string(indexCode)));
75+
}
76+
77+
int SyntaxAnalisator::checkStringSingleElem(string const& word)
78+
{
79+
if (isDigit((int)word[0]) == true)
80+
return 1;
81+
if (isOperation((int)word[0]) == true || isLogicalSingleOperation((int)word[0]) == true)
82+
return 2;
83+
if (isSeparators((int)word[0]) == true)
84+
return 3;
85+
if (isLetter((int)word[0]) == true)
86+
return 4;
87+
return 0;
88+
}
89+
90+
string SyntaxAnalisator::getCodeWordLength_1(string word)
91+
{
92+
switch (checkStringSingleElem(word))
93+
{
94+
case 1:
95+
if (getNumberConstCode(word) == "\0")
96+
addCode(word, numberConst, 2);
97+
return getNumberConstCode(word);
98+
case 2:
99+
return getOperationsCode(word);
100+
case 3:
101+
return getSeparatorsCode(word);
102+
case 4:
103+
if (getIdentifierCode(word) == "\0")
104+
addCode(word, identifier, 1);
105+
return getIdentifierCode(word);
106+
default:
107+
return "";
108+
}
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;*/
130+
}
131+
132+
133+
string SyntaxAnalisator::getCodeWordLengthGreaterOne(string word)
134+
{
135+
string code = getServiceWordCode(word);
136+
if (code == "\0")
137+
code = getOperationsCode(word);
138+
if (code == "\0")
139+
{
140+
if (isNumber(word) == true)
141+
{
142+
if (getNumberConstCode(word) == "\0")
143+
addCode(word, numberConst, 2);
144+
return getNumberConstCode(word);
145+
}
146+
else
147+
{
148+
if ((int)word[0] == 34)// \"
149+
{
150+
if (isLibrary_header(word) == false)
151+
{
152+
if (getSymbolsConstCode(word) == "\0")
153+
addCode(word, symbolsConst, 3);
154+
return getSymbolsConstCode(word);
155+
}
156+
}
157+
if (getIdentifierCode(word) == "\0")
158+
addCode(word, identifier, 1);
159+
return getIdentifierCode(word);
160+
}
161+
}
162+
else
163+
return code;
164+
}
165+
166+
string SyntaxAnalisator::getCodeWord(string word)
167+
{
168+
if (word.length() == 1)
169+
return getCodeWordLength_1(word);
170+
else
171+
return getCodeWordLengthGreaterOne(word);
172+
}

SyntaxAnalisator.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#ifndef SYNTAXANALISATOR_H
3+
#define SYNTAXANALISATOR_H
4+
5+
#include "Translator.h"
6+
class SyntaxAnalisator :
7+
public Translator
8+
{
9+
public:
10+
SyntaxAnalisator();
11+
~SyntaxAnalisator();
12+
string getCodeWord(string word);
13+
private:
14+
string getServiceWordCode(string str);
15+
string getOperationsCode(string str);
16+
string getSeparatorsCode(string str);
17+
string getIdentifierCode(string str);
18+
string getNumberConstCode(string str);
19+
string getSymbolsConstCode(string str);
20+
void addCode(string str, map<string, string>& table, int numTable);
21+
int checkStringSingleElem(string const& word);
22+
string getCodeWordLength_1(string word);
23+
string getCodeWordLengthGreaterOne(string word);
24+
};
25+
#endif

Translator.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "Translator.h"
2+
3+
Translator::Translator()
4+
{
5+
}
6+
7+
8+
Translator::~Translator()
9+
{
10+
}

0 commit comments

Comments
 (0)