Question

Im building a symbol table and im having a hard time writing get_symbol, which has the arguments of (symbol_table symtab, char sym). I have to write code for 2 functions get_symbol and set_symbol, I'm having some trouble understanding what a symbol table is and how to write out these two functions. it says that get_symbol() has two arguments: the symbol table, and the symbol name whose value should be retrieved. If the symbol name is one of A,B,C the corresponding value from the symbol table should be returned. if it is not one of those names, then 0 should be returned. If any one could help that would be great thanks for your time.

symbol_table.c Code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #include "globals.h"
    #include "symbol_table.h"

    typedef struct symbol_table_s {
            int a,b,c;
    }*symbol_table_rep;


    status init_symbol_table (symbol_table *p_symtab)
    {
    symbol_table_rep st = malloc(sizeof(struct symbol_table_s));
    if (st == NULL)
            return ERROR;
    st->a = st->b = st->c = 0;
    *p_symtab = (symbol_table) st;
    return OK;
    }

    void destroy_symbol_table (symbol_table *p_symtab)
    {
    free(*p_symtab);
    *p_symtab = NULL;
    }
    void set_symbol(symbol_table *p_symtab, char sym, int value)
    {

    /* WRITE CODE FOR THIS */

    }
    int get_symbol (symbol_table symtab, char sym)
    {

    /* WRITE CODE FOR THIS FUNCTION */


    symbol_table_rep symtab;
    if (A,B,C)
    {
            return symbol_table;
    }else{
            return 0;
    }
    }

    symbol_table.h Code:
    #ifndef _SYMBOL_TABLE_H
    #define _SYMBOL_TABLE_H

    #include "globals.h"

    ABSTRACT_TYPE(symbol_table);

    status init_symbol_table   (symbol_table *p_symtab);
    void   destroy_symbol_table(symbol_table *p_symtab);

    void   set_symbol          (symbol_table *p_symtab, char sym, int value);
    int    get_symbol          (symbol_table  symtab,    char sym);

    #endif
Was it helpful?

Solution

There are a number of other problems with your code, not least that you are passing the entire symbol table by value into get_symbol. How much of the code you're showing did you write, and how much is boiler-plate code you're supposed to leave as-is?

Here is an implementation of a symbol table where the symbols are single character and the values are just ints, as with your code. But this supports more than 3 symbols.

// symbol_table.h
struct symbol_table;
struct symbol_table* init_symbol_table(void);
void   destroy_symbol_table(struct symbol_table *p_symtab);

void   set_symbol          (symbol_table *p_symtab, char sym, int value);
int    get_symbol          (const symbol_table  *symtab,    char sym);

// symbol_table.c
#include <limits.h>
#include <stdlib.h>
#define ARRAYSIZE(a) (sizeof(a)/sizeof((a)[0]))
struct symbol_table
{
  // On rare systems, UCHAR_MAX == SIZE_MAX, and this array size will not work.
  // Most people will never write code for such a system though.  We'll ignore them. 
  int values[UCHAR_MAX+1];
};

struct symbol_table* init_symbol_table (void)
{
  struct symbol_table *p = malloc(sizeof(struct symbol_table));
  if (p) 
  { 
    size_t i;
    for (i=0; i<ARRAYSIZE(p->values); ++i)
      p->values[i] = 0;
  }
  return p;
}


void destroy_symbol_table(struct symbol_table *p)
{
  free(p);
}

void   set_symbol (symbol_table *p, char sym, int value)
{
  p->values[(unsigned char)sym] = value;
}

int get_symbol (const symbol_table  *p, char sym)
{
  return p->values[(unsigned char)sym];
}

If you need to keep the function interface identical (complete with the rather bizarre definition of symbol_table) then you can just implement get_symbol and set_symbol with some simple conditional statements: either a sequence of if statements or a switch statement.

If you're having difficulty with that then go re-read the parts of your course materials which deal with character types and if. If your course materials don't cover that, then you should find some other resources for learning the C language; try starting with the items mentioned at Great C tutorial?

Yes, I could write the get_symbol and set_symbol code for you but I believe the help you're looking for is more around figuring out how to get started with the problem rather than a getting a finished result without understanding.

The key thing I believe you need to achieve is to get a detailed understanding of what specific actions the computer needs to take to return the value of one of the symbols. Start by stating that, as precisely as you can, in any notation at all (a diagram, or in English, whatever). Then try to implement that understanding in the C language.

This process of first understanding the mechanism by which you will solve the problem - that is, what specifically you want to make the computer do - is totally central to the process of learning to program. That learning experience is the thing this kind of homework is intended to provide, I guess. But nobody can do it for you. Showing you completed code might not help, because it won't push into your head the "Aha!" insight.

If you're really, totally, stuck, start with this implementation:

void set_symbol(symbol_table *p_symtab, char sym, int value)
{
  /* WRITE CODE FOR THIS LATER */
}

int get_symbol (symbol_table symtab, char sym)
{
  return 0;
}

It clearly does the wrong thing, but it will compile. Then work on it by modifying it to return a fixed value for A, for B, and for C. Then refer to your learning materials about how to access the members of a structure. Change the code to always return the value of the a member. Then try to figure out how to distinguish between the cases where the caller wants to fetch the value of A or B or C. You might find it helpful to 'artifically' set the members a b and c of the symbol table to some characteristic value to make sure you're returning the correct one (but remember to remove that code later).

Once you have done that, start working on implementing set_symbol. By the time you have made get_symbol work, set_symbol should be easy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top