Skip to content

Commit 3433607

Browse files
#TDP-8 Modify
1. Modify method getCode 2. Modify read file and write to file
1 parent fe3ced3 commit 3433607

5 files changed

Lines changed: 149 additions & 24 deletions

File tree

C.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ int main(int argc, const char* argv[])
2929
{
3030
int size = sizeNumber(36);
3131
int *digitsNumber = setDigitNumber(36);
32-
int sumDigit = 0;
32+
int sum7Digit7 = 0;
3333
for (int i = 0; i < size; i++)
3434
{
3535
printf("array[%d] = %d \n", i, digitsNumber[i]);
36-
sumDigit += digitsNumber[i];
36+
sum7Digit7 += digitsNumber[i];
3737
}
38-
printf("Summa digit: %d", sumDigit);
38+
printf("Summa digit: %d", sum7Digit7);
3939
free(digitsNumber);
4040
return 0;
4141
}

MethodsDevelopmentTranslator.cpp

Lines changed: 129 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ using namespace std;
2525
Cycle: while() {..}
2626
*/
2727

28+
bool isSeparators(int elem)
29+
{
30+
return elem == 40 || elem == 41 || elem == 91 || elem == 93 || elem == 123 || elem == 125 || elem == 59 ? true : false;
31+
}
32+
33+
//\t\a\b...
34+
bool isServiceSymbols(int elem)
35+
{
36+
return elem == 7 || elem == 8 || elem == 9 || elem == 10 || elem == 11 || elem == 12 || elem == 13 ? true : false;
37+
}
2838

2939
int main()
3040
{
3141
ifstream fileC;
32-
ofstream fileAnalysis;
42+
ofstream fileAnalysis("lexical.txt");
3343
fileC.exceptions(ifstream::badbit);
3444
try
3545
{
@@ -42,23 +52,112 @@ int main()
4252
string stringC = "";
4353
string temp = "";
4454
getline(fileC, stringC);
45-
for (int i = 0; i < stringC.length()-1; i++)
55+
for (int i = 0; i < stringC.length(); i++)
4656
{
47-
if (i != 0 && stringC[i] == '*' && isLetter((int)stringC[i - 1]) == true)
48-
fileAnalysis << getCodeWord(stringC[i-1]+"*");
49-
if (stringC[i] != ' ')
57+
if (isServiceSymbols((int)stringC[i]) == true)
58+
continue;
59+
60+
if (isSeparators((int)stringC[i]) == true && temp[0] != '\"')
5061
{
51-
if (isLetter((int)stringC[i]) == true && (isLetter((int)stringC[i+1])==false || isDigit((int)stringC[i+1])==false))
62+
if (temp.length() != 0)
63+
fileAnalysis << getCodeWord(temp) << " ";
64+
temp = stringC[i];
65+
fileAnalysis << getCodeWord(temp) << " ";
66+
temp = "";
67+
continue;
68+
}
69+
70+
// <library.h>
71+
if (stringC[i] == '<' || stringC[i] == '\"')
72+
{
73+
int posClose = 0;
74+
int countSymbols = 0;
75+
if (stringC[i] == '<')
76+
{
77+
posClose = stringC.find(">", 1);
78+
if (posClose != -1)
79+
{
80+
countSymbols = stringC.length() - i;
81+
temp.assign(stringC, i, countSymbols);
82+
}
83+
84+
}
85+
else
86+
{
87+
posClose = stringC.rfind('\"');
88+
if (posClose != -1)
89+
{
90+
countSymbols = posClose+1 - i;
91+
temp.assign(stringC, i, countSymbols);
92+
}
93+
94+
}
95+
if (temp.find(".h") != -1)
5296
{
53-
temp += stringC[i];
5497
fileAnalysis << getCodeWord(temp) << " ";
5598
temp = "";
99+
100+
}
101+
else
102+
{
103+
if (temp[0] == '\"')
104+
{
105+
if (temp != "" && (int)temp[temp.length() - 2] != 92)
106+
{
107+
fileAnalysis << getCodeWord(temp) << " ";
108+
}
109+
}
110+
else
111+
{
112+
string temp2 = "";
113+
temp2 += stringC[i];
114+
fileAnalysis << getCodeWord(temp2) << " ";
115+
}
116+
117+
temp = "";
118+
if (stringC[posClose + 1] == '\0')
119+
break;
120+
else
121+
i = posClose;
56122
continue;
57123
}
124+
}
125+
126+
if (stringC[i] != ' ')
127+
{
128+
if (isLetter((int)stringC[i]) == true && (isLetter((int)stringC[i+1])==false && isDigit((int)stringC[i+1])==false))
129+
{
130+
if (temp[0] == '\"')
131+
{
132+
if (stringC[i] == '\"' && stringC[i + 1] == ' ' && stringC[i + 1] == '\0')
133+
{
134+
temp += stringC[i];
135+
fileAnalysis << getCodeWord(temp) << " ";
136+
temp = "";
137+
continue;
138+
}
139+
else
140+
{
141+
temp += stringC[i];
142+
continue;
143+
}
144+
}
145+
else
146+
{
147+
temp += stringC[i];
148+
fileAnalysis << getCodeWord(temp) << " ";
149+
temp = "";
150+
continue;
151+
}
152+
}
58153
else
59154
{
60-
if(stringC[0] == '#')
155+
if (stringC[i] == '#')
156+
{
61157
temp += stringC[i];
158+
continue;
159+
}
160+
62161
}
63162
temp += stringC[i];
64163
}
@@ -73,7 +172,10 @@ int main()
73172
temp = "";
74173
}
75174
}
76-
175+
}
176+
if (temp != "\0")
177+
{
178+
fileAnalysis << getCodeWord(temp);
77179
}
78180
fileAnalysis << "\n";
79181
}
@@ -91,3 +193,21 @@ int main()
91193
return 0;
92194
}
93195

