0

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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Cka91405
  • 165
  • 1
  • 8
  • 17
  • 1
    I understood from your question what you are trying to achieve, but I failed to understand what *exactly* are you struggling with. Which part exactly are you asking help with? Pointing that out explicitly will probably help you get better answers. – amit Apr 14 '12 at 23:11
  • @amit im trying to write out the two functions and dont know where to start. – Cka91405 Apr 14 '12 at 23:13
  • What's the purpose of the statement `if (A,B,C)`? While legal, you haven't declared any of the variable, and only `C` will actually be used as the boolean expression. – Some programmer dude Apr 14 '12 at 23:23
  • it was just the kind of logic i was getting in my head. im honestly not too sure how to begin. – Cka91405 Apr 14 '12 at 23:39

1 Answers1

0

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.

Community
  • 1
  • 1
James Youngman
  • 3,623
  • 2
  • 19
  • 21
  • Im trying to keep init_symbol_table & destroy_symbol_table the way it is written and the only struct i was given was struct symbol_table_s{ int a,b,c;}*symbol_table; – Cka91405 Apr 15 '12 at 00:37
  • Thank you so much ! This is exactly what i was trying to get at i dont want the correct answer ! Thank you ! – Cka91405 Apr 15 '12 at 15:07
  • I understand the statement you made about the if statement or using a switch case but the only problem im having is understanding the return value or where the return value comes from and how it is taken from the symbol table struct. – Cka91405 Apr 15 '12 at 15:11
  • That boils down to "how do I access a member of a struct" which should be answered in the part of your course materials that introduces the `struct` keyword. This is rather a basic question so there are no questions on Stackoverflow about it. You could read http://cprogramminglanguage.net/c-structure.aspx but that doesn't deal with pointers to structures. – James Youngman Apr 16 '12 at 14:02
  • I figured out how to do it but i dont know if im writing the code correctly i dont think i have to make a new struct do i ? if i could have a small hint please lol. i have figured out the english of the process of get_symbol(...) which is if sym == 'a' then return the value of a from the symbol_table symtab. but im confused as to if i need a new struct that has symtab. – Cka91405 Apr 16 '12 at 16:22
  • Read yoiur course materials - the part where it describes how to access members of structures. Or see http://www2.its.strath.ac.uk/courses/c/subsection3_12_2.html – James Youngman Apr 18 '12 at 08:41
  • Thanks I got my code to compile and work Im just getting a seg fault now within one of my other codes. :( – Cka91405 Apr 18 '12 at 15:36