compiler.c

来自「mgcp协议源代码和测试程序,还有一个编译器」· C语言 代码 · 共 2,132 行 · 第 1/5 页

C
2,132
字号
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL SortRule(TRuleList *pRuleList, TSList *pArray)
{
  int i = 0;
  int iCount = pRuleList->iCount;
  TRule *pRule = NULL;
  int iRuleNumInTmpArray = 0;
  BOOL ret = TRUE;

  /*Temp rule pointer list */
  TSList TmpArray;
  SListInit(&TmpArray);

  /* Save Seq/Choice rule pointer into temp array with reverse order
   * and save TokenRule pointer into output rule array */
  for (i = iCount-1; i >= 0; i--)
  {
    pRule = pRuleList->pRule + i;

    /* Store Seq/Choice rule into temp rule array */
    if (pRule->iType == RULE_SEQUENCE || pRule->iType == RULE_CHOICE)
    {
      SListAppend(&TmpArray, pRule);
      continue;
    }

    /* Directly store TokenRule into output rule array */
    if (pRule->iType == RULE_STRING || pRule->iType == RULE_STRING_CHOICE)
    {
      SListAppend(pArray, pRule);
    }
  }

  iRuleNumInTmpArray = TmpArray.count;
  while (iRuleNumInTmpArray-- > 0)
  {
    SListReset(&TmpArray);

    /* Every iteration one rule should be sorted */
    while ((pRule = (TRule*)SListGetCurData(&TmpArray)) != NULL)
    {
      /* If pRule's link Rule already in pArray */
      if (FindRulePntInList(pRule,pArray) == TRUE)
      {
        /* Store pRule into pArray */
        SListAppend(pArray, pRule);
        
        /* Remove pRule from temp array */
        SListDelCurNode(&TmpArray);
      }
      else
        SListNextNode(&TmpArray);
    } 
  }
  
  /* If temp array still have rule pointer */
  if (TmpArray.count > 0)
  {
    printf("Error! Fail to Sort Rule, %d left:\n", TmpArray.count);

    /* Print the left rules */
    SListReset(&TmpArray);
    do
    {
      printf("  %s\n", ((TRule*)TmpArray.cur->data)->pRuleName);
    } while (SListNextNode(&TmpArray));

    ret = FALSE;
  }

  /* Free the temp list */
  SListDel(&TmpArray);
  
  return ret;
}
/***************************************************************************
 * Function          : LinkRule
 *
 * Description       : Find the associate rules of sepcific rule in rulelist, 
 *                     the specific rule must include elements as bellow:
 *                      ELEMENT_RULE
 *                      ELEMENT_TOKEN
 *                      ELEMENT_GRP_RULE
 *                      ELEMENT_OPT_RULE
 *                      ELEMENT_OPT_LST
 *                      ELEMENT_RULE_LST_GRP
 *                     
 * Input parameters  : pRuleList - rule list 
 *
 * Output parameters : pRuleList - rules are associated
 *
 * Return value      : TRUE if rules find their associate rules, otherwise 
 *                     return FALSE.
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL LinkRule(TRuleList *pRuleList)
{
  int i,j,k;
  TRule *pRule;
  TConcatenation *pConcat;
  TRepetition *pRep;

  for (i = 0; i < pRuleList->iCount; i++)
  {
    pRule = pRuleList->pRule + i;/* Rule */
    for (j = 0; j < pRule->iCount; j++)
    {
      pConcat = ((TConcatenation*)pRule->pConcat) + j;/* Concatenation */      
      for (k = 0; k < pConcat->iCount; k++)
      {
     	  pRep = pConcat->pRepetition + k;/* Repetition */        
     	  switch (pRep->iType)
     	  {
     	    case ELEMENT_RULE:
            if ((pRep->pRuleLink = FindRule(pRuleList, pRep->pName)) == NULL)
        	  {
              printf("\nError! Cann't find associate Rule of: %s!",pRep->pName);
              return FALSE;
            }
          break;

          case ELEMENT_TOKEN:
            if ((pRep->pRuleLink = FindRule(pRuleList, pRep->pName)) == NULL)
        	  {
              printf("\nError! Cann't find associate Rule  of: %s!",pRep->pName);
              return FALSE;
            }

            /* Token element must associate string type rule */
            if (pRep->pRuleLink->iType != RULE_STRING 
              && pRep->pRuleLink->iType != RULE_STRING_CHOICE)
            {
              printf("\nError! The element type in Token element: %s must be String!",
                     pRep->pName);
              return FALSE;
            }
          break;

          case ELEMENT_GRP_RULE:
          case ELEMENT_OPT_RULE:
          case ELEMENT_OPT_LST:   
          case ELEMENT_RULE_LST_GRP:
            if ((pRep->pRuleLink = FindRule(pRuleList, pRep->pName)) == NULL)
            {
              printf("\nError! Cann't find associate Rule of :%s!",pRep->pName);
              return FALSE;
            }

            if ((pRep->pGrpRuleLink = FindRule(pRuleList, pRep->pRuleName)) == NULL)
            {
              printf("\nError! Cann't find associate Rule of: %s!",pRep->pRuleName);
              return FALSE;
            }
          break;
        }
      }
    }
  }
  
  return TRUE;
}
/***************************************************************************
 * Function          : FindRule
 *
 * Description       : Search the rule list to find the rule according its 
 *                     name,if found Rule is OneElem type, continue to find
 *                     the right rule using the OneElem rule name.
 *                                      
 * Input parameters  : pRuleList - pointer of rule list 
 *                     RuleName - name of the rule to be searched
 *
 * Output parameters : None
 *
 * Return value      : If rule in rule list, return its pointer;
 *                     otherewise return NULL
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
TRule* FindRule(TRuleList *pRuleList, char *RuleName)
{
  int i;
  TRule *pRule;
  
  if (RuleName == NULL || pRuleList == NULL)
    return NULL;

  for (i = 0; i < pRuleList->iCount; i++)
  {
    pRule = pRuleList->pRule + i;
    if (StrCaseCmp(pRule->pRuleName,RuleName) == 0)
    {
      if (pRule->iType == RULE_ONE_ELEM)
      {
        /* If find rule is OneElem type, continue find target rule using the name*/
        RuleName = ((TConcatenation*)pRule->pConcat)->pRepetition->pName;
        i = 0;
        continue;
      }
	    return pRule;
    }
  }

  return NULL;
}
/***************************************************************************
 * Function          : FindRulePntInList
 *
 * Description       : Check if pointer of rule has been in the pointer list
 *                     
 * Input parameters  : pRule - pointer of the rule to be checked
 *                     pArray - rule pointer list
 *
 * Output parameters : None
 *
 * Return value      : TRUE if rule pointer has been in list;
 *                     otherwise return FALSE.
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
  *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL FindRulePntInList(TRule *pRule, TSList *pArray)
{
  int iNum = 0;
  TConcatenation *pConcat = NULL;
  TRepetition *TmpRep = NULL;
  
  if (pRule != NULL && pArray != NULL)
  {
    /* If rule is string type, no need to check */
    if (pRule->iType == RULE_STRING
        || pRule->iType == RULE_STRING_CHOICE
        || pRule->iType == RULE_ONE_ELEM)
      return TRUE;
   
    /* Sequence type, check every repetition's link rule */
    if (pRule->iType == RULE_SEQUENCE)
    {
      iNum = ((TConcatenation*)pRule->pConcat)->iCount;/* repet num */
      TmpRep = ((TConcatenation*)pRule->pConcat)->pRepetition; /* repet in sequence rule */
  
      while (iNum-- > 0)
      {
        /* Check if link rule of every Rule type repet has been in pArray */         
        if (TmpRep->iType == ELEMENT_RULE
            && (SListFindByPointer(pArray, TmpRep->pRuleLink) == NULL))
         return FALSE;
  
        TmpRep++;
      }
      return TRUE;
    }
    
    /* Choice type, check every repetition's link rule */
    if (pRule->iType == RULE_CHOICE)
    {
      iNum = pRule->iCount;/* Concat num */
      pConcat = (TConcatenation*)pRule->pConcat;
  
      while (iNum-- > 0)
      {
         TmpRep = pConcat->pRepetition;       
         if ((TmpRep->iType == ELEMENT_RULE || TmpRep->iType == ELEMENT_TOKEN)
             && (SListFindByPointer(pArray, TmpRep->pRuleLink) == NULL))
          return FALSE;
         
         pConcat++;
      }
      return TRUE;
    }  
  }
  
  return FALSE;
}




