home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 241_01 / getkeywo.cr < prev    next >
Encoding:
Text File  |  1987-08-31  |  3.3 KB  |  162 lines

  1. /*
  2. HEADER:         CUG241;
  3. TITLE:          Rule-Based Compiler for Expert System;
  4. DATE:           12/30/85;
  5. VERSION:
  6. DESCRIPTION:   "Source code for rule-based compiler for an Expert System";
  7. KEYWORDS:       Artificial Intelligence, expert systems, rule-based compiler;
  8. SYSTEM:         MS-DOS or UNIX System V;
  9. FILENAME:       ;
  10. WARNINGS:      "User-supported, non-commercial"
  11. AUTHORS:        George Hageman; 
  12. COMPILERS:      Microsoft C V3.00 or UNIX System V Portable C Compiler;
  13. REFERENCES:     ;
  14. ENDREF
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include "expert.h"
  20.  
  21. /******************************************************************
  22. **
  23. **
  24. **        GET KEYWORD
  25. **
  26. **
  27. ******************************************************************/
  28.  
  29. /*
  30. **
  31. **    getKeyWord(infile,keyWords,lineNum) ;
  32. **
  33. **    returns either KEY_EOF, or number of keyword, or indications
  34. **        that keyword was not first word on line.
  35. **
  36. */
  37.  
  38. int    getKeyWord (infile,keyWords,lineNum)
  39.  
  40. FILE    *infile ;
  41. char    *keyWords[] ;
  42. int    *lineNum ;
  43.  
  44. {
  45. int    firstc,c,i,j ;
  46. #ifdef    DEBUG
  47.     fprintf(stdout,"\nDEBUG-GET_KEYWORD") ;
  48. #endif
  49.  
  50. /*
  51. **    get first non blank character
  52. */
  53.  
  54. i = 0 ;
  55. while( TRUE )
  56.     {
  57.     c = getc(infile);
  58.     putchar(c) ;
  59. #ifdef    DEBUG
  60.     fprintf(stdout,"\nDEBUG-GET_KEYWORD character= %c,%x",c,c) ;
  61. #endif
  62.     if( c == EOF )
  63.         return (KEY_EOF) ;
  64.     if( (i == 0) && (c == COMMENT_CHAR) )    /* ignore comment line */
  65.         {
  66.         while(  c != EOL )
  67.             {
  68. #ifdef DEBUG
  69.     fprintf(stdout,"\nDEBUG-GET_KEYWORD (delete comment) character=%c,%x",c,c);
  70. #endif
  71.             c = getc(infile) ;
  72.             if( c == EOF)
  73.                 return (KEY_EOF) ;
  74.             putchar(c) ;
  75.             } 
  76.         printf("%04d  ",*lineNum) ;
  77.         *lineNum += 1 ;
  78.         i = 0 ;
  79.         }
  80.     else
  81.         {
  82.         i = 1 ;
  83.         if( c != BLANK )
  84.             {
  85.             if(c == EOF)
  86.                 return(KEY_EOF) ;
  87.             break ;
  88.             }
  89.         }
  90.     }
  91.  
  92. /*
  93. **    locate first keyword with matching first character
  94. */
  95.  
  96. for( i = 0 ; i < NUM_KEYWORDS ; i++ )
  97.     {
  98. #ifdef    DEBUG
  99.     fprintf(stdout,"\nDEBUG-GET_KEYWORD i=%d, keyword=%c",i,*(keyWords[i])) ;
  100. #endif
  101.     if ( *(keyWords[i]) == c )
  102.         break ;
  103.     }
  104. if( i == NUM_KEYWORDS )
  105.     return (KEY_WORD_ERROR)  ;
  106.  
  107. /*
  108. **    find the key word if there
  109. **
  110. **    Note that this search algrorithm is very dependant on having
  111. **    the keyWord file being in strict alphabetical order!!!
  112. **
  113. */
  114.  
  115. j=0 ;         
  116. firstc = c ;
  117.  
  118. while( i < NUM_KEYWORDS  )
  119.     {
  120.     if( (c=getc(infile)) == *(keyWords[i]+(++j)) )
  121.         {
  122. #ifdef    DEBUG
  123.     fprintf(stdout,"\nDEBUG-GET_KEYWORD char=%c,keyword=%c",c,*((keyWords[i])+j)) ;
  124. #endif
  125.         putchar(c) ;
  126.         if( c == BLANK )
  127.             return (i) ;
  128.         if( c == EOF )
  129.             return(KEY_EOF) ;
  130.         }
  131. /*
  132. **    increment the keyWord pointer to be tested
  133. **    decrement the character pointer in keyWord
  134. **    and put the last character tested back.
  135. */
  136.  
  137. /*
  138. **    since the keyWord array is alphabetical, testing
  139. **    for a first character change would indicate that
  140. **    the possible keywords have been exhausted.
  141. */
  142.  
  143.     else
  144.         {
  145.         i++ ;
  146.         j-- ;
  147.         ungetc(c,infile) ;
  148.         if( c == EOF )
  149.             return (KEY_EOF) ;
  150.         if( firstc != *(keyWords[i]))
  151.             return (KEY_WORD_ERROR) ;
  152.         }
  153.     }
  154.  
  155. /*
  156. **    no keywords were found because we exhausted the keyWord array
  157. */
  158.  
  159. return (KEY_WORD_ERROR)  ;
  160. }
  161.  
  162.