@@ -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
2939int 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+ }*/
0 commit comments