-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical.h
184 lines (163 loc) · 3.65 KB
/
lexical.h
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
179
180
181
182
183
184
#include <map>
#include <iostream>
#include <string>
#include <fstream>
std::ifstream read_file;
std::ofstream output_file;
int line_number = 1, column_number = 1;
std::map<std::string, std::string> tokens = {
{"PROGRAM", "RW_PROGRAM"},
{"VAR", "RW_VAR"},
{"CONST", "RW_CONST"},
{"PROCEDURE", "RW_PROCEDURE"},
{"FUNCTION", "RW_FUNCTION"},
{"LABEL", "RW_LABEL"},
{"BEGIN", "RW_BEGIN"},
{"DOWNTO", "RW_DOWNTO"},
{"END", "RW_END"},
{"IF", "RW_IF"},
{"THEN", "RW_THEN"},
{"ELSE", "RW_ELSE"},
{"RAISE", "RW_RAISE"},
{"CATCH", "RW_CATCH"},
{"TRY", "RW_TRY"},
{"FINALLY", "RW_FINALLY"},
{"RECORD", "RW_RECORD"},
{"REPEAT", "RW_REPEAT"},
{"TYPE", "RW_TYPE"},
{"UNTIL", "RW_UNTIL"},
{"USES", "RW_USES"},
{"WHILE", "RW_WHILE"},
{"FOR", "RW_FOR"},
{"DO", "RW_DO"},
{"OR", "RW_OR"},
{"IN", "RW_IN"},
{"AND", "RW_AND"},
{"NOT", "RW_NOT"},
{"DIV", "RW_DIV"},
{"INTEGER", "RW_INTEGER"},
{"ARRAY", "RW_ARRAY"},
{"STRING", "RW_STRING"},
{":=", "SS_ASSIGN"},
{"+", "SS_PLUS"},
{"-", "SS_MINUS"},
{"*", "SS_MULTIPLY"},
{"/", "SS_DIVIDE"},
{"<", "SS_LESS"},
{">", "SS_GREATER"},
{"=", "SS_EQUAL"},
{"(", "SS_OPEN_PARENTHESIS"},
{")", "SS_CLOSE_PARENTHESIS"},
{".", "SS_DOT"},
{",", "SS_COMMA"},
{";", "SS_SEMICOLON"},
{":", "SS_COLON"},
};
void write_output(std::string token)
{
output_file << std::noskipws << token << ' ';
}
void write_output(char capsuled_char)
{
output_file << std::noskipws << capsuled_char;
}
// FUNÇÃO PRÓXIMO
bool proximo(char &next_char)
{
if (read_file.good() && read_file.peek() != EOF)
{
read_file >> std::noskipws >> next_char;
if (next_char == '\n')
{
line_number++;
column_number = 1;
}
else if (next_char == '\t')
{
column_number += 4;
}
else
{
column_number++;
if (next_char >= 'a' && next_char <= 'z')
next_char = toupper(next_char);
}
return true;
}
return false;
}
// CODIGO (TOKENIZADOR)
std::string codigo(std::string atom, int type)
{
switch (type)
{
case 1:
return "NUMBER_" + atom;
case 2:
return "IDENTIFIER_" + atom;
default:
return tokens[atom];
}
}
// FUNÇÃO ERRO
void erro()
{
throw std::runtime_error("Token inválido na linha " + std::to_string(line_number) + " na posição " + std::to_string(column_number));
}
std::string lexical_analysis(char &next_char)
{
std::string atom = "";
while (next_char == '\n' || next_char == '\t' || next_char == ' ')
{
if (next_char != ' ')
write_output(next_char);
proximo(next_char);
}
if (tokens[std::string(1, next_char)] != "")
{
atom = next_char;
proximo(next_char);
if (atom == ":" && next_char == '=')
{
atom += next_char;
proximo(next_char);
}
return codigo(atom, 0);
}
else
{
if (next_char <= 'Z' && next_char >= 'A')
{
do
{
atom += next_char;
proximo(next_char);
} while (next_char <= 'Z' && next_char >= 'A' || next_char <= '9' && next_char >= '0');
if (tokens[atom] != "")
return codigo(atom, 0); // reserved word
else
return codigo(atom, 2); // identifier
}
else
{
if (next_char <= '9' && next_char >= '0')
{
do
{
atom += next_char;
proximo(next_char);
} while (next_char <= '9' && next_char >= '0');
if (next_char <= 'Z' && next_char >= 'A')
{
erro();
}
return codigo(atom, 1); // number
}
else
{
erro();
}
}
}
return ""; // unreachable
}