Cell* search_symbol(string s, bool expected)
{
  //stack<symbol_map> temp_AR_stack;
  symbol_map curr_map;
  symbol_map::iterator curr_map_it;
  bool exist = false;
  Cell* linked_symbol = NULL;

/*  while (!(AR_stack.empty()))
  {
    curr_map = AR_stack.top();
    curr_map_it = curr_map.find(s);
    // Check if the current record (map) has the mapped value
    if (curr_map_it != curr_map.end())
    {
      linked_symbol = curr_map_it->second;
      exist = true;
      break;
    }
    else
    {
      temp_AR_stack.push(curr_map);
      AR_stack.pop();
    }
  }
*/
  for (int i = AR_vector.size()-1; i>=0; i--)
  {
    curr_map = AR_vector[i];
    curr_map_it = curr_map.find(s);
    //Check if the current record (map) contains the mapped value
   if (curr_map_it != curr_map.end())
   {
     linked_symbol = curr_map_it->second;
     exist = true;
     break;
   }
  }
  /*
   * after searching the symbol map, if it is still not found,
   * check the global variable record
   */
  if (!exist)
  {
    SMap_it = SMap.find(s);
    if ((SMap_it == SMap.end())&&expected)
    {
      //if (!Symbol_operator_finder(s))
        throw runtime_error("attempt to reference an undefined symbol \"" + s
        +"\"");
      //return make_symbol(s.c_str());
    }
    else if (SMap_it == SMap.end())
    	linked_symbol = NULL;
    else
        linked_symbol = SMap_it->second;
  }

  return linked_symbol;
}
void insert_element(string s, Cell* map_cell)
{

  //check if there is any local symbol table
  if (!(AR_vector.empty()))
  {
    symbol_map curr_map = AR_vector.back();
    AR_vector.pop_back();
    symbol_map::iterator curr_map_it = curr_map.find(s);

    //check if it is defined before
    if (curr_map_it != curr_map.end())
    {
      AR_vector.push_back(curr_map);
      throw runtime_error(s + " cannot be redefined.");
    }
    curr_map[s] = map_cell;
    AR_vector.push_back(curr_map);
  }
  else
  {
    SMap_it = SMap.find(s);

    //check if it is defined before
    if (SMap_it != SMap.end())
      throw runtime_error(s + " cannot be redefined.");
    SMap.insert( pair< string, Cell*>(s,map_cell));
  }
}