-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyc_lex.l
More file actions
63 lines (51 loc) · 1.31 KB
/
tinyc_lex.l
File metadata and controls
63 lines (51 loc) · 1.31 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
D [0-9]
L [a-zA-Z_]
%{
#define YYSTYPE char*
#include "y.tab.h"
void comment();
void yyerror();
%}
%%
"#include" { return INCLUDE; }
#[^\n]* { }
"//"[^\n]* { }
"/*" { comment(); }
[ \t\n]+ { }
"{" { return OPEN_BRACE; }
"}" { return CLOSE_BRACE; }
"(" { return '('; }
")" { return ')'; }
"~" { return '~'; }
"!" { return '!'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"<" { return '<'; }
"<=" { return LESS_OR_EQUAL; }
"=" { return '='; }
";" { return ';'; }
"," { return ','; }
[0-9]+ {
//检查数字在合法范围内,不能以0开头
yylval = strdup(yytext); return NUMBER;
}
"if" { return IF; }
"while" { return WHILE; }
"return" { return RETURN; }
"int" { return TYPE; }
{L}({L}|{D})* { yylval = strdup(yytext); return IDENTIFIER; }
"<"[a-z.]+">" { return HEADER_NAME; }
%%
#define INPUT_EOF 0
//多行注释处理
void comment(void) {
char c, prev = 0;
//跳过,直到*/出现
while ((c = input()) != INPUT_EOF) {
if (c == '/' && prev == '*')
return;
prev = c;
}
yyerror("unterminated comment");
}