-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathlex.h
More file actions
100 lines (81 loc) · 2.11 KB
/
lex.h
File metadata and controls
100 lines (81 loc) · 2.11 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
#ifndef LEX_H
#define LEX_H
#include <stdlib.h>
#include <ctype.h>
#include "input.h"
#include "defs.h"
#include "sym.h"
#include "ast.h"
#define EOP -1
#define ID 1
#define NUM 2
#define OP1 3
#define OP2 4
#define LBR 5
#define RBR 6
#define SEM 7
#define EQ 8
#define MAX_LEN 20
struct Token
{
int type;
int attr;
};
bool looked_ahead = false;
struct Token next_tok;
struct Token lex()
{
int ch;
struct Token tok = {0};
if (looked_ahead) {
looked_ahead = false;
return next_tok;
}
eat:
switch (ch = fgetc(get_file())) {
case ' ': case '\n': goto eat;
case EOF: tok.type = EOP; break;
case '+': tok.type = OP1; tok.attr = ADD_TYPE; break;
case '-': tok.type = OP1; tok.attr = SUB_TYPE; break;
case '*': tok.type = OP2; tok.attr = MUL_TYPE; break;
case '/': tok.type = OP2; tok.attr = DIV_TYPE; break;
case '(': tok.type = LBR; break;
case ')': tok.type = RBR; break;
case '=': tok.type = EQ; break;
case ';': tok.type = SEM; break;
default:
// ID
if (isalpha(ch)) {
char *id_name = malloc(MAX_LEN);
int len = 0;
id_name[len++] = ch;
do {
if (MAX_LEN == len)
fatal("Lexer: Variable name is too long");
id_name[len++] = (ch = fgetc(get_file()));
} while (isalpha(ch) || isdigit(ch));
id_name[len - 1] = '\0';
tok.type = ID;
tok.attr = add_sym(id_name);
// NUM
} else if (isdigit(ch)) {
int val = ch - '0';
while (isdigit(ch = fgetc(get_file())))
val = val * 10 + (ch - '0');
tok.type = NUM;
tok.attr = val;
// Error
} else {
fatal("Lexer: Unexpected character");
}
ungetc(ch, get_file());
}
return tok;
}
struct Token lookahead()
{
next_tok = lex();
looked_ahead = true;
return next_tok;
}
#endif