-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexical_Analyzer.cpp
More file actions
182 lines (169 loc) · 4.47 KB
/
Copy pathLexical_Analyzer.cpp
File metadata and controls
182 lines (169 loc) · 4.47 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
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 255
#define Key "KETWORD_END"
struct opt{
char att[MAX_LINE];
int spe;
};
opt analyze(char *input);
void pop(char *str, int x);
void push();
int isDigit(char x);
int isLetter(char x);
int isCharacter(char x);
int isSpace(char x);
char word[MAX_LINE];
int n, i;
static char Reserved[][15]={"while", "if", "else", "int", Key};
static int Reserves_num[] = {20, 17, 15, 5};
static char Character[][15] = {"+", "-", "*", "/", "%", "=", ">", "<", "!", "(", ")", ";", "{", "}", "[", "]", ",",Key};
static int Character_num[] = {41, 42, 43, 44, 45, 46, 47, 49, 55, 81, 82, 84, 86, 87, 88, 89, 90};
static char Character_double[][15] = {">=", "<=", "==", "!=", "&&", "||", "++", "--", Key};
static int Character_double_num[] = {48, 50, 51, 52, 53, 54, 56, 57};
int isSpace(char x){
if(x == 32){
return 1;
}
else{
return 0;
}
}
int isLetter(char x){
if((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')){
return 1;
}
else{
return 0;
}
}
int isCharacter(char x){
if((x >= '0' && x <= '9') || (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') || x < 33 || x == 127){
return 0;
}
else{
return 1;
}
}
int isDigit(char x){
if(x >= '0' && x <= '9'){
return 1;
}
else{
return 0;
}
}
void pop(char *str, int x){
word[n++] = str[x];
}
void push(){
word[--n] = '\0';
}
struct opt analyze(char *input){
//puts(input);
struct opt res;
//unsigned long length_s = strlen(input);
//for(int i = 0; i < length_s; i++){
if(isLetter(input[i])){
n = 0;
do{
pop(input, i);
i++;
}while((isLetter(input[i]) || isDigit(input[i])) && !isSpace(input[i]));
//判断是否为保留字符
int num = 0;
while(strcmp(Reserved[num], Key)){
if(!strcmp(Reserved[num], word)){
//printf("<%d, ->\n", Reserves_num[num]);
res.spe = Reserves_num[num];
strcpy(res.att, "-");
i--;
memset(word, '\0', MAX_LINE);
return res;
}
else{
num++;
}
}
{
//printf("<111, %s>\n", word);
res.spe = 111;
strcpy(res.att, word);
i--;
memset(word, '\0', MAX_LINE);
return res;
}
}
else if(isDigit(input[i])){
n = 0;
do{
pop(input, i);
i++;
}while(isDigit(input[i]));
//printf("<100, %s>\n", word);
res.spe = 100;
strcpy(res.att, word);
i--;
memset(word, '\0', MAX_LINE);
return res;
}
else if(isCharacter(input[i])){
n = 0;
int num = 0;
pop(input, i);
if(!isCharacter(input[i+1])){
while(1){
if(!strcmp(Character[num], word)){
//printf("<%d, ->\n", Character_num[num]);
res.spe = Character_num[num];
strcpy(res.att, "-");
memset(word, '\0', MAX_LINE);
return res;
}
else{
num++;
}
}
}
else{
pop(input, i+1);
while(strcmp(Character_double[num], Key)){
if(!strcmp(Character_double[num], word)){
//printf("<%d, ->\n", Character_double_num[num]);
res.spe = Character_double_num[num];
strcpy(res.att, "-");
i++;
memset(word, '\0', MAX_LINE);
return res;
}
else{
num++;
}
}
{
push();
num = 0;
while(1){
if(!strcmp(Character[num], word)){
//printf("<%d, ->\n", Character_num[num]);
res.spe = Character_num[num];
strcpy(res.att, "-");
memset(word, '\0', MAX_LINE);
return res;
}
else{
num++;
}
}
//printf("<%d, ->\n", Character_num[num]);
}
}
}
else {
i++;
res = analyze(input);
}
return res;
//}
}