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
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.
If you need to keep the function interface identical (complete with the rather bizarre definition of
symbol_table) then you can just implementget_symbolandset_symbolwith some simple conditional statements: either a sequence ofifstatements or aswitchstatement.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_symbolandset_symbolcode 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:
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
amember. 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 membersabandcof 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 madeget_symbolwork,set_symbolshould be easy.