-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
55 lines (47 loc) · 1.25 KB
/
scanner.cpp
File metadata and controls
55 lines (47 loc) · 1.25 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
// $Id: scanner.cpp,v 1.8 2015-07-02 16:48:18-07 - - $
#include <iostream>
#include <locale>
using namespace std;
#include "scanner.h"
#include "debug.h"
scanner::scanner() {
seen_eof = false;
advance();
}
void scanner::advance() {
if (not seen_eof) {
cin.get (lookahead);
if (cin.eof()) seen_eof = true;
}
}
token_t scanner::scan() {
token_t result;
while (not seen_eof and isspace (lookahead)) advance();
if (seen_eof) {
result.symbol = tsymbol::SCANEOF;
}else if (lookahead == '_' or isdigit (lookahead)) {
result.symbol = tsymbol::NUMBER;
do {
result.lexinfo += lookahead;
advance();
}while (not seen_eof and isdigit (lookahead));
}else {
result.symbol = tsymbol::OPERATOR;
result.lexinfo += lookahead;
advance();
}
DEBUGF ('S', result);
return result;
}
ostream& operator<< (ostream& out, const tsymbol& symbol) {
switch (symbol) {
case tsymbol::NUMBER : out << "NUMBER" ; break;
case tsymbol::OPERATOR: out << "OPERATOR"; break;
case tsymbol::SCANEOF : out << "SCANEOF" ; break;
}
return out;
}
ostream& operator<< (ostream& out, const token_t& token) {
out << token.symbol << ": \"" << token.lexinfo << "\"";
return out;
}