-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.cpp
217 lines (205 loc) · 4.27 KB
/
scan.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "stdafx.h"
#include "scan.h"
#include "util.h"
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <cctype>
std::string token_val;
std::size_t line_num = 0;
Token token = NUL;
std::fstream source_file;
static std::string line_buffer;
static std::string last_line_buffer;
static std::size_t line_pos = 0;
static bool back_to_last_line = false;
std::unordered_map<std::string, Token> reserved_words
{
{ "if", If }, { "then", Then }, { "else", Else }, { "begin", Begin }, { "end", End },
{ "while", While }, { "do", Do }, { "exit", Exit }, { "call", Call }, { "write", Write },
{ "read", Read }, { "procedure", Procedure }, { "function", Function }, { "var", Var },
{ "type", Type }, { "const", Const }, { "integer", Integer }, { "real", Real },
{ "boolean", Boolean }, { "array", Array }, { "of", Of }, { "true", True },
{ "false", False }, { "odd", Odd }, { "or", Or }, { "not", Not },
{ "and", And }, { "div", Div }, { "mod", Mod},
};
static int getNextChar()
{
if (line_pos >= line_buffer.size())
{
last_line_buffer = line_buffer;
while (std::getline(source_file, line_buffer))
{
++line_num;
if (line_buffer.empty()) { continue; }
line_pos = 0;
line_buffer.push_back('\n');
return line_buffer.at(line_pos++);
}
source_file.close();
return EOF;
}
else if (back_to_last_line && !last_line_buffer.empty())
{
return last_line_buffer.back();
}
return line_buffer.at(line_pos++);
}
static void ungetChar(int c)
{
if (line_pos == 0 && last_line_buffer.back() == c) { back_to_last_line = true; }
else if (line_buffer.at(line_pos - 1)) { --line_pos; }
else { std::cerr << "failing to rollback to last char" << std::endl; }
}
static Token reservedLookup(const std::string& str)
{
if (reserved_words.find(str) != reserved_words.end())
{
return reserved_words[str];
}
return Id;
}
Token getToken()
{
token_val.clear();
token_val.reserve(10);
int c = 0;
while ((c = getNextChar()) != EOF)
{
if (isspace(c)) { continue; }
else if (isalpha(c) || '_' == c)
{
token_val.push_back(c);
while (isalnum(c = getNextChar()) || '_' == c)
{
token_val.push_back(c);
}
ungetChar(c);
return reservedLookup(token_val);
}
else if (isdigit(c))
{
do
{
token_val.push_back(c);
} while (isdigit(c = getNextChar()));
if ('.' == c)
{
if (isdigit(c = getNextChar()))
{
token_val.push_back('.');
do
{
token_val.push_back(c);
} while (isdigit(c = getNextChar()));
ungetChar(c);
return RealNum;
}
ungetChar(c);
ungetChar('.');
return IntNum;
}
ungetChar(c);
return IntNum;
}
else if ('/' == c)
{
if ((c = getNextChar()) == '/')
{
line_buffer.clear();
continue;
}
else if ('*' == c)
{
while (true)
{
while ((c = getNextChar()) != '*') {}
if ((c = getNextChar()) != '/') { ungetChar(c); }
else { break; }
}
continue;
}
else
{
ungetChar(c);
token_val.push_back('/');
return DIV;
}
break;
}
else if ('<' == c)
{
token_val.push_back('<');
if ((c = getNextChar()) == '=')
{
token_val.push_back('=');
return LE;
}
else if ('>' == c)
{
token_val.push_back('>');
return NE;
}
ungetChar(c);
return LT;
}
else if ('>' == c)
{
token_val.push_back('>');
if ((c = getNextChar()) == '=')
{
token_val.push_back('=');
return GE;
}
ungetChar(c);
return GT;
}
else if (':' == c)
{
token_val.push_back(':');
if ((c = getNextChar()) == '=')
{
token_val.push_back('=');
return ASSIGN;
}
ungetChar(c);
return COLON;
}
else
{
token_val.push_back(c);
switch (c)
{
case '+':
return PLUS;
case '-':
return MINUS;
case '*':
return MULT;
case '=':
return EQ;
case '{':
return LB;
case '}':
return RB;
case '(':
return LP;
case ')':
return RP;
case '[':
return LA;
case ']':
return RA;
case ';':
return SC;
case ',':
return COMMA;
case '.':
return DOT;
default:
break;
}
}
}
return NUL;
}