-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.cpp
More file actions
171 lines (155 loc) · 4.23 KB
/
token.cpp
File metadata and controls
171 lines (155 loc) · 4.23 KB
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
#include"token.h"
int token::count = 0;
void token::inc_cnt(){
count++;
}
int id::id_cnt = 0;
int id::id_id = 200;
void id::inc_id_cnt(){
id_cnt++;
token::count++;
}
map<string, int> id::wordlist;
void id::add_id(string s){ //should be added first and then increase id_cnt
wordlist[s] = id_id;
id_id++;
}
void id::display(string s){
if(argc == 3){
outfile << left << setw(30) << "Identifier" << left << setw(30) << s << endl;
}else{
cout << left << setw(30) << "Identifier" << left << setw(30) << s << endl;
}
}
bool id::exit(string s){
return wordlist.count(s);
};
int id::detected(){
string s(yytext);
if(exit(s)){
inc_id_cnt();
display(s);
return wordlist[s];
}else{
add_id(s);
display(s);
return id_id - 1;
}
}
void id::disp_tbl(){
if (argc == 2){
cout << endl << endl << "ID_TABLE" << endl << endl;
map<string, int>::iterator iter = wordlist.begin();
map<string ,int>::iterator end = wordlist.end();
for (; iter != end; iter ++)
cout << setw(5) << iter->second << setw(10) << " " << iter->first << endl;
}else if (argc == 3){
outfile << endl << endl << "ID_TABLE" << endl << endl;
map<string, int>::iterator iter = wordlist.begin();
map<string ,int>::iterator end = wordlist.end();
for (; iter != end; iter ++)
outfile << setw(5) << iter->second << setw(10) << " " << iter->first << endl;
}else {
;
}
}
int num::num_cnt = 0;
void num::inc_num_cnt(){
num_cnt++;
token::count++;
}
void num::display(string s){
if (argc == 3){
outfile << left << setw(30) << "Number" << left << setw(30) << s << endl;
}else{
cout << left << setw(30) << "Number" << left << setw(30) << s << endl;
}
}
double num::detected(){
inc_num_cnt();
string s(yytext);
display(s);
double value = stod(s);
return value;
}
int operators::opr_cnt = 0;
void operators::inc_opr_cnt(){
opr_cnt++;
token::count++;
}
void operators::display(string s){
if (argc == 3){
outfile << left << setw(30) << "Operator" << left << setw(30) << s << endl;
}else{
cout << left << setw(30) << "Operator" << left << setw(30) << s << endl;
}
}
void operators::detected(char* yytext){
string s(yytext);
inc_opr_cnt();
display(s);
}
int delimiter::dlmt_cnt = 0;
void delimiter::inc_dlmt_cnt(){
dlmt_cnt++;
token::count++;
}
void delimiter::display(string s){
if (argc == 3){
outfile << left << setw(30) << "Delimiter" << left << setw(30) << s << endl;
}else{
cout << left << setw(30) << "Delimiter" << left << setw(30) << s << endl;
}
}
void delimiter::detected(char* yytext){
string s(yytext);
inc_dlmt_cnt();
display(s);
}
int keyword::kw_cnt = 0;
void keyword::inc_kw_cnt(){
kw_cnt++;
token::count++;
}
void keyword::display(string s){
if (argc == 3){
outfile << left << setw(30) << "Keyword" << left << setw(30) << s << endl;
}else {
cout << left << setw(30) << "Keyword" << left << setw(30) << s << endl;
}
}
void keyword::detected(char* yytext){
string s(yytext);
inc_kw_cnt();
display(s);
}
int line::count = 1;
void line::inc_cnt(){
count++;
}
void strings::display(char* yytext){
string s_temp(yytext);
string s = s_temp.substr(1, s_temp.length() - 2);
if (argc == 3){
outfile << left << setw(30) << "String" << left << setw(30) << s << endl;
}else{
cout << left << setw(30) << "String" << left << setw(30) << s << endl;
}
}
void characters::display(char* yytext){
string s_temp(yytext);
string s = s_temp.substr(1, s_temp.length() - 2);
if (argc == 3){
outfile << left << setw(30) << "Char" << left << setw(30) << s << endl;
}else{
cout << left << setw(30) << "Char" << left << setw(30) << s << endl;
}
}
void error::display(char* yytext, int yylineno){
string s(yytext);
if (argc == 3){
outfile << left << setw(30) << "Error" << left << setw(30) << s << left <<"Line: " << yylineno << endl;
}else{
cout << left << setw(30) << "Error" << left << setw(30) << s << left <<"Line: " << yylineno << endl;
}
}