-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
92 lines (82 loc) · 2.11 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
#include "lexer.h"
#include "number.h"
Lexer::Lexer() : index(0){
}
Lexer::~Lexer(){
deleteSymbols();
}
void Lexer::read(string s) {
deleteSymbols();
for(unsigned int i=0;i<s.length();i++) {
if (s[i] >= 48 && s[i] <= 57){
string chaine = "";
do {
chaine += s[i];
i++;
} while(s[i] >= 48 && s[i] <= 57 && i < s.length());
symbols.push_back(new Number(stoi(chaine), INT));
i--;
} else {
Symbol *sym = nullptr;
switch(s[i]) {
case ' ' :
case '\n' :
case '\t' : break;
case '$' : sym = new Symbol(END); break;
case '+' : sym = new Symbol(ADD); break;
case '*' : sym = new Symbol(MUL); break;
case '(' : sym = new Symbol(PO); break;
case ')' : sym = new Symbol(PF); break;
default:
cout << "ERROR : caractere " << s[i] << " lu" << endl;
sym = new Symbol(ERR);
}
if(sym != nullptr) {
symbols.push_back(sym);
}
}
}
}
deque<Symbol*> Lexer::getSymbols() const {
return symbols;
}
string Lexer::print() const {
string chaine = "";
unsigned int ind = 0;
deque<Symbol*>::const_iterator i = symbols.begin();
while(i != symbols.end()) {
if(ind == index)
chaine += ".";
chaine += (*i)->print();
ind++;
i++;
}
chaine += "\n";
return chaine;
}
Symbol* Lexer::next() const {
if(index < symbols.size())
return symbols[index];
else
return nullptr;
}
Symbol* Lexer::shift() {
if(index < symbols.size())
return symbols[index++];
else
return nullptr;
}
void Lexer::deleteSymbols() {
index = 0;
while(!symbols.empty()) {
Symbol *s = this->symbols.back();
symbols.pop_back();
if(s != nullptr) {
delete s;
}
}
}
ostream& operator<<(ostream& os, const Lexer& l) {
os << l.print();
return os;
}