/***************************************************************************
 * Function          : CheckDupRuleName
 *
 * Description       : Check if dup rule name in rule list
 *                     
 * Input parameters  : pRuleList - pointer of rule list to be checked
 *                     
 * Output parameters : None
 *
 * Return value      : If no dup rule name, return NULL; otherwise
 *                     return the pointer of the dup rule
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
TRule *CheckDupRuleName(TRuleList *pRuleList)
{
  int i,j;
  TRule *pCurRule, *pNextRule;

  for (i = 0; i < pRuleList->iCount-1; i++)
  {
    pCurRule = pRuleList->pRule + i;
    for (j = i+1; j < pRuleList->iCount; j++)
    {
      pNextRule = pRuleList->pRule + j;
      if (StrCaseCmp(pCurRule->pRuleName, pNextRule->pRuleName) == 0)
        return pCurRule;      
    }
  }
  
  return NULL;
}

/***************************************************************************
 * Function          : ConvertName
 *
 * Description       : Check every char in src string, if find special char,
 *                     convert it to be a string or its hex value string,
 *                     store the converted string into des string.
 *                     
 * Input parameters  : des - pointer to string to store converted string
 *                     src - pointer to original string
 *
 * Output parameters : des - Converted string
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void ConvertName(char *des, char *src)
{
  unsigned int i;
  char *pnt;

  Assert(des);
  Assert(src);

  /* The des string must be initialize to be zero filled */
  Assert(strlen(des) == 0);
  
  for (i = 0; i < strlen(src); i++)
  {
    /* After every iteration,the des string content mabe modified
     * so re-seek the end point position */
    pnt = des + strlen(des);

    if (isdigit(*(src+i)) || isalpha(*(src+i)) || *(src+i) == '_')
    {
      *pnt = *(src + i);
      continue;
    }
    else
    {
      switch (*(src+i))
      {
        case '.':
          strcpy(pnt, "DOT");
          break;
          
        case '*':
          strcpy(pnt, "STAR");
          break;
          
        case '#':
          strcpy(pnt, "POUND");
          break;
          
        case '\\':
          strcpy(pnt, "SLASH");
          break;

        case '+':
          strcpy(pnt, "PLUS");
          break;

        case '-':
          strcpy(pnt, "MINUS");
          break;

        case '$':
          strcpy(pnt, "DALLAR");
          break;

        case '@':
          strcpy(pnt, "AT");
          break;

        case '=':
          strcpy(pnt, "EQUAL");
          break;

        case '%':
            strcpy(pnt, "PERCENT");
            pnt += strlen("PERCENT");
            

⌨️ 快捷键说明

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