forked from Wilfred/babyc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babyc_lex.l
62 lines (50 loc) · 1.43 KB
/
babyc_lex.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
D [0-9]
L [a-zA-Z_]
%{
#define YYSTYPE char*
#include "y.tab.h"
void comment();
void error();
%}
%%
"#include" { return INCLUDE; }
#[^\n]* { /* Discard preprocessor comments. */ }
"//"[^\n]* { /* Discard c99 comments. */ }
"/*" { comment(); }
[ \t\n]+ { /* Ignore whitespace */ }
"{" { return OPEN_BRACE; }
"}" { return CLOSE_BRACE; }
"(" { return '('; }
")" { return ')'; }
"~" { return '~'; }
"!" { return '!'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"<" { return '<'; }
"<=" { return LESS_OR_EQUAL; }
"=" { return '='; }
";" { return ';'; }
"," { return ','; }
[0-9]+ {
/* TODO: check numbers are in the legal range, and don't start with 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) {
/* Consume characters up to the closing comment marker. */
char c, prev = 0;
while ((c = input()) != INPUT_EOF) {
if (c == '/' && prev == '*')
return;
prev = c;
}
error("unterminated comment");
}