-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.c
More file actions
50 lines (49 loc) · 1.38 KB
/
lexer.c
File metadata and controls
50 lines (49 loc) · 1.38 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
#include "global.h"
char lexbuf[BSIZE];
int lineno = 1;
int tokenval = NONE;
int lexan() {
int t;
while (1) {
t = getchar();
if (t == ' ' || t == '\t');
else if (t == '\n')
lineno = lineno + 1;
else if (isdigit(t)) {
ungetc(t, stdin);
scanf("%d", &tokenval);
if (tokenval > 32767 && tokenval <= 2147483647) {
return INT32;
} else if (tokenval > 127 && tokenval <= 32767) {
return INT16;
} else if (tokenval >= 0 && tokenval <= 127)
return INT8;
} else if (isalpha(t) || t == '_') {
int p, b = 0;
while (isalnum(t) || t == '_') {
lexbuf[b] = t;
t = getchar();
b = b + 1;
if (b >= BSIZE)
error("compiler error");
}
lexbuf[b] = EOS;
if (t != EOF)
ungetc(t, stdin);
p = lookup(lexbuf);
if (p == 0) {
p = insert(lexbuf, ID);
} if (p >= 52) {
tokenval = p - 49;
} else if (p > 0 && p < 52) {
tokenval = NONE;
}
return symtable[p].token;
} else if (t == EOF)
return DONE;
else {
tokenval = NONE;
return t;
}
}
}