Skip to content

Commit 98cc493

Browse files
#TDP-11 Modify
1. add method checkStringOneEleme 2. method isOperation etc input 2 element(cur, next)
1 parent 711cafa commit 98cc493

File tree

4 files changed

+69
-76
lines changed

4 files changed

+69
-76
lines changed

MethodsDevelopmentTranslator.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ int main(int argc, char* argv[])
116116
}
117117
}
118118

119-
if (isOperation(stringLanguageC, i) == true || isLogicalOperation(stringLanguageC, i) == true)
119+
if (isOperation((int)stringLanguageC[i]) == true || isLogicalSingleOperation((int)stringLanguageC[i]) == true)
120120
{
121-
if (isIncrement(stringLanguageC, i) == true || isDoubleOperation(stringLanguageC, i) == true)
121+
if (isIncrement((int)stringLanguageC[i], (int)stringLanguageC[i+1]) == true ||
122+
isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1] == true) ||
123+
isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
122124
{
123125
temp += stringLanguageC[i];
124126
i++;
@@ -190,22 +192,3 @@ int main(int argc, char* argv[])
190192

191193
return 0;
192194
}
193-
194-
195-
/*
196-
bool isPointer(string word, int index)
197-
{
198-
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)
199-
return true;
200-
int pos = 0;
201-
pos = word.find("=", 1);
202-
string temp = "";
203-
temp.assign(word, index + 1, pos - index);
204-
return word.find("+") == -1 && word.find("-") == -1 && word.find("*") == -1 && word.find("/") == -1 && word.find("+") == -1 ? true : false;
205-
}*/
206-
/*if (i != 0 && stringC[i]=='*')
207-
if (isPointer(stringC, i) == true)
208-
{
209-
fileAnalysis << getCodeWord("p*") << " ";
210-
continue;
211-
}*/

function.cpp

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,43 @@
44
// ( ) [ ] { } ;
55
bool isSeparators(int const &elem)
66
{
7-
return elem == 40 || elem == 41 || elem == 91 || elem == 93 || elem == 123 || elem == 125 || elem == 59 ? true : false;
7+
return elem == 40 || elem == 41 || elem == 91 || elem == 93 || elem == 123 || elem == 125 || elem == 59 || elem == 44 ? true : false;
88
}
99

1010
// \a \b \t \n \v \f \r
1111
bool isServiceSymbols(int const &elem)
1212
{
1313
return elem == 7 || elem == 8 || elem == 9 || elem == 10 || elem == 11 || elem == 12 || elem == 13 ? true : false;
1414
}
15-
/* +- * / % */
16-
bool isOperation(string const &str, int const &ind)
15+
/* +- * / % = */
16+
bool isOperation(int const& elem)
1717
{
18-
return str[ind] == '+' || str[ind] == '-' || str[ind] == '/' || str[ind] == '*' || str[ind]== '=' || str[ind] == '%' ? true : false;
18+
return elem == 43 || elem == 45 || elem == 47 || elem == 42 || elem == 61 || elem == 37 ? true : false;
1919
}
2020

21-
/* < > == >= <= != */
22-
bool isLogicalOperation(string const& str, int const& ind)
21+
/* < > */
22+
bool isLogicalSingleOperation(int const& elem)
2323
{
24-
return str[ind] == '<' || str[ind] == '>' || (str[ind] == '=' && str[ind + 1] == '=') ||
25-
(str[ind] == '>' && str[ind + 1] == '=') || (str[ind] == '<' && str[ind + 1] == '=') || (str[ind] == '!' && str[ind + 1] == '=') ? true : false;
24+
return elem == 60 || elem == 62 ? true : false;
25+
}
26+
/*== >= <= != && ||*/
27+
bool isLogicalDoubleOperation(int const& elem, int const& nextElem)
28+
{
29+
return (elem == 61 && nextElem == 61) || (elem == 62 && nextElem == 61) || (elem == 60 && nextElem == 61)
30+
|| (elem == 33 && nextElem == 61) || (elem == 38 && nextElem == 38) || (elem == 124 && nextElem == 124) ? true : false;
2631
}
2732

2833
//++ --
29-
bool isIncrement(string const& str, int const& ind)
34+
bool isIncrement(int const& elem, int const& nextElem)
3035
{
31-
return (str[ind] == '+' && str[ind + 1] == '+') || (str[ind] == '-' && str[ind + 1] == '-') ? true : false;
36+
return (elem == '+' && nextElem == '+') || (elem == '-' && nextElem == '-') ? true : false;
3237
}
3338

3439
/* += -= *= /= */
35-
bool isDoubleOperation(string const& str, int const& ind)
40+
bool isDoubleOperation(int const& elem, int const& nextElem)
3641
{
37-
return (str[ind] == '+' && str[ind + 1] == '=') || (str[ind] == '-' && str[ind + 1] == '=') ||
38-
(str[ind] == '*' && str[ind + 1] == '=') || (str[ind] == '/' && str[ind + 1] == '=') ? true : false;
42+
return (elem == '+' && nextElem == '=') || (elem == '-' && nextElem == '=') ||
43+
(elem == '*' && nextElem == '=') || (elem == '/' && nextElem == '=') ? true : false;
3944
}
4045

4146
bool isComment(int const &slash, int const& star)
@@ -135,52 +140,71 @@ void addCode(string str, map<string, string> &table, int numTable)
135140
table.insert(pair<string, string>(str, "C" + to_string(indexCode)));
136141
}
137142

143+
int checkStringSingleElem(string const & word)
144+
{
145+
if (isDigit((int)word[0]) == true)
146+
return 1;
147+
if (isOperation((int)word[0]) == true || isLogicalSingleOperation((int)word[0]) == true)
148+
return 2;
149+
if (isSeparators((int)word[0]) == true)
150+
return 3;
151+
if (isLetter((int)word[0]) == true)
152+
return 4;
153+
}
138154

139155
string getCodeWordLength_1(string word)
140156
{
157+
switch (checkStringSingleElem(word))
158+
{
159+
case 1:
160+
if (getNumberConstCode(word) == "\0")
161+
addCode(word, numberConst, 2);
162+
return getNumberConstCode(word);
163+
case 2:
164+
return getOperationsCode(word);
165+
case 3:
166+
return getSeparatorsCode(word);
167+
case 4:
168+
if (getIdentifierCode(word) == "\0")
169+
addCode(word, identifier, 1);
170+
return getIdentifierCode(word);
171+
default:
172+
return "";
173+
}
174+
/*
141175
string code = getOperationsCode(word);
142176
if (code == "\0")
143177
code = getSeparatorsCode(word);
144178
if (code=="\0")
145179
{
146180
if (isDigit((int)word[0]) == true)
147181
{
148-
code = getNumberConstCode(word);
149-
if(code =="\0")
182+
if (getNumberConstCode(word) == "\0")
150183
addCode(word, numberConst, 2);
151184
return getNumberConstCode(word);
152185
}
153186
if (isLetter((int)word[0]) == true)
154187
{
155-
code = getIdentifierCode(word);
156-
if(code=="\0")
188+
if(getIdentifierCode(word) =="\0")
157189
addCode(word,identifier,1);
158190
return getIdentifierCode(word);
159191
}
160192
}
161193
else
162-
{
163-
return code;
164-
}
194+
return code;*/
165195
}
166196

167197

