home *** CD-ROM | disk | FTP | other *** search
- program findsym (input,output);
-
- { Purpose: find symbol in symbol table, if present }
- { and install it if not }
-
- { Separate NDP C functions needed: }
- { lookup() in module LOOKUP.C }
- { install() in module LOOKUP.C }
-
- { The symbol table is an array of pointers to buckets }
- { Each bucket is a linked list of zero or more cells }
- { Each cell is a record of type SYMBOL }
- { Each SYMBOL has a name by which it is known }
- { The index into the array for each bucket is the }
- { hashed value of the name associated with the SYMBOL }
- { Therefore each bucket is a linked list of zero or more }
- { cells all having names that hash to the same value }
-
- const
- STSIZE = 200; { size of symbol table }
- NAMESIZE = 30; { size of symbol name }
-
- type
- NAMETYPE = packed array [1..NAMESIZE] of char;
- SYMBOL = record
- name : NAMETYPE;
- symtype : integer;
- nextsym : ^SYMBOL;
- end;
- SYMBOLTABLE = array [1..STSIZE] of ^SYMBOL;
-
- var
- symtable : SYMBOLTABLE;
- identifier : NAMETYPE;
- psymbol : ^SYMBOL;
- i :integer;
-
- function lookup(var identifier : NAMETYPE): ^SYMBOL; external;
- function install(var identifier : NAMETYPE): ^SYMBOL; external;
-
- procedure stinit;
- { Utility routine to initialize symbol table entries to NIL }
- var i:integer;
- begin for i:= 1 to STSIZE do symtable[i]:= NIL end;
-
- function getid (var identifier : NAMETYPE): integer;
- { Utility routine to take input from keyboard }
- var i:integer;
- begin
- write ('Enter string ');
- i := 1;
- repeat
- read (identifier[i]);
- i := i + 1;
- until eoln;
- readln; { flush carriage return }
- identifier[i] := chr(0);
- writeln;
- getid := i-1 {return number of chars read minus carriage return }
- end {getid};
-
- begin
- writeln;
- writeln ('Initializing symbol table entries to NIL');
- writeln;
- stinit;
-
- while ((i := getid(identifier)) > 1) do begin
- writeln ('identifier is ',identifier:i);
- psymbol := lookup (identifier);
- if psymbol = NIL then
- begin
- writeln ('No entry found. Installing symbol.');
- psymbol := install (identifier)
- end
- else
- writeln ('Entry was found.');
- writeln;
- end { while }
- end.
-