-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.cpp
213 lines (205 loc) · 3.29 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
#include <iostream>
#include <string>
#include "token.h"
#include "define.h"
using namespace std;
int p_ = 0,token_i;
extern char token_buffer[200];
void clear_buffer()
{
for (token_i = 0; token_i < 100; token_i++)
{
token_buffer[token_i] = 0;
}
}
void buffer_char(char ch)
{
token_buffer[p_] = ch;
p_++;
}
void lexical_error(char)
{
cout << "lexical_error!" << endl;
return;
}
token check_reserved()
{
if (!strcmp(token_buffer, "in"))
{
return key_in;
}
else if (!strcmp(token_buffer, "out"))
{
return key_out;
}
else if (!strcmp(token_buffer, "while"))
{
return key_while;
}
else if (!strcmp(token_buffer, "if"))
{
return key_if;
}
else if (!strcmp(token_buffer, "else"))
{
return key_else;
}
else if (!strcmp(token_buffer, "int"))
{
return key_int;
}
else if (!strcmp(token_buffer, "real"))
{
return key_real;
}
else if (!strcmp(token_buffer, "string"))
{
return key_string;
}
else
{
return IDENTIFIER;
}
}
token get_token(void)
{
int in_char, c;
clear_buffer();
if (feof(stdin))
return SCANEOF;
while ((in_char = getchar()) != EOF)
{
p_ = 0;
if (isspace(in_char))
continue;
else if (isalpha(in_char))//判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0。
{
buffer_char(in_char);
for (c = getchar(); isalnum(c) ; c = getchar())//判断字符变量c是否为字母或数字,若是则返回非零,否则返回零。
buffer_char(c);
ungetc(c, stdin);//将c退回到输入流。
return check_reserved();
}
else if (isdigit(in_char))
{
buffer_char(in_char);
for (c = getchar(); isdigit(c); c = getchar())
buffer_char(c);
ungetc(c, stdin);
return INTLITERAL;
}
else if (in_char == '(')
return LPRAEN;
else if (in_char == ')')
return RPRAEN;
else if (in_char == ';')
return SEMICOLON;
else if (in_char == ',')
return COMMA;
else if (in_char == '+')
return OP_plus;
/* else if (in_char == ':')
{
ungetc(c, stdin);
lexical_error(in_char);
*/
else if (in_char == '=')
{
c = getchar();
if (c == '=')
return OP_eq;
else
{
ungetc(c, stdin);
return ASSIGNOP;
}
}
else if (in_char == '-')
{
return OP_minus;
}
else if (in_char == '/')
{
c = getchar();
if (c == '/')
{
while ((in_char = getchar()) != '\n');
}
}
else if (in_char == '"')
{
return Double_quotes;
}
else if (in_char == '*')
{
return OP_multiply;
}
else if (in_char == '/')
{
return OP_divide;
}
else if (in_char == '%')
{
return OP_mod;
}
else if (in_char == '<')
{
c = getchar();
if (c == '=')
return OP_le;
else if (c == '>')
{
return OP_ne;
}
else
{
ungetc(c, stdin);
return OP_lt;
}
}
else if (in_char == '>')
{
c = getchar();
if (c == '=')
{
return OP_ge;
}
else
{
ungetc(c, stdin);
return OP_gt;
}
}
else if (in_char == '&')
{
c = getchar();
if (c == '&')
{
return OP_and;
}
else lexical_error(in_char);
}
else if (in_char == '|')
{
c = getchar();
if (c == '|')
{
return OP_or;
}
else lexical_error(in_char);
}
else if (in_char == '!')
{
return OP_not;
}
else if (in_char == '$')
{
return OP_link;
}
else if (in_char == '#')
{
return OP_delete;
}
else lexical_error(in_char);
}
}