168198
string getCodeWordLengthGreaterOne(string word)
169199
{
170-
/*if (word == "p*")
171-
{
172-
word.erase(0, 1);
173-
return getSeparatorsCode(word);
174-
}*/
175200
string code = getServiceWordCode(word);
176201
if (code == "\0")
177202
code = getOperationsCode(word);
178203
if (code == "\0")
179204
{
180205
if (isNumber(word) == true)
181206
{
182-
code = getNumberConstCode(word);
183-
if(code=="\0")
207+
if(getNumberConstCode(word) =="\0")
184208
addCode(word, numberConst, 2);
185209
return getNumberConstCode(word);
186210
}
@@ -190,23 +214,14 @@ string getCodeWordLengthGreaterOne(string word)
190214
{
191215
if (isLibrary_header(word) == false)
192216
{
193-
code = getSymbolsConstCode(word);
194-
if (code == "\0")
217+
if (getSymbolsConstCode(word) == "\0")
195218
addCode(word, symbolsConst, 3);
196219
return getSymbolsConstCode(word);
197-
}
198-
else
199-
goto addCodeIdentifier;
220+
}
200221
}
201-
else
202-
{
203-
addCodeIdentifier:
204-
code = getIdentifierCode(word);
205-
if (code == "\0")
206-
addCode(word, identifier, 1);
207-
return getIdentifierCode(word);
208-
}
209-
222+
if (getIdentifierCode(word) == "\0")
223+
addCode(word, identifier, 1);
224+
return getIdentifierCode(word);
210225
}
211226
}
212227
else

function.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55

66
bool isSeparators(int const &);
77
bool isServiceSymbols(int const &elem);
8-
bool isOperation(string const &str, int const &ind);
9-
bool isLogicalOperation(string const& str, int const& ind);
10-
bool isIncrement(string const& str, int const& ind);
11-
bool isDoubleOperation(string const& str, int const& ind);
8+
bool isOperation(int const& elem);
9+
bool isLogicalSingleOperation(int const& elem);
10+
bool isLogicalDoubleOperation(int const& elem, int const& nextElem);
11+
bool isIncrement(int const& elem, int const& nextElem);
12+
bool isDoubleOperation(int const& elem, int const& nextElem);
1213
bool isComment(int const& slash, int const& star);
1314
bool isOneStringComment(int const& slash, int const& slash2);
1415
bool isDigit(int const &);
1516
bool isLetter(int const &);
1617
bool isLibrary_header(string const &);
17-
string getServiceWordCode(string);
18-
string getOperationsCode(string str);
19-
string getSeparatorsCode(string str);
20-
string getIdentifierCode(string str);
21-
string getNumberConstCode(string str);
22-
string getSymbolsConstCode(string str);
2318
string getCodeWord(string );
2419
#endif

lexical.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ W8 I2
44
W1 I3 R3 W1 I4 R4
55
R5
66
W1 I5 O5 N1 R7
7-
W7 R3 I4 O5 N1 R4
7+
W7 R3 I4 O10 N1 R4
88
R5
99
I4 O5 I4 O4 N2 R7
1010
I5 O20 R7
@@ -17,10 +17,10 @@ R5
1717
W1 I7 O5 I3 R3 I4 R4 R7
1818
W1 O3 I8 O5 R3 W1 O3 R4 W9 R3 I7 O3 W10 R3 W1 R4 R4 R7
1919
W1 I9 O5 N1 R7
20-
W7 R3 I4 O5 N1 R4
20+
W7 R3 I4 O10 N1 R4
2121
R5
2222
I8 R1 I9 R2 O5 I4 O6 N2 R7
23-
I4 O13 N2 R7
23+
I4 O4 O5 N2 R7
2424
I9 O20 /*--*/ R7
2525
R6
2626
W11 I8 R7
@@ -38,7 +38,7 @@ I15 R3 W1 I16 O5 N1 R7 I16 O8 I7 R7 I16 O20 R4
3838
R5
3939
W5 R3 I16 O8 N4 O17 I16 O7 N1 R4
4040
I17 R3 C1 C2 I16 R8 I13 R1 I16 R2 R4 R7
41-
I14 O14 I13 R1 I16 R2 R7
41+
I14 O1 O5 I13 R1 I16 R2 R7
4242
R6
4343
I17 R3 C3 C2 I14 R4 R7
4444
I18 R3 I13 R4 R7

0 commit comments

Comments
 (0)