/
keywordTable.h
74 lines (59 loc) · 1.98 KB
/
keywordTable.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
// Version 1.0
#ifndef _TABLICA_SLOW_KLUCZOWYCH_H_
#define _TABLICA_SLOW_KLUCZOWYCH_H_
#include "parser.h"
#include <map>
#include <vector>
#include <iterator>
#include <algorithm>
#include <utility>
class KeywordTable {
public:
explicit KeywordTable(int __notKeyword)
: _notKeyword(__notKeyword)
{
addKeyword("double", Parser::DOUBLE);
addKeyword("string", Parser::STRING);
addKeyword("while", Parser::WHILE);
addKeyword("if", Parser::IF);
addKeyword("return", Parser::RETURN);
addKeyword("printf", Parser::PRINTF);
addKeyword("scanf", Parser::SCANF);
addKeyword("else", Parser::ELSE);
addKeyword("endl", Parser::ENDL);
}
virtual ~KeywordTable() {}
virtual int notKeyword() const {
return _notKeyword; }
// Podajemy co ma zwr�cic tablica je�eli lekser znajdzie identifier
// nie b�d�cy s�owem kluczowym
virtual void notKeyword(int __notKeyword) {
_notKeyword = __notKeyword; }
// Dodajemy s�owo kluczowe oraz jego warto�c
virtual bool addKeyword(const std::pair<const std::string, int>& __keyword)
{
return (_keywords.insert(__keyword)).second;
}
// Dodajemy s�owo kluczowe oraz jego warto�c
virtual bool addKeyword(const char* __keyword, int __value)
{
return addKeyword(make_pair(std::string(__keyword), __value));
}
// Sprawdzamy, czy znaleziony przez lekser identifier jest s�owem kluczowym
inline virtual int findKeyword(const std::string& __keyword) const;
protected:
int _notKeyword;
private:
std::map<const std::string, int> _keywords;
};
/* ============================================================== findKeyword */
inline int KeywordTable::findKeyword(const std::string& __keyword) const
{
std::map<const std::string, int>::const_iterator p = _keywords.find(__keyword);
if (p != _keywords.end()) {
return p->second;
} else {
return _notKeyword;
}
}
#endif