home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c329 / 2.img / EXAMPLES / FINDSYM.P < prev    next >
Encoding:
Text File  |  1989-08-22  |  2.3 KB  |  81 lines

  1. program findsym (input,output);
  2.  
  3. { Purpose: find symbol in symbol table, if present }
  4. { and install it if not }
  5.  
  6. { Separate NDP C functions needed: }
  7. {       lookup() in module LOOKUP.C }
  8. {       install() in module LOOKUP.C }
  9.  
  10. { The symbol table is an array of pointers to buckets }
  11. { Each bucket is a linked list of zero or more cells }
  12. { Each cell is a record of type SYMBOL }
  13. { Each SYMBOL has a name by which it is known }
  14. { The index into the array for each bucket is the }
  15. { hashed value of the name associated with the SYMBOL }
  16. { Therefore each bucket is a linked list of zero or more }
  17. { cells all having names that hash to the same value }
  18.  
  19. const
  20.    STSIZE = 200;        { size of symbol table }
  21.    NAMESIZE = 30;       { size of symbol name }
  22.  
  23. type
  24.    NAMETYPE = packed array [1..NAMESIZE] of char;
  25.    SYMBOL = record
  26.                name     : NAMETYPE;
  27.                symtype  : integer;
  28.                nextsym  : ^SYMBOL;
  29.             end;
  30.    SYMBOLTABLE = array [1..STSIZE] of ^SYMBOL;
  31.  
  32. var
  33.    symtable : SYMBOLTABLE;
  34.    identifier : NAMETYPE;
  35.    psymbol : ^SYMBOL;
  36.    i :integer;
  37.  
  38. function lookup(var identifier : NAMETYPE): ^SYMBOL; external;
  39. function install(var identifier : NAMETYPE): ^SYMBOL; external;
  40.  
  41. procedure stinit;
  42.     { Utility routine to initialize symbol table entries to NIL }
  43.     var i:integer;
  44.     begin for i:= 1 to STSIZE do symtable[i]:= NIL end;
  45.  
  46. function getid (var identifier : NAMETYPE): integer;
  47.     { Utility routine to take input from keyboard }
  48.     var i:integer;
  49.     begin
  50.        write ('Enter string ');
  51.        i := 1;
  52.        repeat
  53.           read (identifier[i]);
  54.           i := i + 1;
  55.        until eoln;
  56.        readln;   { flush carriage return }
  57.        identifier[i] := chr(0); 
  58.        writeln;
  59.        getid := i-1 {return number of chars read minus carriage return }
  60. end  {getid};
  61.  
  62. begin
  63.   writeln;
  64.   writeln ('Initializing symbol table entries to NIL');
  65.   writeln;
  66.   stinit;
  67.  
  68.   while ((i := getid(identifier)) > 1) do begin
  69.      writeln ('identifier is ',identifier:i);
  70.      psymbol := lookup (identifier);
  71.      if psymbol = NIL then
  72.         begin
  73.            writeln ('No entry found. Installing symbol.');
  74.            psymbol := install (identifier)
  75.         end
  76.      else
  77.            writeln ('Entry was found.');
  78.      writeln;
  79.   end   { while }
  80. end.
  81.