Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 189 lines (155 sloc) 4.918 kb
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#include <stdio.h>
#include <string.h>

#define MAX 150
#define LENGTH(x) (sizeof(x)/sizeof(*(x)))
#define true 1
#define false 0

struct {
  int dispatch[52];
  char symbol[MAX];
  int next[MAX];
} symbol_table;

void init_symbol_table(void);
int next_symbol(char *);
int find_first_empty(char *, int);
void print_switch(int *, int);
void print_symbol(char *, int);
void print_next(int *, int);
void insert(char *);

int main(int argc, char *argv[]) {

  init_symbol_table();
 
  printf("Inserting bool...\n");
  insert("bool");
  
  printf("Inserting boolean...\n");
  insert("boolean");
  
  printf("Inserting class...\n");
  insert("class");
  
  printf("Inserting extends...\n");
  insert("extends");
 
  printf("Inserting implements...\n");
  insert("implements");
  
  printf("Inserting a...\n");
  insert("a");
  
  printf("Inserting abba...\n");
  insert("abba");
  
  printf("\nSwitch Table\n============\n");
  print_switch(symbol_table.dispatch, LENGTH(symbol_table.dispatch));
   
  printf("\nSymbol Table\n============\n");
  print_symbol(symbol_table.symbol, LENGTH(symbol_table.symbol));
  
  printf("\nNext Table\n==========\n");
  print_next(symbol_table.next, LENGTH(symbol_table.next));
  
  printf("\n");
  return 0;
}

/* Initialize the symbol table */
void init_symbol_table(void) {
  int i;
  for (i = 0; i < 52; i++)
    symbol_table.dispatch[i] = -1;
  for (i = 0; i < MAX; i++)
    symbol_table.symbol[i] = '\0';
  for (i = 0; i < MAX; i++)
    symbol_table.next[i] = -1;
}

/* A B C ... Z a b c ... z
0 1 2 25 26 ...... 51 */
int next_symbol (char *s) {
  int p = s[0];
  if (p >= 97) return p - 97 + 26;
  return p - 65;
}

/* Insert a string to the symbol table
* if the string already exists, do nothing
* iF the string does not exist, create it
* Store same prefixes once */
void insert (char *s) {
  int value = next_symbol(s);
  int ptr = symbol_table.dispatch[ value ];
  
  // pointer is the first prefix string[0] previously stored in the symbol table
  // if pointer is undefined, create.
  if (ptr == -1) {
    
    // find the location of prefix in dispatch table
    int slot = find_first_empty(symbol_table.symbol, LENGTH(symbol_table.symbol));
    symbol_table.dispatch[value] = slot; // update the pointer
    
    // store the rest of characters to symbol table
    int i = 1;
    while (i < strlen(s))
      symbol_table.symbol[slot++] = s[i++];
    symbol_table.symbol[slot] = '@';

  } else {
  // pointer is defined, there are same prefixes. First char is skipped
    int exit = false;

    // keep traversing as long as prefix is same
    int i = 1; // start with second character of input string
    int p = ptr; // start index of same prefix in the symbol table
    while (i < strlen(s)) {
      if (s[i] == symbol_table.symbol[p]) {
        i++;
        p++;
      } else {
        exit = true;
        break;
      }
    }
    
    // The Rest of character start to differ:
    // 1. either reached end marker
    // 2. or the character is different.
    if (exit == true) {
      // use the next table to jump to the right position to store data.
      int next;
      if (symbol_table.next[p] == -1)
        next = find_first_empty(symbol_table.symbol, LENGTH(symbol_table.symbol));
      else
        next = symbol_table.next[p];
          
      // update the next table
      symbol_table.next[p] = next;
      
      while (i < strlen(s))
        symbol_table.symbol[next++] = s[i++];
      symbol_table.symbol[next] = '@';
    }
  
    // If exit stays false until here, the input string is accepted.
    // The exact same string has been stored before in the trie.
  }
}

/* Find the first empty slot in the symbol table */
int find_first_empty(char *array, int size) {
  int i;
  for (i = 0; i < size; i++)
    if (array[i] == '\0')
      return i;
}

/* Print switch array */
void print_switch(int *table, int size) {
  char alphabets[52] = { 'A', 'B', 'C', 'D', 'E', 'F','G','H',
                         'I','J','K','L','M','N','O','P','Q',
                         'R','S','T','U','V','W','X','Y','Z',
                         'a','b','c','d','e','f','g','h',
                         'i','j','k','l','m','n','o','p','q',
                         'r','s','t','u','v','w','x','y','z'};
  int i;
  printf(" ");
  for (i = 0; i < 52; i++)
    printf("%1c ", alphabets[i]);
  printf("\n");
  for (i = 0; i < size; i++)
    printf("%1d ", table[i]);
}

/* Print symbol array */
void print_symbol(char *table, int size) {
  int i;
 
  for (i = 0; i < size; i++)
    printf("%1d ", i);
  printf("\n");
  for (i = 0; i < size; i++)
    printf("%1c ", table[i]);
}

/* Print next array*/
void print_next(int *table, int size) {
  int i;
   printf("Array Indices\n");
  for (i = 0; i < size; i++)
    printf("%1d ", i);
  printf("\nContent:\n");
  for (i = 0; i < size; i++)
    printf("%1d ", table[i]);
}
Something went wrong with that request. Please try again.