⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cstrcpsr.c

📁 NASA 开发使用的一个专家系统
💻 C
📖 第 1 页 / 共 2 页
字号:
   /*******************************************************/   /*      "C" Language Integrated Production System      */   /*                                                     */   /*             CLIPS Version 6.05  04/09/97            */   /*                                                     */   /*              CONSTRUCT PARSER MODULE                */   /*******************************************************//*************************************************************//* Purpose: Parsing routines and utilities for parsing       *//*   constructs.                                             *//*                                                           *//* Principal Programmer(s):                                  *//*      Gary D. Riley                                        *//*                                                           *//* Contributing Programmer(s):                               *//*                                                           *//* Revision History:                                         *//*                                                           *//*************************************************************/#define _CSTRCPSR_SOURCE_#include "setup.h"#if (! RUN_TIME) && (! BLOAD_ONLY)#include <stdio.h>#define _CLIPS_STDIO_#include "router.h"#include "watch.h"#include "constrct.h" #include "prcdrpsr.h"#include "exprnpsr.h"#include "modulutl.h"#include "modulpsr.h"#include "utility.h"#include "cstrcpsr.h"/***************************************//* LOCAL INTERNAL FUNCTION DEFINITIONS *//***************************************/#if ANSI_COMPILER   static int                     FindConstructBeginning(char *,struct token *,int,int *);#else   static int                     FindConstructBeginning();#endif/********************************************************//* Load: C access routine for the load command. Returns *//*   0 if the file couldn't be opened, -1 if the file   *//*   was opened but an error occurred while loading     *//*   constructs, and 1 if the file was opened and no    *//*   errors occured while loading.                      *//********************************************************/globle int Load(fileName)  char *fileName;  {   FILE *theFile;   int noErrorsDetected;   /*=======================================*/   /* Open the file specified by file name. */   /*=======================================*/   if ((theFile = fopen(fileName,"r")) == NULL) return(0);        /*===================================================*/   /* Read in the constructs. Enabling fast load allows */   /* the router system to be bypassed for quicker load */   /* times.                                            */   /*===================================================*/      SetFastLoad(theFile);   noErrorsDetected = LoadConstructsFromLogicalName((char *) theFile);   SetFastLoad(NULL);   /*=================*/   /* Close the file. */   /*=================*/   fclose(theFile);      /*========================================*/   /* If no errors occurred during the load, */   /* return 1, otherwise return -1.         */   /*========================================*/      if (noErrorsDetected) return(1);      return(-1);  }/******************************************************************//* LoadConstructsFromLogicalName: Loads a set of constructs into  *//*   the current CLIPS environment from a specified logical name. *//******************************************************************/globle int LoadConstructsFromLogicalName(readSource)  char *readSource;  {   int constructFlag;   struct token theToken;   int noErrors = CLIPS_TRUE;   int foundConstruct;    /*=========================================*/   /* Reset the halt execution and evaluation */   /* error flags in preparation for parsing. */   /*=========================================*/      if (CurrentEvaluationDepth == 0) SetHaltExecution(CLIPS_FALSE);   SetEvaluationError(CLIPS_FALSE);      /*========================================================*/   /* Find the beginning of the first construct in the file. */   /*========================================================*/      GetToken(readSource,&theToken);   foundConstruct = FindConstructBeginning(readSource,&theToken,CLIPS_FALSE,&noErrors);   /*==================================================*/   /* Parse the file until the end of file is reached. */   /*==================================================*/   while ((foundConstruct == CLIPS_TRUE) && (GetHaltExecution() == CLIPS_FALSE))     {      /*===========================================================*/      /* Clear the pretty print buffer in preparation for parsing. */      /*===========================================================*/            FlushPPBuffer();            /*======================*/      /* Parse the construct. */      /*======================*/            constructFlag = ParseConstruct(ValueToString(theToken.value),readSource);            /*==============================================================*/      /* If an error occurred while parsing, then find the beginning  */      /* of the next construct (but don't generate any more error     */      /* messages--in effect, skip everything until another construct */      /* is found).                                                   */      /*==============================================================*/            if (constructFlag == 1)        {         PrintCLIPS(WERROR,"\nERROR:\n");         PrintInChunks(WERROR,GetPPBuffer());         PrintCLIPS(WERROR,"\n");         noErrors = CLIPS_FALSE;         GetToken(readSource,&theToken);         foundConstruct = FindConstructBeginning(readSource,&theToken,CLIPS_TRUE,&noErrors);         }      /*======================================================*/      /* Otherwise, find the beginning of the next construct. */      /*======================================================*/            else        {          GetToken(readSource,&theToken);         foundConstruct = FindConstructBeginning(readSource,&theToken,CLIPS_FALSE,&noErrors);        }                    /*=====================================================*/      /* Yield time if necessary to foreground applications. */      /*=====================================================*/       if (foundConstruct)         { IncrementSymbolCount(theToken.value); }                             CurrentEvaluationDepth--;       PeriodicCleanup(FALSE,TRUE);       YieldTime();       CurrentEvaluationDepth++;       if (foundConstruct)         { DecrementSymbolCount(theToken.value); }     }   /*========================================================*/   /* Print a carriage return if a single character is being */   /* printed to indicate constructs are being processed.    */   /*========================================================*/#if DEBUGGING_FUNCTIONS   if ((GetWatchItem("compilations") != CLIPS_TRUE) && GetPrintWhileLoading())#else   if (GetPrintWhileLoading())#endif     { PrintCLIPS(WDIALOG,"\n"); }        /*=============================================================*/   /* Once the load is complete, destroy the pretty print buffer. */   /* This frees up any memory that was used to create the pretty */   /* print forms for constructs during parsing. Thus calls to    */   /* the mem-used function will accurately reflect the amount of */   /* memory being used after a load command.                     */   /*=============================================================*/                     DestroyPPBuffer();      /*==========================================================*/   /* Return a boolean flag which indicates whether any errors */   /* were encountered while loading the constructs.           */   /*==========================================================*/      return(noErrors);  }/********************************************************************//* FindConstructBeginning: Searches for a left parenthesis followed *//*   by the name of a valid construct. Used by the load command to  *//*   find the next construct to be parsed. Returns TRUE is the      *//*   beginning of a construct was found, otherwise FALSE.           *//********************************************************************/static int FindConstructBeginning(readSource,theToken,errorCorrection,noErrors)  char *readSource;  struct token *theToken;  int errorCorrection;  int *noErrors;  {   int leftParenthesisFound = CLIPS_FALSE;   int firstAttempt = CLIPS_TRUE;      /*===================================================*/   /* Process tokens until the beginning of a construct */   /* is found or there are no more tokens.             */   /*===================================================*/      while (theToken->type != STOP)     {      /*=====================================================*/      /* Constructs begin with a left parenthesis. Make note */      /* that the opening parenthesis has been found.        */      /*=====================================================*/            if (theToken->type == LPAREN)         { leftParenthesisFound = CLIPS_TRUE; }              /*=================================================================*/      /* The name of the construct follows the opening left parenthesis. */      /* If it is the name of a valid construct, then return TRUE.       */      /* Otherwise, reset the flags to look for the beginning of a       */      /* construct. If error correction is being performed (i.e. the     */      /* last construct parsed had an error in it), then don't bother to */      /* print an error message, otherwise, print an error message.      */      /*=================================================================*/            else if ((theToken->type == SYMBOL) && (leftParenthesisFound == CLIPS_TRUE))        {         /*===========================================================*/         /* Is this a valid construct name (e.g., defrule, deffacts). */         /*===========================================================*/                  if (FindConstruct(ValueToString(theToken->value)) != NULL) return(CLIPS_TRUE);                  /*===============================================*/         /* The construct name is invalid. Print an error */         /* message if one hasn't already been printed.   */         /*===============================================*/                  if (firstAttempt && (! errorCorrection))           {            errorCorrection = CLIPS_TRUE;            *noErrors = CLIPS_FALSE;            PrintErrorID("CSTRCPSR",1,CLIPS_TRUE);            PrintCLIPS(WERROR,"Expected the beginning of a construct.\n");           }                    /*======================================================*/         /* Indicate that an error has been found and that we're */         /* looking for a left parenthesis again.                */         /*======================================================*/                  firstAttempt = CLIPS_FALSE;         leftParenthesisFound = CLIPS_FALSE;        }              /*====================================================================*/      /* Any token encountered other than a left parenthesis or a construct */      /* name following a left parenthesis is illegal. Again, if error      */      /* correction is in progress, no error message is printed, otherwise, */      /*  an error message is printed.                                      */      /*====================================================================*/            else        {          if (firstAttempt && (! errorCorrection))           {            errorCorrection = CLIPS_TRUE;            *noErrors = CLIPS_FALSE;            PrintErrorID("CSTRCPSR",1,CLIPS_TRUE);            PrintCLIPS(WERROR,"Expected the beginning of a construct.\n");           }                    firstAttempt = CLIPS_FALSE;         leftParenthesisFound = CLIPS_FALSE;        }              /*============================================*/      /* Move on to the next token to be processed. */      /*============================================*/            GetToken(readSource,theToken);     }        /*===================================================================*/   /* Couldn't find the beginning of a construct, so FALSE is returned. */   /*===================================================================*/        return(CLIPS_FALSE);  }/***********************************************************//* ParseConstruct: Parses a construct. Returns an integer. *//*   -1 if the construct name has no parsing function, 0   *//*   if the construct was parsed successfully, and 1 if    *//*   the construct was parsed unsuccessfully.              *//***********************************************************/globle int ParseConstruct(name,logicalName)  char *name, *logicalName;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -