Last active
May 21, 2019 16:00
-
-
Save anoopsarkar/0a570c6bca560412048cb81401349d38 to your computer and use it in GitHub Desktop.
Symbol table in C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++ -std=c++17 -o symtbl_test symtbl_test.c | |
#include <utility> // std::pair, std::make_pair | |
#include <string> // std::string | |
#include <iostream> // std::cout | |
#include <map> // std::map | |
#include <list> // std::list | |
using namespace std; | |
typedef pair<int, int> descriptor; | |
typedef map<string, descriptor* > symbol_table; | |
typedef list<symbol_table > symbol_table_list; | |
symbol_table_list symtbl; | |
descriptor* access_symtbl(string ident) { | |
for (auto i : symtbl) { | |
auto find_ident = i.find(ident); | |
if (find_ident != i.end()) { | |
return find_ident->second; | |
} | |
} | |
return NULL; | |
} | |
int main() | |
{ | |
symbol_table outer; | |
outer["x"] = new descriptor(1,5); | |
outer["y"] = new descriptor(1,7); | |
symtbl.push_front(outer); | |
descriptor* d = access_symtbl("y"); | |
if (d != NULL) { | |
cout << d->first << '\t' << d->second << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment