-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.cpp
179 lines (152 loc) · 4.36 KB
/
lex.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
#include <cctype>
#include <map>
using std::map;
#include "lexer.h"
static map<TokenType,string> tokenPrint = {
{ T_INT, "T_INT" },
{ T_STRING, "T_STRING" },
{ T_SET, "T_SET" },
{ T_PRINT, "T_PRINT" },
{ T_PRINTLN, "T_PRINTLN" },
{ T_ID, "T_ID" },
{ T_ICONST, "T_ICONST" },
{ T_SCONST, "T_SCONST" },
{ T_PLUS, "T_PLUS" },
{ T_MINUS, "T_MINUS" },
{ T_STAR, "T_STAR" },
{ T_SLASH, "T_SLASH" },
{ T_LPAREN, "T_LPAREN" },
{ T_RPAREN, "T_RPAREN" },
{ T_SC, "T_SC" },
{ T_ERROR, "T_ERROR" },
{ T_DONE, "T_DONE" }
};
//
ostream& operator<<(ostream& out, const Token& tok) {
TokenType tt = tok.GetTokenType();
out << tokenPrint[ tt ];
if( tt == T_ID || tt == T_ICONST || tt == T_SCONST || tt == T_ERROR ) {
out << "(" << tok.GetLexeme() << ")";
}
return out;
}
//
//
static map<string,TokenType> kwmap = {
{ "int", T_INT },
{ "string", T_STRING },
{ "set", T_SET },
{ "print", T_PRINT },
{ "println", T_PRINTLN },
};
Token
id_or_kw(const string& lexeme)
{
TokenType tt = T_ID;
auto kIt = kwmap.find(lexeme);
if( kIt != kwmap.end() )
tt = kIt->second;
return Token(tt, lexeme);
}
Token
getToken(istream* br)
{
enum LexState { BEGIN, INID, INSTRING, ININT, ONESLASH, INCOMMENT } lexstate = BEGIN;
string lexeme;
for(;;) {
int ch = br->get();
if( br->bad() || br->eof() ) break;
if( ch == '\n' ) {
extern int lineNumber;
++lineNumber;
}
switch( lexstate ) {
case BEGIN:
if( isspace(ch) )
continue;
lexeme = ch;
if( isalpha(ch) ) {
lexstate = INID;
}
else if( ch == '"' ) {
lexstate = INSTRING;
}
else if( isdigit(ch) ) {
lexstate = ININT;
}
else if( ch == '/' ) {
lexstate = ONESLASH;
}
else {
TokenType tt = T_ERROR;
switch( ch ) {
case '+':
tt = T_PLUS;
break;
case '-':
tt = T_MINUS;
break;
case '*':
tt = T_STAR;
break;
case '(':
tt = T_LPAREN;
break;
case ')':
tt = T_RPAREN;
break;
case ';':
tt = T_SC;
break;
}
return Token(tt, lexeme);
}
break;
case INID:
if( isalpha(ch) || isdigit(ch) ) {
lexeme += ch;
}
else {
br->putback(ch);
return id_or_kw(lexeme);
}
break;
case INSTRING:
lexeme += ch;
if( ch == '\n' ) {
return Token(T_ERROR, lexeme );
}
if( ch == '"' ) {
return Token(T_SCONST, lexeme );
}
break;
case ININT:
if( isdigit(ch) ) {
lexeme += ch;
}
else if( isalpha(ch) ) {
lexeme += ch;
return Token(T_ERROR, lexeme);
}
else {
br->putback(ch);
return Token(T_ICONST, lexeme);
}
break;
case ONESLASH:
if( ch != '/' ) {
br->putback(ch);
return Token(T_SLASH, lexeme );
}
lexstate = INCOMMENT;
break;
case INCOMMENT:
if( ch == '\n' ) {
lexstate = BEGIN;
}
break;
}
}
if( br->eof() ) return T_DONE;
return T_ERROR;
}