-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo1.l
329 lines (317 loc) · 7.71 KB
/
demo1.l
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
*file: C-lexical-analyzer.l
*auther: jin1ming
*system: manjaro
*/
%option yylineno
%{
#include<stdio.h>
#include<string.h>
int Oct2Dec(char temp[]);
float d_Oct2Dec(char temp[]);
int Hex2Dec(char temp[]);
float d_Hex2Dec(char temp[]);
int IsDecimals(char temp[]);
extern int yylineno;
%}
/*数字定义*/
/*科学计数表示*/
science {decimal}(\.[0-9]+)?([Ee][-+]?[0-9]+)?
/*十进制*/
decimal (0|[1-9][0-9]*)(\.[0-9][0-9]*)?
/*十六进制*/
hexadecimal 0[xX][a-fA-F0-9]+(\.[0-9a-fA-F][0-9a-fA-F]+)?
/*二进制*/
binary 0[bB][01]+
/*八进制*/
octal 0[0-7]+(\.[0-7][0-7]+)?
/*总表示*/
number ({hexadecimal}|{binary}|{science}|{octal})(([uU]?[Ll]?)|([Ll]?[Uu]?)|([fF]?))
/*注意浮点数总是有符号,不需要Uu后缀,所以在接下来单做一个浮点数异常处理*/
/*数字异常处理*/
floatexcption {decimal}\.[0-9]+([Ee]?[-+]?[0-9]+)?[Uu]
excption [0-9][0-9a-zA-Z\.]+
/*关键字*/
BREAK break
CASE case
CHAR char
CONTINUE continue
DEFAULT default
DO do
DOUBLE double
ELSE else
FLOAT float
FOR for
IF if
INT int
LONG long
RETURN return
SHORT short
SIGNED signed
SWITCH switch
UNSIGNED unsigned
VOID void
WHILE while
/*标识符定义*/
identifier [a-zA-Z][a-zA-Z0-9]*([_\.][a-zA-Z0-9]+)?
/*其它字符*/
comment (\/\/.*)|(\/\*(.|\n)*\/)
whitespace [ \t\n\r\f\v]+
errno .
/*运算符*/
/*算术运算符*/
ADD \+
SUB \-
MUL \*
QUO \/
REM %
INC \+\+
DEC \-\-
/*赋值运算符*/
ASSIGN =
ADD_ASSIGN \+=
SUB_ASSIGN \-=
MUL_ASSIGN \*=
QUO_ASSIGN \/=
REM_ASSIGN %=
AND_ASSIGN \&=
OR_ASSIGN \|=
XOR_ASSIGN \^=
SHL_ASSIGN <<=
SHR_ASSIGN >>=
AND_NOT_ASSIGN ~=
/*位运算符*/
AND &
OR \|
XOR \^
SHL <<
SHR >>
AND_NOT ~
/*逻辑运算符*/
LAND &&
LOR \|\|
NOT \!
/*关系运算符*/
EQL ==
LSS <
GTR >
NEQ !=
LEQ <=
GEQ >=
/*标点符号*/
LPAREN \(
LBRACK \[
LBRACE \{
COMMA ,
PERIOD \.
RPAREN \)
RBRACK \]
RBRACE \}
SEMICOLON ;
COLON :
POT \->
DQUA \"
SQUA \'
%%
/*关键字*/
{BREAK} {printf("BREAK\t_\n");}
{CASE} {printf("CASE\t_\n");}
{CHAR} {printf("CHAR\t_\n");}
{CONTINUE} {printf("CONTINUE\t_\n");}
{DEFAULT} {printf("DEFAULT\t_\n");}
{DO} {printf("DO\t_\n");}
{DOUBLE} {printf("DOUBLE\t_\n");}
{ELSE} {printf("ELSE\t_\n");}
{FLOAT} {printf("FLOAT\t_\n");}
{FOR} {printf("FOR\t_\n");}
{IF} {printf("IF\t_\n");}
{INT} {printf("INT\t_\n");}
{LONG} {printf("LONG\t_\n");}
{RETURN} {printf("RETURN\t_\n");}
{SHORT} {printf("SHORT\t_\n");}
{SIGNED} {printf("SIGNED\t_\n");}
{SWITCH} {printf("SWITCH\t_\n");}
{UNSIGNED} {printf("UNSIGNED\t_\n");}
{VOID} {printf("VOID\t_\n");}
{WHILE} {printf("WHILE\t_\n");}
/*提前处理浮点数+uU的异常*/
{floatexcption} {printf("Float Execption: %s\n",yytext);}
/*数字表示*/
{decimal} {
if(IsDecimals(yytext))
printf("REAL10\t%s\n",yytext);
else
printf("INT10\t%s\n",yytext);}
{octal} {
if(IsDecimals(yytext))
printf("REAL8\t%lf\n",d_Oct2Dec(yytext));
else
printf("INT8\t%d\n",Oct2Dec(yytext));}
{hexadecimal} {
if(IsDecimals(yytext))
printf("REAL16\t%lf\n",d_Hex2Dec(yytext));
else
printf("INT16\t%d\n",Hex2Dec(yytext));}
/*异常数字处理*/
{excption} {printf("Number Execption: %s\n",yytext);}
/*跳过空白和注释*/
{whitespace} {}
{comment} {printf("This is a commit.\n");}
/*运算符*/
/*算术运算符*/
{ADD} {printf("%s\t_\n",yytext);}
{SUB} {printf("%s\t_\n",yytext);}
{MUL} {printf("%s\t_\n",yytext);}
{QUO} {printf("%s\t_\n",yytext);}
{REM} {printf("%s\t_\n",yytext);}
{INC} {printf("%s\t_\n",yytext);}
{DEC} {printf("%s\t_\n",yytext);}
/*逻辑运算符*/
{LAND} {printf("%s\t_\n",yytext);}
{LOR} {printf("%s\t_\n",yytext);}
{NOT} {printf("%s\t_\n",yytext);}
/*赋值运算符*/
{ASSIGN} {printf("%s\t_\n",yytext);}
{ADD_ASSIGN} {printf("%s\t_\n",yytext);}
{SUB_ASSIGN} {printf("%s\t_\n",yytext);}
{MUL_ASSIGN} {printf("%s\t_\n",yytext);}
{QUO_ASSIGN} {printf("%s\t_\n",yytext);}
{REM_ASSIGN} {printf("%s\t_\n",yytext);}
{AND_ASSIGN} {printf("%s\t_\n",yytext);}
{OR_ASSIGN} {printf("%s\t_\n",yytext);}
{XOR_ASSIGN} {printf("%s\t_\n",yytext);}
{SHL_ASSIGN} {printf("%s\t_\n",yytext);}
{SHR_ASSIGN} {printf("%s\t_\n",yytext);}
{AND_NOT_ASSIGN} {printf("%s\t_\n",yytext);}
/*位运算符*/
{AND} {printf("%s\t_\n",yytext);}
{OR} {printf("%s\t_\n",yytext);}
{XOR} {printf("%s\t_\n",yytext);}
{SHL} {printf("%s\t_\n",yytext);}
{SHR} {printf("%s\t_\n",yytext);}
{AND_NOT} {printf("%s\t_\n",yytext);}
/*关系运算符*/
{EQL} {printf("%s\t_\n",yytext);}
{LSS} {printf("%s\t_\n",yytext);}
{GTR} {printf("%s\t_\n",yytext);}
{NEQ} {printf("%s\t_\n",yytext);}
{LEQ} {printf("%s\t_\n",yytext);}
{GEQ} {printf("%s\t_\n",yytext);}
/*标点符号*/
{LPAREN} {printf("%s\t_\n",yytext);}
{LBRACK} {printf("%s\t_\n",yytext);}
{LBRACE} {printf("%s\t_\n",yytext);}
{COMMA} {printf("%s\t_\n",yytext);}
{PERIOD} {printf("%s\t_\n",yytext);}
{RPAREN} {printf("%s\t_\n",yytext);}
{RBRACK} {printf("%s\t_\n",yytext);}
{RBRACE} {printf("%s\t_\n",yytext);}
{SEMICOLON} {printf("%s\t_\n",yytext);}
{COLON} {printf("%s\t_\n",yytext);}
{POT} {printf("%s\t_\n",yytext);}
{DQUA} {printf("%s\t_\n",yytext);}
{SQUA} {printf("%s\t_\n",yytext);}
{identifier} {printf("IDN\t%s\n",yytext);}
{errno} {printf("On line %d,mystery character: %s\n",yylineno,yytext);}
%%
int IsDecimals(char temp[]) //判断是否是小数
{
for (int i = 1;i < strlen(temp);i++)
{
if(temp[i]=='.')
return 1;
}
return 0;
}
int Oct2Dec(char temp[]) //整数八进制转十进制
{
int sum = 0;
for (int i = 1;i < strlen(temp);i++)
{
sum = sum * 8 + temp[i] - '0';
}
return sum;
}
float d_Oct2Dec(char temp[]) //小数八进制转十进制
{
float sum1 = 0,sum2=temp[strlen(temp) - 1] - '0';
int i;
for (i = 1;i < strlen(temp)&&temp[i]!='.';i++)
{
sum1 = sum1 * 8 + temp[i] - '0';
}
for (int j = strlen(temp) - 2;j > i;j--)
{
sum2=sum2/8+ temp[j] - '0';
}
sum2=sum2/8;
return sum1+sum2;
}
int Hex2Dec(char temp[]) //整数>十六进制转十进制
{
int sum = 0;
int mid[100];
for (int ii = 0;ii < strlen(temp);ii++)
{
if (temp[ii] == 'a' || temp[ii] == 'A')
mid[ii] = 10;
else if (temp[ii] == 'b' || temp[ii] == 'B')
mid[ii] = 11;
else if (temp[ii] == 'c' || temp[ii] == 'C')
mid[ii] = 12;
else if (temp[ii] == 'd' || temp[ii] == 'D')
mid[ii] = 13;
else if (temp[ii] == 'e' || temp[ii] == 'E')
mid[ii] = 14;
else if (temp[ii] == 'f' || temp[ii] == 'F')
mid[ii] = 15;
else mid[ii] = temp[ii] - '0';
}
int i;
for (i = 2;i < strlen(temp);i++)
{
sum = sum * 16 + mid[i];
}
return sum;
}
float d_Hex2Dec(char temp[]) //小数十六进制转十进制
{
int mid[100];
for (int ii = 0;ii < strlen(temp);ii++)
{
if (temp[ii] == 'a' || temp[ii] == 'A')
mid[ii] = 10;
else if (temp[ii] == 'b' || temp[ii] == 'B')
mid[ii] = 11;
else if (temp[ii] == 'c' || temp[ii] == 'C')
mid[ii] = 12;
else if (temp[ii] == 'd' || temp[ii] == 'D')
mid[ii] = 13;
else if (temp[ii] == 'e' || temp[ii] == 'E')
mid[ii] = 14;
else if (temp[ii] == 'f' || temp[ii] == 'F')
mid[ii] = 15;
else mid[ii] = temp[ii] - '0';
}
float sum1 = 0, sum2 = mid[strlen(temp)-1];
int i;
for (i = 2;i < strlen(temp) && mid[i] != '.'-'0';i++)
{
sum1 = sum1 * 16 + mid[i];
}
for (int j = strlen(temp) - 2;j > i;j--)
{
sum2 = sum2 / 16 + mid[j];
}
sum2 = sum2 / 16;
return sum1 + sum2;
}
int main(int argc,char **argv)
{
yylineno = 1;
yylex();
return 0;
}
int yywarp(){
return 1;
}