summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/OR_PRACTICAL_C/symbol.c
blob: 600bc4c146f50be726bd7c0d83fc919fb1f1d304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/********************************************************
 *  symbol -- handle the symbol table                   *
 *                                                      *
 * Functions                                            *
 *      enter  -- put a symbol in a symbol table        *
 *      lookup -- get the data associated with a symbol *
 ********************************************************/
#include <stdio.h>
#include "symbol.h"
#include <string.h>
#include <stdlib.h>

/********************************************************
 * enter -- enter a word into the symbol table          *
 *                                                      *
 * Parameters                                           *
 *      node -- top node of the symbol table for add    *
 *      symbol -- symbol name to add (1 or 2 chars)     *
 *      data -- data associated with the symbol         *
 ********************************************************/
void enter(struct symbol **node_ptr, char *symbol, generic *data)
{
    int  result;        /* result of strcmp */
    /* New node that we are creating */
    struct symbol *new_node_ptr;

    /* see if we have reached the end */
    if ((*node_ptr) == NULL) {
        new_node_ptr = (struct symbol *) malloc(sizeof(struct symbol));
        (void)strcpy(new_node_ptr->name, symbol);
        new_node_ptr->data = data;
        new_node_ptr->left_ptr = NULL;
        new_node_ptr->right_ptr = NULL;
        *node_ptr = new_node_ptr;
        return;
    }
    /*
     * Need to sub-divide the symbol table and try again
     */
    result = strcmp((*node_ptr)->name, symbol);

    if (result == 0)
        return;

    if (result > 0)
        enter(&(*node_ptr)->left_ptr, symbol, data);
    else
        enter(&(*node_ptr)->right_ptr, symbol, data);
}
/********************************************************
 * lookup -- lookup a symbol in a table                 *
 *                                                      *
 * Parameters                                           *
 *      root -- root of the symbol table to search      *
 *      name -- name to lookup.                         *
 *                                                      *
 * Returns                                              *
 *      Pointer to the data or NULL if not found.       *
 ********************************************************/
generic *lookup(struct symbol *root_ptr, char *name)
{
    int result; /* Result of string compare */

    if (root_ptr == NULL) 
        return (NULL);

    result = strcmp(root_ptr->name, name);

    if (result == 0) {
        return (root_ptr->data);
    }

    if (result > 0)
        return (lookup(root_ptr->left_ptr, name));
    else 
        return (lookup(root_ptr->right_ptr, name));
}