196+
197+
/*
198+
bool isPointer(string word, int index)
199+
{
200+
if (word[index] == '*' && isLetter((int)word[index - 1]) == true && isLetter((int)word[index + 1]) == false && isDigit((int)word[index + 1]) == false && isSeparators((int)word[index+1])==false)
201+
return true;
202+
int pos = 0;
203+
pos = word.find("=", 1);
204+
string temp = "";
205+
temp.assign(word, index + 1, pos - index);
206+
return word.find("+") == -1 && word.find("-") == -1 && word.find("*") == -1 && word.find("/") == -1 && word.find("+") == -1 ? true : false;
207+
}*/
208+
/*if (i != 0 && stringC[i]=='*')
209+
if (isPointer(stringC, i) == true)
210+
{
211+
fileAnalysis << getCodeWord("p*") << " ";
212+
continue;
213+
}*/

function.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ string getSymbolsConstCode(string str)
7272
return "\0";
7373
}
7474

75-
void addCode(string str, map<string, string> table, int numTable)
75+
void addCode(string str, map<string, string> &table, int numTable)
7676
{
7777
int indexCode = 0;
7878
for (const auto& word : table)
@@ -81,11 +81,11 @@ void addCode(string str, map<string, string> table, int numTable)
8181
}
8282
indexCode++;
8383
if(numTable==1)
84-
table.insert(pair<string, string>(str, "I" + indexCode));
84+
table.insert(pair<string, string>(str, "I" + to_string(indexCode)));
8585
if(numTable==2)
86-
table.insert(pair<string, string>(str, "N" + indexCode));
86+
table.insert(pair<string, string>(str, "N" + to_string(indexCode)));
8787
if(numTable==3)
88-
table.insert(pair<string, string>(str, "C" + indexCode));
88+
table.insert(pair<string, string>(str, "C" + to_string(indexCode)));
8989
}
9090

9191

@@ -120,8 +120,11 @@ string getCodeWordLength_1(string word)
120120

121121
string getCodeWordLengthGreaterOne(string word)
122122
{
123-
if (isLetter((int)word[0]) == true && word[1] == '*')
123+
/*if (word == "p*")
124+
{
125+
word.erase(0, 1);
124126
return getSeparatorsCode(word);
127+
}*/
125128
string code = getServiceWordCode(word);
126129
if (code == "\0")
127130
code = getOperationsCode(word);

lexical.txt

Whitespace-only changes.

table.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "include.h"
66

77
#define SIZE_serviceWord 11
8-
#define SIZE_separators 10
9-
#define SIZE_operation 12
8+
#define SIZE_separators 9
9+
#define SIZE_operation 14
1010
#define SIZE_columns 2
1111

1212
string const serviceWord[SIZE_serviceWord][SIZE_columns] =
@@ -20,7 +20,7 @@ string const serviceWord[SIZE_serviceWord][SIZE_columns] =
2020
{"while","W7"},
2121
{"#include","W8"},
2222
{"malloc","W9"},
23-
{"sizeof","W0"},
23+
{"sizeof","W10"},
2424
{"return","W11"}
2525
};
2626

@@ -37,7 +37,9 @@ string const operations[SIZE_operation][SIZE_columns] =
3737
{"!=","O9"},
3838
{">=","O10"},
3939
{"<=","O11"},
40-
{"++","O12"}
40+
{"/=","O12"},
41+
{"+=","O13"},
42+
{"++","O14"}
4143
};
4244

4345
string const separators[SIZE_separators][SIZE_columns] =
@@ -49,9 +51,9 @@ string const separators[SIZE_separators][SIZE_columns] =
4951
{"{","R5"},
5052
{"}","R6"},
5153
{";","R7"},
52-
{".","R8"},
53-
{"*","R9"},
54-
{"","R10"}
54+
{",","R8"},
55+
//{"*","R9"},
56+
{"","R9"}
5557
};
5658

5759
map<string, string> identifier;

0 commit comments

Comments
 (0)