home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / CONTRIB / HUU / PREMATCH.SA next >
Text File  |  1994-10-25  |  6KB  |  211 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- File: prestrmatch.sa
  9. -- Author: Le van Huu (huu@ICSI.Berkeley.EDU, levan@imiucca.csi.unimi.it))
  10. --*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. --* FUNCTION: A simple example program for testing the class STRMATCH
  12. --* relating to the main string matching algorithms. It prepares the text
  13. --*    and the pattern string to be compared. The text string is read
  14. --* from a file object while the pattern string is read from the stdin.
  15. --* The file name of the text string is got from stdin.
  16. --*
  17. --*    The algorithms implemented are the naive string matcher algorithm
  18. --*    and the Knuth-Morris-Pratt algorithm. The user can selects the 
  19. --*    algorithm to be performed using the stdin.
  20. --* The algorithms come from 
  21. --*    Thomas H. Cormen, Charles E., Introduction to Algorithms, MIT Press,1990,
  22. --* pp. 853-885.
  23. --*
  24. --* CLASSES: PATTERN,  TEXT,  FILEOPER, TEMPL_ALGTHM,  PRESTRMATCH
  25. --* 
  26. --* RELATED FILES: strmatch.sa
  27. --*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28.  
  29. class PATTERN is
  30.     -- Pattern string class. It gets the pattern string from the stdin
  31.  
  32.      attr P:STR;                    -- The pattern string 
  33.     attr lenstr:INT;            -- length of pattern string
  34.    
  35.        create:PATTERN is
  36.         res::= new;
  37.         return res;
  38.     end;
  39.    
  40.     get_own_str is
  41.         -- Gets pattern string and calculates its lenght
  42.         #OUT + "Input pattern string: ";
  43.          lenstr :=0;
  44.         P:= #IN.get_str;
  45.         lenstr:= P.length;    
  46.     end;
  47.  
  48.     print is
  49.         #OUT + "pattern: ";
  50.          i:INT :=0;
  51.         loop until!  (i=lenstr);
  52.             #OUT + P[i];
  53.             i := i +1;
  54.         end;
  55.         #OUT + "\n";
  56.     end;
  57.  
  58. end; -- class PATTERN
  59.  
  60. --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  61.  
  62. class TEXT is
  63.  
  64.     -- Text class. The routine "get_own_str" is redefined to read the text
  65.     -- string from a file whose name is read from the stdin.
  66.  
  67.       attr ftext : FILE;
  68.     attr T: STR;
  69.     attr lenstr : INT;
  70.  
  71.     create:TEXT is
  72.         res ::= new;
  73.         return res;
  74.     end;
  75.  
  76.     opentext : FILE is
  77.         fid_text:FILE;
  78.         fname : STR;
  79.            fobj:FILEOPER:= #FILEOPER; 
  80.  
  81.         #OUT + "text file name : ";
  82.         fname:= #IN.get_str;
  83.         fid_text := fobj.openfile (fname);
  84.         if fid_text.error then
  85.             #OUT + "Can't open file text, so ...";
  86.         end;
  87.         return fid_text;
  88.     end;    
  89.  
  90.     get_own_str is
  91.         c:CHAR;
  92.  
  93.         ftext:= opentext;
  94.         if ftext.error then
  95.             T := "File error";
  96.             lenstr:= 0;    
  97.         else
  98.             lenstr := 0;
  99.             T:=ftext.fstr.str;
  100.             lenstr:= T.length;    
  101.         end;    
  102.     end;
  103. end; -- class TEXT
  104.  
  105. --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  106. class FILEOPER is
  107.     -- Class for common file operations. At present time it contains
  108.     -- only the open operation.
  109.  
  110.     create:FILEOPER is
  111.         res::=new;
  112.         return res;
  113.     end;
  114.     openfile (name:STR) : FILE is 
  115.         fileid:FILE := FILE::open_for_read(name);
  116.         return fileid;
  117.     end;
  118. end; -- class FILEOPER
  119.  
  120. --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121.  
  122. class TEMPL_ALGTHM  is
  123.     -- Template class for dispaching the appropriate algorithm call to
  124.     -- the class STRMATCH.
  125.  
  126.     const init_size:INT:=50;
  127.     const NAIVE:STR := "1";
  128.     const KMP:STR:= "2";
  129.     const ENDARR:INT := -2;
  130.  
  131.     attr arr_shift:ARRAY{INT};                   
  132.  
  133.     create: TEMPL_ALGTHM is
  134.         res ::= new;
  135.         return res;
  136.     end;
  137.  
  138.     dispatch (objT:TEXT, objP:PATTERN, alg : STR) is
  139.         -- Dispatchs the call to the proper algorithm routine contained in
  140.         -- the class STRMATCH 
  141.         -- Each algorithm routine returns an array (arr_shift) containing
  142.         -- valid shift values, i.e., positions of text substrings
  143.         -- that match the pattern
  144.         i, n:INT;
  145.         arr_shift:=#ARRAY{INT}(init_size);
  146.         strmatch_obj:STRMATCH := #STRMATCH;
  147.  
  148.         case alg
  149.         when NAIVE then
  150.             arr_shift := strmatch_obj.naive (objT.T , objP.P);
  151.             print ("Naive ", objP.P);
  152.  
  153.         when KMP then
  154.             arr_shift := strmatch_obj.kmp (objT.T , objP.P);
  155.             print ("KMP ", objP.P);
  156.         else
  157.                 #OUT + "Option not implemented. Use: " + "\n";
  158.                 #OUT + "   1: Naive algorithm" + "\n";
  159.                 #OUT + "   2: KMP algorithm" + "\n";
  160.         end;
  161.     end;
  162.  
  163.     print (alg, pattern :STR) is
  164.         i : INT := 0;
  165.         loop until! (arr_shift [i] = ENDARR);
  166.             #OUT + alg;
  167.             #OUT + "- Strings matched at: " + arr_shift [i] + "\n";
  168.             i:=i+1;
  169.         end;
  170.         #OUT + "Total occurences of \"" + pattern + "\" : " + i + "\n";
  171.     end;    
  172. end; -- class TEMPL_ALGTHM
  173.  
  174. --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175.    
  176. class PRESTRMATCH is
  177.    
  178. -- A simple example program for testing the class STRMATCH
  179. -- relating to the main string matching algorithm. It prepares the text
  180. -- and the pattern string to be compared.
  181.  
  182.        main is
  183.         c:CHAR;
  184.         algorithm: STR := "";
  185.  
  186.         loop until! (algorithm = "1" or algorithm = "2"); 
  187.             #OUT + "Select the algorithm. Use: " + "\n";
  188.             #OUT + "   1: Naive algorithm" + "\n";
  189.             #OUT + "   2: KMP algorithm" + "\n";
  190.             algorithm := #IN.get_str;
  191.             if (algorithm /= "1" and algorithm /= "2") then
  192.                 #OUT + "Option not implemented." + "\n";
  193.             end;
  194.         end;
  195.         
  196.            p:PATTERN := #PATTERN; 
  197.            t:TEXT := #TEXT; 
  198.            pre_algthm:TEMPL_ALGTHM:= #TEMPL_ALGTHM; 
  199.         -- template class for preparing the algorithm processing
  200.  
  201.         p.get_own_str;
  202.         t.get_own_str;
  203.         if t.lenstr = 0 or p.lenstr = 0 then
  204.             #OUT + " can't compare null strings" + "\n";
  205.         else
  206.             pre_algthm.dispatch(t,p, algorithm );
  207.         end;
  208.    end; -- main
  209.    
  210. end; -- class PRESTRMATCH 
  211.