-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.h
54 lines (50 loc) · 1.43 KB
/
symbols.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
/*******************************************************************************
File: symbols.h
Author: Dylan Bryan
Date: 8/9/21, 8:41 PM
Project: project-four
Purpose: Defines the class for symbol table
******************************************************************************/
template <typename T>
class Symbols
{
public:
void insert(char* lexeme, T entry);
bool find(char* lexeme, T& entry);
bool findDuplicate(char* lexeme, T& entry);
private:
map<string, T> symbols;
};
template <typename T>
void Symbols<T>::insert(char* lexeme, T entry)
{
string name(lexeme);
symbols[name] = entry;
}
template <typename T>
bool Symbols<T>::find(char* lexeme, T& entry)
{
string name(lexeme);
typedef typename map<string, T>::iterator Iterator;
Iterator iterator = symbols.find(name);
bool found = iterator != symbols.end();
if (found)
{
entry = iterator->second;
}
return found;
}
template <typename T>
bool Symbols<T>::findDuplicate(char* lexeme, T& entry)
{
string name(lexeme);
typedef typename map<string, T>::iterator Iterator;
Iterator iterator = symbols.find(name);
bool found = iterator != symbols.end();
if (found)
{
appendError(GENERAL_SEMANTIC, "Duplicate Variable Found");
entry = iterator->second;
}
return found;
}