Skip to content

Instantly share code, notes, and snippets.

@NlightNFotis
Last active May 5, 2016 10:48
Show Gist options
  • Save NlightNFotis/65b973ff7fe809f5f6be to your computer and use it in GitHub Desktop.
Save NlightNFotis/65b973ff7fe809f5f6be to your computer and use it in GitHub Desktop.
Symbol table implementation based on a stack.
// Copyright (c) 2015 Fotis Koutoulakis.
// See file w_license for details.
#include "stdafx.h"
#include <iostream>
#include "stack_symtab.h"
// Just return a new empty symbol table.
StackSymbolTable::StackSymbolTable()
{}
// Return a new symbol table, that contains a single name, value binding
StackSymbolTable::StackSymbolTable(std::string name, std::string info)
{
std::map<std::string, std::string> binding;
binding[name] = info;
this->stack.push_back(binding);
}
// Bind a name to a value
void StackSymbolTable::bind(std::string name, std::string info)
{
std::map<std::string, std::string> binding;
binding[name] = info;
this->stack.push_back(binding);
}
// Lookup a name into the stack
std::string StackSymbolTable::lookup(std::string name)
{
std::vector<std::map<std::string, std::string>>::iterator it;
for (it = this->stack.begin(); it < this->stack.end(); it++)
{
// if the nama we are looking for is found in this specific map,
// return the value associated with it.
if (*it->find(name) != *it->end()) {
return it->at(name);
}
}
// else return None
return "None";
}
// Add markings for the new scope that is being entered
void StackSymbolTable::enter()
{
std::map<std::string, std::string> binding;
binding["new"] = "scope";
this->stack.push_back(binding);
}
// Exit the scope.
void StackSymbolTable::exit()
{
std::vector<std::map<std::string, std::string>>::reverse_iterator it;
std::map<std::string, std::string>::const_iterator map_it;
// search until we find the markings of the last scope
for (it = this->stack.rbegin(); it < this->stack.rend(); it++)
{
// if we haven't found them yet, pop the last binding
if (it->find("new") == it->end()) {
this->stack.pop_back();
it = this->stack.rbegin();
//continue;
}
else {
// when they are found, remove the markings,
// and we are ready to go.
this->stack.pop_back();
return;
}
}
}
// debugging assist
void StackSymbolTable::dump_state()
{
std::vector<std::map<std::string, std::string>>::iterator it;
std::map<std::string, std::string>::const_iterator map_it;
// search until we find the markings of the last scope
for (it = this->stack.begin(); it < this->stack.end(); it++)
{
for (map_it = it->begin(); map_it != it->end(); map_it++)
{
std::cout << "Name: " << map_it->first << " Value: " << map_it->second << std::endl;
}
}
std::cout << "---------------------------------" << std::endl;
}
// Copyright (c) 2015 Fotis Koutoulakis.
// See file w_license for details.
#include <vector>
#include <map>
#include <string>
class StackSymbolTable
{
private:
std::vector<std::map<std::string, std::string>> stack;
public:
StackSymbolTable();
StackSymbolTable(std::string name, std::string info);
void bind(std::string name, std::string info);
std::string lookup(std::string name);
void enter();
void exit();
void dump_state();
};
The MIT License (MIT)
Copyright (c) 2015 Fotis Koutoulakis
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment