-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexeme.cpp
154 lines (150 loc) · 5.28 KB
/
lexeme.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
/*
* lexeme.cpp
*
* Lucas Bastos
* CS280
* Spring 2020
*/
#include "lex.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;
string tokens[] = {"PRINT","PRINTLN","REPEAT", "BEGIN", "END", "IDENT", "ICONST", "SCONST", "PLUS", "MINUS", "STAR", "SLASH", "EQ", "LPAREN", "RPAREN", "SC", "ERR", "DONE"};
void endprint(bool checker, map<string, int> &map, string out) {
if (checker && map.size() > 0) {
cout << out;
int i = 0;
for (auto it=map.begin();it!=map.end();it++) {
cout << " " << it->first;
if (++i < (int)map.size()) cout << ",";
}
cout << endl;
}
}
Tok establish(string &lexeme, int linenum) {
string lower = lexeme;
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
if (lower == "print") {
return Tok(PRINT, lexeme, linenum);
} else if (lower == "println") {
return Tok(PRINTLN, lexeme, linenum);
} else if (lower == "repeat") {
return Tok(REPEAT, lexeme, linenum);
} else if (lower == "begin") {
return Tok(Token::BEGIN, lexeme, linenum);
} else if (lower == "end") {
return Tok(END, lexeme, linenum);
} else if (isalpha(lexeme[lexeme.length()-1]) || isdigit(lexeme[lexeme.length()-1])){
return Tok(IDENT, lexeme, linenum);
} else {
return Tok(ERR, lexeme, linenum);
}
}
ostream& operator<<(ostream& out, const Tok& tok) {
out << tokens[tok.GetToken()];
return out;
}
Tok getNextToken(istream& in, int& linenum) {
enum TokState {BEGIN, INID, INSTRING, ININT, INCOMMENT};
TokState lexstate = BEGIN;
string lexeme;
char ch;
bool escape = false;
while(in.get(ch)) {
switch(lexstate) {
case BEGIN:
if( ch == '\n' ) {
linenum++;
continue;
}
if(isspace(ch)) continue;
lexeme = ch;
if(isalpha(ch)) {
if (isalpha((char)in.peek()) || isdigit((char)in.peek())) {
lexstate = INID;
continue;
}
return Tok(IDENT, lexeme, linenum);
} else if (isdigit(ch)) {
if (isdigit((char)in.peek())) {
lexstate = ININT;
continue;
}
return Tok(ICONST, lexeme, linenum);
} else if (ch == '"') {
lexstate = INSTRING;
} else if (ch == '/') {
if ((char)in.peek()=='/') {
lexstate = INCOMMENT;
continue;
}
return Tok(SLASH, lexeme, linenum);
} else if (ch == '-') {
return Tok(MINUS, lexeme, linenum);
} else if (ch == '+') {
return Tok(PLUS, lexeme, linenum);
} else if (ch == '*') {
return Tok(STAR, lexeme, linenum);
} else if (ch == '=') {
return Tok(EQ, lexeme, linenum);
} else if (ch == '(') {
return Tok(LPAREN, lexeme, linenum);
} else if (ch == ')') {
return Tok(RPAREN, lexeme, linenum);
} else if (ch == ';') {
return Tok(SC, lexeme, linenum);
} else {
return Tok(ERR, lexeme, linenum);
}
break;
case INID:
if ((isdigit(ch) || isalpha(ch))) lexeme += ch;
if (!isdigit((char)in.peek()) && !isalpha((char)in.peek())) {
lexstate = BEGIN;
return establish(lexeme, linenum);
}
break;
case INSTRING:
if (!escape) {
if (ch == '\\') {
escape = true;
continue;
}
lexeme += ch;
if (ch == '"') {
lexstate = BEGIN;
return Tok(SCONST, lexeme, linenum);
}
if (ch == '\n') return Tok(ERR, lexeme, linenum);
} else {
if (ch == 'n') {
lexeme += '\n';
} else {
lexeme += ch;
}
escape = false;
}
break;
case ININT:
if ((isdigit(ch))) lexeme += ch;
if (!isdigit((char)in.peek())) {
lexstate = BEGIN;
return Tok(ICONST, lexeme, linenum);
}
break;
case INCOMMENT:
if (ch == '\n') {
lexstate = BEGIN;
linenum++;
} else {
lexeme = "";
}
break;
}
}
if (in.eof()) return Tok(DONE, lexeme, linenum);
return establish(lexeme, linenum);
}