home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_06 / 1n06037a < prev    next >
Text File  |  1990-10-02  |  1KB  |  88 lines

  1.  
  2. Listing 1
  3.  
  4. unit scan;
  5.  
  6. interface
  7.  
  8. type
  9.     token_code =
  10.         (
  11.         TC_INT, TC_ADD, TC_SUB, TC_MUL, TC_DIV,
  12.         TC_EOL, TC_BAD, TC_EOF
  13.         );
  14.  
  15. const
  16.     token_code_image : array [token_code] of string[7] =
  17.         (
  18.         'TC_INT', 'TC_ADD', 'TC_SUB', 'TC_MUL', 'TC_DIV',
  19.         'TC_EOL', 'TC_BAD', 'TC_EOF'
  20.         );
  21.  
  22. var
  23.     int_value : integer;
  24.  
  25. function get_token : token_code;
  26. procedure restart_scan;
  27. procedure start_scan;
  28.  
  29. implementation
  30.  
  31. const
  32.     TAB = chr(9);
  33.     LF = chr(10);
  34.     CR = chr(13);
  35.     EF = chr(26);
  36.     MINCHAR = chr(0);
  37.     MAXCHAR = chr(255);
  38.  
  39. var
  40.     c : char;
  41.     xtable : array [char] of token_code;
  42.  
  43. function get_token : token_code;
  44.     begin
  45.     while c in [' ', TAB, LF] do
  46.         read(c);
  47.     if c in ['0' .. '9'] then
  48.         begin
  49.         get_token := TC_INT;
  50.         int_value := 0;
  51.         repeat
  52.             int_value := 10 * int_value + ord(c) - ord('0');
  53.             read(c);
  54.         until not (c in ['0' .. '9']);
  55.         end
  56.     else
  57.         begin
  58.         get_token := xtable[c];
  59.         read(c);
  60.         end;
  61.     end;
  62.  
  63. procedure restart_scan;
  64.     begin
  65.     while c <> LF do
  66.         read(c);
  67.     read(c);
  68.     end;
  69.  
  70. procedure start_scan;
  71.     var
  72.         cx : char;
  73.     begin
  74.     for cx := MINCHAR to MAXCHAR do
  75.         xtable[cx] := TC_BAD;
  76.     xtable['+'] := TC_ADD;
  77.     xtable['-'] := TC_SUB;
  78.     xtable['*'] := TC_MUL;
  79.     xtable['/'] := TC_DIV;
  80.     xtable[CR] := TC_EOL;
  81.     xtable[EF] := TC_EOF;
  82.     read(c);
  83.     end;
  84.  
  85. end.
  86.  
  87.  
  88.