-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
94 lines (85 loc) · 3.23 KB
/
main.cpp
File metadata and controls
94 lines (85 loc) · 3.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
#include"global.h"
#include"token.h"
#include"lex.h"
int argc;
char** argv;
ofstream outfile;
int ch_cnt = 0;
int main(int a, char** b){
argc = a;
argv = b;
keyword k;
operators o;
delimiter de;
strings s;
characters c;
error e;
id i;
if (argc > 1){
if(!(yyin = fopen(argv[1], "r"))){
perror(argv[1]);
return 1;
}
if (argc == 2){
cout << left << setw(30) << "TOKEN_TYPE" << left << setw(30) << "CONTENT" << endl << endl;
}else if (argc ==3){
outfile.open(argv[2]);
outfile << left << setw(30) << "TOKEN_TYPE" << left << setw(30) << "CONTENT" << endl << endl;
}
}else{
cout << "Please input the token" << endl;
}
int d = yylex();
while(d){
if (d == KEYWORD){
k.detected(yytext);
ch_cnt += yyleng;
}else if(d == RELOP){
o.detected(yytext);
ch_cnt += yyleng;
}else if(d == DELIMITER){
de.detected(yytext);
ch_cnt += yyleng;
}else if (d == IDENTIFIER){
ch_cnt += yyleng;
}else if(d == STRINGS){
s.display(yytext);
ch_cnt += yyleng;
}else if (d == CHARACTERS){
c.display(yytext);
ch_cnt += yyleng;
}else if(d == NUM){
ch_cnt += yyleng;
}else if(d == ERROR){
e.display(yytext, yylineno);
}
else{
;
}
d = yylex();
}
if(argc == 2){
cout << endl << endl << "STATISTICS" << endl << endl;
cout << left << setw(30) << "ITEM" << left << setw(30) << "COUNT" << endl;
cout << left << setw(30) << "Identifier" << left << setw(30) << id::id_cnt << endl;
cout << left << setw(30) << "Number" << left << setw(30) << num::num_cnt << endl;
cout << left << setw(30) << "Operator" << left << setw(30) << operators::opr_cnt << endl;
cout << left << setw(30) << "Delimiter" << left << setw(30) << delimiter::dlmt_cnt << endl;
cout << left << setw(30) << "Keyword" << left << setw(30) << keyword::kw_cnt << endl;
cout << left << setw(30) << "Line" << left << setw(30) << line::count << endl;
cout << left << setw(30) << "Total character number" << left << setw(30) << ch_cnt << endl;
}else if(argc == 3){
outfile << endl << endl << "STATISTICS" << endl << endl;
outfile << left << setw(30) << "ITEM" << left << setw(30) << "COUNT" << endl;
outfile << left << setw(30) << "Identifier" << left << setw(30) << id::id_cnt << endl;
outfile << left << setw(30) << "Number" << left << setw(30) << num::num_cnt << endl;
outfile << left << setw(30) << "Operator" << left << setw(30) << operators::opr_cnt << endl;
outfile << left << setw(30) << "Delimiter" << left << setw(30) << delimiter::dlmt_cnt << endl;
outfile << left << setw(30) << "Keyword" << left << setw(30) << keyword::kw_cnt << endl;
outfile << left << setw(30) << "Line" << left << setw(30) << line::count << endl;
outfile << left << setw(30) << "Total character number" << left << setw(30) << ch_cnt << endl;
}
i.disp_tbl();
outfile.close();
return 0;
}