-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
168 lines (140 loc) · 4.69 KB
/
lexer.cpp
File metadata and controls
168 lines (140 loc) · 4.69 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
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
#include "lexer.h"
#include "token.h"
#include <vector>
const array<string, 14> KEYWORDS = {"if", "else", "ifend",
"while", "whileend",
"int", "boolean", "real",
"function", "put", "get",
"return", "true", "false"};
const array<string, 11> OPERATORS = {"==", ">", "<",
"=>", "=<", "^=",
"+", "-", "*",
"/", "="};
const array<string, 8> SEPARATORS = {"(", ")", ",",
"{", "}", ";",
":", "$$"};
/*
Integer and Real number Finite state machine
Accepting state: 2 (integer), 3 (real)
*/
int intRealFSM[5][3] = {{1, 4, 4},
{1, 2, 4},
{3, 4, 4},
{3, 4, 4},
{4, 4, 4}};
/*
Identifier Finite state machine
Accepting state: 2
*/
int identifierFSM[5][3] = {{1, 4, 4},
{2, 3, 4},
{2, 3, 4},
{2, 3, 4},
{4, 4, 4}};
int intRealCharToCol(char c)
{
return c == '.' ? 1 : isdigit(c) ? 0 : 2;
}
Token::Type intRealDFSM(string sequence)
{
int state = 0;
for (size_t i = 0; i < sequence.size(); ++i)
{
int col = intRealCharToCol(sequence.at(i));
state = intRealFSM[state][col];
}
return state == 1 ? Token::Type::INTEGER : state == 3 ? Token::Type::REAL : Token::Type::UNKNOWN;
}
int identifierCharToCol(const char &c)
{
return isalpha(c) ? 0 : isdigit(c) ? 1 : 2;
}
Token::Type identifierDFSM(string sequence)
{
int state = 0;
for (size_t i = 0; i < sequence.size(); ++i)
{
int col = identifierCharToCol(sequence.at(i));
state = identifierFSM[state][col];
}
return state == 1 || state == 2 ? Token::Type::IDENTIFIER : Token::Type::UNKNOWN;
}
Lexer::Lexer(ifstream &file) :
m_input(file),
m_currentLine(1),
m_c(),
m_buffer("") {}
Lexer::~Lexer() {}
Token::Wrapper Lexer::getToken() {
m_buffer = "";
stripChars();
m_input.get(m_c);
if (isalpha(m_c)) {
while (!m_input.eof() && !isspace(m_c) && !isToken(string(1, m_c), SEPARATORS) && !isToken(string(1, m_c), OPERATORS)) {
m_buffer += m_c;
m_input.get(m_c);
}
m_input.unget();
if (isToken(m_buffer, KEYWORDS))
return Token::Wrapper(Token::Type::KEYWORD, m_buffer, m_currentLine);
else
return Token::Wrapper(identifierDFSM(m_buffer), m_buffer, m_currentLine);
}
else if (isdigit(m_c)) {
while (!m_input.eof() && !isspace(m_c) && !isToken(string(1, m_c), SEPARATORS) && !isToken(string(1, m_c), OPERATORS)) {
m_buffer += m_c;
m_input.get(m_c);
}
m_input.unget();
return Token::Wrapper(intRealDFSM(m_buffer), m_buffer, m_currentLine);
}
else if (isToken(string(1, m_c), SEPARATORS)) {
return Token::Wrapper(Token::Type::SEPARATOR, string(1, m_c), m_currentLine);
}
else {
while (!m_input.eof() && !isspace(m_c) && !isToken(string(1, m_c), SEPARATORS) && !isToken(string(1, m_c), OPERATORS)) {
m_buffer += m_c;
m_input.get(m_c);
}
if (isToken(string(1, m_c), OPERATORS)) {
while (!m_input.eof() && !isspace(m_c) && !isalpha(m_c) && !isdigit(m_c) && !isToken(string(1, m_c), SEPARATORS)) {
m_buffer += m_c;
m_input.get(m_c);
}
m_input.unget();
return Token::Wrapper(Token::Type::OPERATOR, m_buffer, m_currentLine);
}
else if (isToken(m_buffer, SEPARATORS))
return Token::Wrapper(Token::Type::SEPARATOR, m_buffer, m_currentLine);
else
return Token::Wrapper(Token::Type::UNKNOWN, m_buffer, m_currentLine);
}
}
int Lexer::getCurrentLine() const {
return m_currentLine;
}
void Lexer::stripChars() {
while (m_input.get(m_c)) {
if ((int) m_c == 10)
m_currentLine++;
else if (m_c == '[' && m_input.peek() == '*') {
while (m_input.get(m_c)) {
if ((int) m_c == 10)
m_currentLine++;
else if (m_c == ']')
break;
}
}
else if (!isspace(m_c)) {
m_input.unget();
break;
}
}
}
template<size_t SIZE>
bool Lexer::isToken(const string &lexeme, const array<string, SIZE> &arr) {
for (auto &token : arr)
if (token == lexeme)
return true;
return false;
}