Skip to content

Commit cd8096e

Browse files
#TDP-6 Add method and Modify
1. Add method : isDigit, isNumber, isLetter getCode, getCode for length 1, getCode for length>1 2.Modify table : add malloc and sizeof => serviceWord 3. Main : add read file input language C
1 parent 2f2a965 commit cd8096e

4 files changed

Lines changed: 130 additions & 12 deletions

File tree

MethodsDevelopmentTranslator.cpp

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

28+
2829
int main()
2930
{
30-
cout << getServiceWordCode("w");
31-
/*ifstream fileC;
31+
ifstream fileC;
3232
ofstream fileAnalysis;
3333
fileC.exceptions(ifstream::badbit);
3434
try
@@ -39,7 +39,29 @@ int main()
3939
{
4040
while (!fileC.eof())
4141
{
42+
string stringC = "";
43+
string temp = "";
44+
getline(fileC, stringC);
45+
for (int i = 0; i < stringC.length()-1; i++)
46+
{
47+
if (stringC[i] != ' ')
48+
{
49+
if (isLetter(stringC[i]) == true && (isLetter(stringC[i+1])==false || isDigit(stringC[i+1])==false))
50+
{
51+
52+
}
53+
else
54+
{
55+
temp += stringC[i];
56+
}
57+
}
58+
else
59+
{
60+
string out = getServiceWordCode(temp);
61+
62+
}
4263

64+
}
4365
}
4466
}
4567

@@ -48,7 +70,8 @@ int main()
4870
{
4971
cout << " Exception opening/reading file";
5072
}
51-
fileC.close();*/
73+
74+
fileC.close();
5275

5376
return 0;
5477
}

function.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
#include "table.h"
22
#include "function.h"
33

4+
bool isLetter(char elem)
5+
{
6+
return (elem >= '65' && elem <= '90') || (elem >= '97' && elem <= '122') ? true : false;
7+
}
8+
9+
bool isDigit(char elem)
10+
{
11+
return elem >= '48' && elem <= '57' ? true : false;
12+
}
13+
14+
bool isNumber(string num)
15+
{
16+
for (int i = 0; i < num.length(); i++)
17+
if (isDigit(num[i]) == false)
18+
return false;
19+
return true;
20+
}
21+
422
string getServiceWordCode(string str)
523
{
624
for (int i = 0; i < SIZE_serviceWord; i++)
@@ -48,3 +66,75 @@ string getSymbolsConstCode(string str)
4866
return word.second;
4967
return "\0";
5068
}
69+
70+
string getCodeWordLength_1(string word)
71+
{
72+
string code = getOperationsCode(word);
73+
if (code == "\0")
74+
code = getSeparatorsCode(word);
75+
if (code=="\0")
76+
{
77+
if (isDigit(word[0]) == true)
78+
{
79+
code = getNumberConstCode(word);
80+
//TODO: Add constant
81+
}
82+
if (isLetter(word[0]) == true)
83+
{
84+
code = getIdentifierCode(word);
85+
//TODO: Add word
86+
}
87+
}
88+
else
89+
{
90+
return code;
91+
}
92+
}
93+
94+
string getCodeWordLengthGreaterOne(string word)
95+
{
96+
string code = getServiceWordCode(word);
97+
if (code == "\0")
98+
code = getOperationsCode(word);
99+
if (code == "\0")
100+
{
101+
if (isNumber(word) == true)
102+
{
103+
code = getNumberConstCode(word);
104+
}
105+
else
106+
{
107+
if (word[0] == '34')
108+
{
109+
code = getSymbolsConstCode(word);
110+
if (code == "\0")
111+
{
112+
//TODO:ADD const sym
113+
}
114+
}
115+
else
116+
{
117+
code = getIdentifierCode(word);
118+
if (code == "\0")
119+
{
120+
//TODO:ADD id
121+
}
122+
}
123+
124+
}
125+
}
126+
else
127+
return code;
128+
}
129+
130+
string getCodeWord(string word)
131+
{
132+
if (word.length() == 1)
133+
{
134+
return getCodeWordLength_1(word);
135+
}
136+
else
137+
{
138+
return getCodeWordLengthGreaterOne(word);
139+
}
140+
}

function.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#pragma once
22
#ifndef FUNCTION_H
33
#define FUNCTION_H
4-
#include <iostream>
4+
#include "include.h"
55

6-
std::string getServiceWordCode(std::string);
7-
std::string getOperationsCode(std::string str);
8-
std::string getSeparatorsCode(std::string str);
9-
std::string getIdentifierCode(std::string str);
10-
std::string getNumberConstCode(std::string str);
11-
std::string getSymbolsConstCode(std::string str);
6+
bool isDigit(char);
7+
bool isLetter(char);
8+
string getServiceWordCode(string);
9+
string getOperationsCode(string str);
10+
string getSeparatorsCode(string str);
11+
string getIdentifierCode(string str);
12+
string getNumberConstCode(string str);
13+
string getSymbolsConstCode(string str);
1214
#endif

table.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "include.h"
66

7-
#define SIZE_serviceWord 8
7+
#define SIZE_serviceWord 11
88
#define SIZE_separators 10
99
#define SIZE_operation 12
1010
#define SIZE_columns 2
@@ -18,7 +18,10 @@ string const serviceWord[SIZE_serviceWord][SIZE_columns] =
1818
{"if","W5"},
1919
{"else","W6"},
2020
{"while","W7"},
21-
{"return","W8"}
21+
{"#include","W8"},
22+
{"malloc","W9"},
23+
{"sizeof","W0"},
24+
{"return","W11"}
2225
};
2326

2427
string const operations[SIZE_operation][SIZE_columns] =

0 commit comments

Comments
 (0)