-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
149 lines (133 loc) · 3.63 KB
/
lexer.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
#include <iostream>
#include <algorithm>
#include "lexer.h"
#include "table.cpp"
//precarrega na tabela as palavras reservadas
Lexer::Lexer(std::string filename) :
line(1),buffer(filename)
{
peek = ' ';
for (int i = 0, keyword = PROGRAMAINICIO; i < NUMBER_OF_KEYWORDS;
i++, keyword++) {
Token w(keyword,0, keywords[i]);
install(w);
}
}
int Lexer::getLinesRead(){
return line;
}
void Lexer::install(Token w) {
reservedWords.insert(std::make_pair((std::string) w.lexeme, w)); // @suppress("Invalid arguments")
}
void Lexer::deleteUntilDelimiter() {
while (!isDelimiter(buffer.lookAhead())) {
peek = buffer.next();
}
}
Token* Lexer::getCurrentToken(){
return curToken;
}
Token * Lexer::scan() {
peek = buffer.next();
//consome caracteres em branco, linhas de comentário
//,LF,CR até o próximo caractere válido.
while (true) {
if (peek == ' ' || peek == '\t' || peek == '\r');
else if (peek == '\n')
line++;
else if (peek == '#') {
while (true) {
peek = buffer.next();
if (peek == '\n') {
line++;
break;
} else if (peek == END_FILE) {
curToken = new Token(END_OF_FILE, line, "");
return curToken;
}
}
} else break;
peek = buffer.next();
}
//tokeniza id ou keyword
if (isalpha(peek) || peek == '_') {
std::string lex("");
lex += peek;
char look = buffer.lookAhead();
while (isdigit(look) || isalpha(look) || look == '_') {
peek = buffer.next();
look = buffer.lookAhead();
lex += peek;
if (lex.size() >= 150) {
std::cout << "AVISO::LINHA:" << line
<< ": Limite de caracteres(150) excedido para o identificador. Desconsiderando caracteres seguintes....>>"
<< lex << "<<" << std::endl;
deleteUntilDelimiter();
}
}
if (!isDelimiter(buffer.lookAhead())) {
std::string tmp;
while (!isDelimiter(buffer.lookAhead())) {
peek = buffer.next();
tmp += peek;
}
std::cout << "AVISO::LINHA:" << line
<< ": Simbolos estranhos sendo ignorados. >> " << tmp
<< " <<" << std::endl;
}
std::unordered_map<std::string, Token>::iterator it = reservedWords.find(lex);
if (it != reservedWords.end()) {
curToken = new Token(it->second.getTag(),line,it->second.getLexeme());
return curToken;
}
curToken = new Token(ID,line ,lex);
return curToken;
}
// tokeniza numero
if (isdigit(peek)) {
std::string lex("");
lex += peek;
while (isdigit(buffer.lookAhead())) {
peek = buffer.next();
lex += peek;
if (lex.size() > 9) {
deleteUntilDelimiter();
std::cout << "ERRO::LINHA:" << line
<< ": O numero excede o limite de digitos(9), >>"
<< lex << "<<.\n";
curToken = new Token(ERROR,line, "NUMBER_TOO_BIG");
return curToken;
}
}
if (!isDelimiter(buffer.lookAhead())) {
while (!isDelimiter(buffer.lookAhead())) {
lex += buffer.next();
if (lex.size() > 150) {
deleteUntilDelimiter();
break;
}
}
std::cout << "ERRO::LINHA:" << line << ": Numero mal formado. >> "
<< lex << " <<\n";
curToken = new Token(ERROR,line, "NUMBER_MALFORMED");
return curToken;
}
curToken = new Token(NUMERO,line, lex);
return curToken;
}
if (peek == END_FILE) {
curToken = new Token(END_OF_FILE,line, "");
return curToken;
} else {
std::string lex("");
while (!isDelimiter(buffer.lookAhead())) {
lex += peek;
peek = buffer.next();
}
std::cout << "ERRO::LINHA:" << line
<< ": Impossivel associar simbolos a qualquer padrao conhecido. >> "
<< lex << "\n";
}
curToken = new Token(ERROR,line, "");
return curToken;
}