-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.c
184 lines (167 loc) · 4.71 KB
/
scan.c
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
#include "defs.h"
#include "data.h"
#include "decl.h"
// Structure and enum definitions
// Copyright (c) 2019 Warren Toomey, GPL3
// Modification (c) 2022 Emin, GPL3
// Get the next character from the input file. If putback is true, then we are
// putting back a character, and we should not get a new one.
static int next(void) {
int c;
if (Putback) {
c = Putback;
Putback = 0;
return c;
}
// inputFile is a global variable
c = fgetc(inputFile);
if (c == '\n')
Line++;
return c;
}
// Put back a character that we don't want
static void putback(int c) {
Putback = c;
}
// Skip past input that we don't need to deal with, i.e. whitespace, newlines.
// Return the first character we do need to deal with.
static int skip(void) {
int c;
c = next();
while (isspace(c))
c = next();
return c;
}
// Scan and return an integer literal value from the input file. Store the
// value as a string in Text.
static int scanint(int c) {
int val = 0;
// Convert each character into an int value
while (isdigit(c)) {
val = val * 10 + c - '0';
c = next();
}
// We hit a non-integer character, put it back.
putback(c);
return val;
}
// Scan an identifier from the input file and
// store it in buf[]. Return the identifier's length
static int scanident(int c, char* buf, int lim) {
int i = 0;
// Allow digits, alpha and underscores
while (isalpha(c) || isdigit(c) || '_' == c) {
// Error if we hit the identifier length limit,
// else append to buf[] and get next character
if (lim - 1 == i) {
fatal("Identifier too long");
} else if (i < lim - 1) {
buf[i++] = c;
}
c = next();
}
// We hit a non-valid character, put it back.
// NUL-terminate the buf[] and return the length
putback(c);
buf[i] = '\0';
return (i);
}
// Given a word from the input, return the matching
// keyword token number or 0 if it's not a keyword.
// Switch on the first letter so that we don't have
// to waste time strcmp()ing against all the keywords.
static int keyword(char* s) {
switch (*s) {
case 'i':
if (!strcmp(s, "int"))
return (T_INT);
break;
case 'p':
if (!strcmp(s, "print"))
return (T_PRINT);
break;
}
return 0;
}
// Scan and return the next token found in the input. Return 1 if token valid,
// 0 if no tokens left.
int scan(struct token* t) {
int c, tokentype;
// Skip whitespace
c = skip();
// Determine the token based on the input character
switch (c) {
case EOF:
return t->token = T_EOF;
case '+':
t->token = T_PLUS;
break;
case '-':
t->token = T_MINUS;
break;
case '*':
t->token = T_STAR;
break;
case '/':
t->token = T_SLASH;
break;
case ';':
t->token = T_SEMI;
break;
case '=':
if ((c = next()) == '=') {
t->token = T_EQ;
} else {
putback(c);
t->token = T_ASSIGN;
}
break;
case '!':
if ((c = next()) == '=') {
t->token = T_NE;
} else {
fatalc("Unrecognised character", c);
}
break;
case '<':
if ((c = next()) == '=') {
t->token = T_LE;
} else {
putback(c);
t->token = T_LT;
}
break;
case '>':
if ((c = next()) == '=') {
t->token = T_GE;
} else {
putback(c);
t->token = T_GT;
}
break;
default:
// If it's a digit, scan the
// literal integer value in
if (isdigit(c)) {
t->intvalue = scanint(c);
t->token = T_INTLIT;
break;
} else if (isalpha(c) || '_' == c) {
// Read in a keyword or identifier
scanident(c, Text, TEXTLEN);
// If it's a recognised keyword, return that token
tokentype = keyword(Text);
if (tokentype) {
t->token = tokentype;
break;
}
// Otherwise, return an identifier token
t->token = T_IDENT;
break;
}
// The character isn't part of any recognised token, error
fatalc("Unrecognised character", c);
}
// We found a token
return 1;
}