Компиляторы. Лаба 5. БОЛЬШЕ ПЛЮСОВ!!!!

  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
185
186
187
188
189
190
191
192
193
194
195
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#define SIZE 128
//#define ITERATOR std::vector<Lexema*>::iterator
int charGroups[SIZE];
std::string finalStates[] = {"SPACE", "NUM", "IDENT", "IDENT", "IDENT",
"IDENT", "IDENT", "IDENT", "IDENT", "KEYWORD"};
int transitions[10][11] = { { 5, 4, 4, 4, 4, 2, 4, 1, 4, -1, 0},
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, -1, 0},
{ 4, 4, 4, 4, 4, 4, 3, 4, 4, -1, 0},
{ 4, 4, 4, 4, 9, 4, 4, 4, 4, -1, 0},
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, -1, 0},
{ 4, 6, 4, 4, 4, 4, 4, 4, 4, -1, 0},
{ 4, 4, 7, 4, 4, 4, 4, 4, 4, -1, 0},
{ 4, 4, 4, 8, 4, 4, 4, 4, 4, -1, 0},
{ 4, 4, 4, 4, 9, 4, 4, 4, 4, -1, 0},
{ 4, 4, 4, 4, 4, 4, 4, 4, 4, -1, 0}};
void InitializeGroups(){
for (int i = 0; i < SIZE; i++){
if ((i > 'a' && i < 'z') || (i > 'A' && i < 'Z')){
charGroups[i] = 8;
} else if(i >= '0' && i <= '9'){
charGroups[i] = 7;
} else charGroups[i] = 9;
}
charGroups['f'] = 0;
charGroups['l'] = 1;
charGroups['o'] = 2;
charGroups['a'] = 3;
charGroups['t'] = 4;
charGroups['i'] = 5;
charGroups['n'] = 6;
charGroups[' '] = 10;
charGroups['\t'] = 10;
charGroups['\n'] = 10;
charGroups['\r'] = 10;
}
struct Position{
int line;
int column;
int index;
};
std::ostream& operator<<(std::ostream& out, Position pos)
{
out << "(" << pos.line << "," << pos.column << ")";
return out;
}
class ProgramText {
private:
std::string program;
Position curentPos;
public:
ProgramText(const std::string& program);
void MoveNext();
bool AtEnd();
Position CurrentPos();
char CurrentChar();
};
ProgramText::ProgramText(const std::string& program){
InitializeGroups();
this->program = program;
curentPos.line = 1;
curentPos.column = 1;
curentPos.index = 0;
}
Position ProgramText::CurrentPos(){
return curentPos;
}
char ProgramText::CurrentChar(){
return program[curentPos.index];
}
void ProgramText::MoveNext(){
curentPos.index++;
curentPos.column++;
if(CurrentChar() == '\n'){
curentPos.line++;
curentPos.column = 0;
}
}
bool ProgramText::AtEnd(){
return curentPos.index == program.length();
}
struct Lexema {
std::string tag;
Position start;
Position follow;
std::string value;
};
std::ostream& operator<<(std::ostream& out, Lexema* lex)
{
out << lex->tag << " " << lex->start << "-" << lex->follow << " " << lex->value;
return out;
}
typedef std::vector<Lexema*>::iterator ITERATOR;
int main(){
std::ifstream fin("program.txt");
std::string program;
std::getline(fin, program, '$');
ProgramText* programText = new ProgramText(program);
std::vector<Lexema*> errors;
int state = 0;
Lexema* lex = new Lexema{"", programText->CurrentPos(),
programText->CurrentPos(), ""};
Lexema* er;
std::string s;
while(!programText->AtEnd()){
if (charGroups[programText->CurrentChar()] != 10){
s += programText->CurrentChar();
}
state = transitions[state][charGroups[programText->CurrentChar()]];
switch(state){
case 0:
if (lex->value != ""){
std::cout << lex << std::endl;
s = (charGroups[programText->CurrentChar()] != 10) ?
programText->CurrentChar() : 0;
}
programText->MoveNext();
lex->start = programText->CurrentPos();
lex->value = "";
break;
case -1:
if (lex->value != ""){
std::cout << lex << std::endl;
lex->value = "";
}
s = "";
lex->start = programText->CurrentPos();
while (charGroups[programText->CurrentChar()] == 9){
programText->MoveNext();
if (charGroups[programText->CurrentChar()] != 10){
s += programText->CurrentChar();
}
}
er = new Lexema{"ERROR", lex->start,
Position{lex->follow.line, lex->follow.column, lex->follow.index}, s};
errors.push_back(er);
s = "";
state = 0;
break;
default:
lex->tag = finalStates[state];
lex->follow = programText->CurrentPos();
lex->value = s;
programText->MoveNext();
}
}
for (ITERATOR it = errors.begin(); it != errors.end(); it++){
std::cout << (*it) << std::endl;
delete *it;
}
delete lex;
return 0;
}