-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex_unit_test.l
More file actions
191 lines (167 loc) · 4.23 KB
/
lex_unit_test.l
File metadata and controls
191 lines (167 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
%{
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define ERRORCHAR 500
#define ERRORINT8 500
#define ERRORINT16 501
#define ERRORFLOAT 502
#define MAXSTR 32
static char KeyWord[7][MAXSTR]={
"int",
"float",
"struct",
"return",
"if",
"else",
"while"
};
enum {
INT10 = 256,
INT8 = 257,
INT16 = 258,
FLOAT_NUM = 259,
ID = 260,
GE = 261,
LE = 262,
Equal = 263,
Unequal = 264,
And = 265,
Or = 266,
INT = 267,
FLOAT = 268,
STRUCT = 268,
RETURN = 270,
IF = 271,
ELSE = 272,
WHILE = 273,
UMINUS = 274,
LOWER_THAN_ELSE = 275
};
char str[MAXSTR];
int id_or_keyword(char *);
%}
letter [_A-Za-z]
n_decimal [1-9]
decimal [0-9]
n_octonary [1-7]
octonary [0-7]
un_octonary [8-9A-Za-z]
n_hexadecimal [1-9A-Fa-f]
hexadecimal [0-9A-Fa-f]
un_hexadecimal [G-Zg-z]
nextline [\n]
ignore [\t\040]
%option yylineno
%start COMMENT1 COMMENT2
%%
"//" {BEGIN COMMENT1;}
<COMMENT1>\n {BEGIN 0; return '\n';}
<COMMENT1>[^\n]+ {}
"/*" {BEGIN COMMENT2;}
<COMMENT2>\n {}
<COMMENT2>"*/" {BEGIN 0;}
<COMMENT2>[^"*/"]+ {}
{decimal}|{n_decimal}{decimal}+ {return INT10;}
[0]([x]|[X]){hexadecimal}|{n_hexadecimal}{hexadecimal}+ {return INT16;}
[0]([x]|[X])({hexadecimal}|{n_hexadecimal}{hexadecimal}+)*{un_hexadecimal}+({un_hexadecimal}|{hexadecimal})* {printf("Lexical error at line %d: Illegal hexadecimal number \'%s\'\n", yylineno, yytext); return ERRORINT16;}
[0]({octonary}|{n_octonary}{octonary}+) {return INT8;}
[0]({octonary}|{n_octonary}{octonary}+)*{un_octonary}+({un_octonary}|{octonary})* {printf("Lexical error at line %d: Illegal octonary number \'%s\'\n", yylineno, yytext); return ERRORINT8;}
{decimal}+[.]{decimal}+ {return FLOAT_NUM;}
{decimal}*[.]{decimal}*([E]|[e]){decimal}+ {return FLOAT_NUM;}
{decimal}*[.]{decimal}*([E]|[e]) {printf("Lexical error at line %d: Illegal float number \'%s\'\n", yylineno, yytext); return ERRORFLOAT;}
";" {return ';';}
"," {return ',';}
"=" {return '=';}
">" {return '>';}
"<" {return '<';}
">=" {return GE;}
"<=" {return LE;}
"==" {return Equal;}
"!=" {return Unequal;}
"+" {return '+';}
"-" {return '-';}
"*" {return '*';}
"/" {return '/';}
"&&" {return And;}
"||" {return Or;}
"." {return '.';}
"!" {return '!';}
"(" {return '(';}
")" {return ')';}
"[" {return '[';}
"]" {return ']';}
"{" {return '{';}
"}" {return '}';}
{letter}({letter}|{decimal})* {int i; i = id_or_keyword(yytext); return i;}
{nextline} {return '\n';}
{ignore}+ {}
. {printf("Lexical error at line %d: Mysterious characters \'%s\'\n", yylineno, yytext); return ERRORCHAR;}
%%
int id_or_keyword(char* str)
{
unsigned i, j;
for(i=0; i<7; i++){
if(!strcmp(str, KeyWord[i]))
break;
}
if(i<7){
return i + 267;
}
else{
return ID;
}
}
int yywrap(){
return 1;
}
int main()
{
int c, i, flag1=0, temp=0, flag2=0;
char source[40], target[40];
FILE *target_file;
strcpy(source, "test.c");
strcpy(target, "test.txt");
if((yyin = fopen(source, "r")) == NULL)
{
printf("Cannot open the source file!\n");
return 0;
}
target_file = fopen(target, "w");
while((c = yylex()) != 0){
if(flag1 == 0){
fprintf(target_file, "line 1: ");
flag1 = 1;
}
if(c == '\n'){
temp = 1;
}
else{
if(temp == 1){
temp = 0;
fprintf(target_file, "\nline %2d: ", yylineno);
}
if(c == ERRORCHAR)
fprintf(target_file, "[%3d, \"%s\"]\t", c, yytext);
else if(c == ERRORINT8)
fprintf(target_file, "[%3d, \"%s\"]\t", c, yytext);
else if(c == ERRORINT16)
fprintf(target_file, "[%3d, \"%s\"]\t", c, yytext);
else if(c == ERRORFLOAT)
fprintf(target_file, "[%3d, \"%s\"]\t", c, yytext);
else if(c == 256)
fprintf(target_file, "[%3d, %2d]\t", c, atoi(yytext));
else if(c == 257)
fprintf(target_file, "[%3d, %2ld]\t", c, strtol(yytext, NULL, 8));
else if(c == 258)
fprintf(target_file, "[%3d, %2ld]\t", c, strtol(yytext, NULL, 16));
else if(c == 259)
fprintf(target_file, "[%3d, %2f]\t", c, atof(yytext));
else
fprintf(target_file, "[%3d, \"%s\"]\t", c, yytext);
}
}
fclose(yyin);
fclose(target_file);
return 0;
}