-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.cpp
216 lines (180 loc) · 3.58 KB
/
Token.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <iostream>
#include <string>
#include "Token.h"
#define SIZE 10
using namespace std;
int id=0;
int keyW=0;
int sym=0;
int str=0;
int lit=0;
string keyword[SIZE]={"numbers", "decimals", "words", "if",
"else", "check", "expr" "stop", "repeatTill","send"
};
//Functions Definitions
void Tokenizer::setSourceCode(string code)
{
s=code;
}
//Display Source Code
void Tokenizer::display()
{
cout<<s;
}
void Tokenizer::separateWords()
{
int index=0;
char c;
string word="";
while(index < s.length() ) // In String length there is extra increase of length by 2
{
c=s.at(index);
if(isChar(c))
{
word=word+c;
}
if(isString(c))
{
word=word+c;
while(true)
{
index++;
c=s.at(index);
word=word+c;
if(c== 34)
{
//Generates String "Hello World !"
//cout<<word<<endl;
generateToken(word, 'S');
word="";
break;
}
}
}
if(isSymbol(c) || c == 32 )
{
if(word != "")
{//Generates Keyword or Identifier
//cout<<word<<endl;
if(checkKeyword(word) == true)
{
generateToken(word, 'K');
}
//Checking Identifier repective to Regular Expression
else if(checkIdentifier(word) == true)
{
generateToken(word, 'I');
}
else
{
//For Constants
generateToken(word, 'L');
}
word="";
}
if(c!=32)
{
//For Logical Operator e.g == , !=, >=, <=
if((c== '='|| c=='>' || c=='<' || c=='!') && '='==s.at(index+1) || c=='|' || c=='&')
{
string logical_O="";
logical_O +=c;
logical_O +=s.at(index+1);
generateToken(logical_O, 'O');
//cout<<logical_O<<endl;
index++;
}
else if(c!=10)
{
//Generates Symbols like *,-,/...etc
//cout<<c<<endl;
string s(1, c); // Casting char into string
generateToken(s, 'O');
}
}
}
index++;
}
}
bool Tokenizer::isChar(char c)
{
if((c>=65 && c<=90) || (c>=97 && c<=122) || (c>=48 && c<=57) )
{
return true;
}
return false;
}
bool Tokenizer::isSymbol(char c)
{
//c==10 for printing last character
if((c>=35 && c<=47) || c==33 || c == 10 || (c>=58 && c<=64) || (c>=91 && c<=96) || (c>=123 && c<=125))
{
return true;
}
return false;
}
bool Tokenizer::isString(char c)
{
if(c == 34)
{
return true;
}
return false;
}
bool Tokenizer::checkKeyword(string word)
{
for(int i=0; i<SIZE; i++)
{
if(keyword[i] == word)
{
return true;
}
}
return false;
}
void Tokenizer::generateToken(string word, char c)
{
switch(c)
{
case'K':
keyW++;
cout<<"< Keyword#"<<keyW<<", "<<word<<" >\n"<<endl;
break;
case'I':
id++;
cout<<"< Idenfier#"<<id<<", "<<word<<" >\n"<<endl;
break;
case'O':
sym++;
cout<<"< Symbol#"<<sym<<", "<<word<<" >\n"<<endl;
break;
case'S':
str++;
cout<<"< String#"<<str<<", "<<word<<" >\n"<<endl;
break;
case'L':
lit++;
cout<<"< Literals#"<<lit<<", "<<word<<" >\n"<<endl;
break;
}
}
bool Tokenizer::checkIdentifier(string word)
{
//If string starts from letters a, b,c..z or A, B, ..z
if((word.at(0)>=65 && word.at(0)<=90) || (word.at(0)>=97 && word.at(0)<=122) )
{
//Checking for string such assasd!# which is not accepted
for(int i=1; i<word.length(); i++)
{
if(word.at(i) >= 48 && word.at(i) <= 57 || word.at(i) >= 65 && word.at(i) <= 90 || word.at(i) >= 97 && word.at(i) <= 122 )
{
}
else
{
return false;
}
}
return true;
}
return false;
}