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

📄 compiler.c

📁 mgcp协议源代码和测试程序,还有一个编译器
💻 C
📖 第 1 页 / 共 5 页
字号:
          {
            printf("\nError! Duplicate name: %s in rule!", pCurRep->pName);
            return FALSE;
          }
        }
      }
    }
  }
  
  return TRUE;
}
/***************************************************************************
 * Function          : ProcessGroup
 *
 * Description       : Check the validity of Group repetition and decide its
 *                     tpye, if the Group is valid, create a new rule into
 *                     NewRuleList and change its type.
 *                     
 * Input parameters  : pRep - pointer of group repetition
 *                     g_pRuleList - parameter passed to internal function
 *                     g_pNewRuleList - parameter passed to internal function
 *
 * Output parameters : pRep - processed group repet
 *                     pRuleList - processed rule list
 *                     pNewRuleList - new rule list
 *
 * Return value      : TRUE  - group repetition is valid
 *                     FALSE - group repetition is invalid
 *
 * Comments          : The valid format of Group is like: *(Rule *WSP "abcd")
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL ProcessGroup(TRepetition *pRep, TRuleList *g_pRuleList,
                       TRuleList *g_pNewRuleList)
{
  int i;
  TConcatenation *pConcat;
  TRepetition *pRepInGrp;

  TElementCount count;
  memset(&count, 0, sizeof(TElementCount));

  /* Choice type, No support */
  if (pRep->GroupOption.Rule.iCount > 1)
  {  
    printf("\nError! No Choice is allowed in Group!");
	  return FALSE;
  }

  pConcat =  (TConcatenation*)pRep->GroupOption.Rule.pConcat;

  /* Process Token and OneElem Group in every repet */
  for (i = 0; i < pConcat->iCount; i++)
    PreProcessRepet(pConcat->pRepetition + i);

  CountRepNumInConcat(pConcat, &count);

  if (count.iGrpCount+count.iOptCount > 0)
  {
    printf("\nError! No Group/Option is allowed in Group!");
    return FALSE;
  }  
  if (count.iRuleCount > 1)
  {
    printf("\nError! Only one RuleName is allowed in Group!");
    return FALSE;
  }
  if (count.iRuleCount == 0)
  {
    printf("\nError! No RuleName in Group!");
    return FALSE;
  }

  /* Create a new rule and modify Group repet */
  for (i = 0; i < pConcat->iCount; i++)
  {
    pRepInGrp = pConcat->pRepetition + i;
  
    if (pRepInGrp->iType == ELEMENT_RULE)
    {
      /* Repeat of Rule in group must be 1 */
      if (pRepInGrp->iRepMin == 1 && pRepInGrp->iRepMax == 1)
	    {
        pRep->pName = Strdup(pRepInGrp->pName);
        pRep->iType = ELEMENT_GRP_RULE;	

	      CreateNewRule(pRep->pName, pRep,g_pRuleList, g_pNewRuleList);        
        return TRUE;
      }
	    else
      {
        printf("\nError! The repeat of Rule: %s in Group must be 1!", pRep->pName);
        return FALSE;
      }
    }
  }  
  
  return FALSE;
}

/***************************************************************************
 * Function          : ProcessOpt
 *
 * Description       : Check the validity of Option repetition and decide 
 *                     its tpye, if the Option is valid, create a new rule
 *                     into NewRuleList and change its type.
 *                     
 * Input parameters  : pRep - pointer of Option repetition
 *                     g_pRuleList - parameter passed to internal function
 *                     g_pNewRuleList - parameter passed to internal function
 *
 * Output parameters : pRep - processed option repet
 *                     pRuleList - processed rule list
 *                     pNewRuleList - new rule list
 *
 * Return value      : TRUE  - Option repetition is valid
 *                     FALSE - Option repetition is invalid
 *
 * Comments          : The valid formats of Option :
 *                     ELEMENT_OPT_RULE : [*WSP Rule]
 *                     ELEMENT_OPT_LST  : [Rule *(*WSP RULE)]
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL ProcessOpt(TRepetition *pRep, TRuleList *pRuleList,
                    TRuleList *pNewRuleList)
{
  int i;
  TConcatenation *pConcat;
  
  TElementCount count;
  memset(&count, 0, sizeof(TElementCount));

  /* Choice type, no support */
  if (pRep->GroupOption.Rule.iCount > 1) 
  {
    printf("\nError! No Choice is allowed in Option!");
    return FALSE;
  }
  
  /* Repeat must be 1 */
  if (pRep->iRepMin != 1 || pRep->iRepMax != 1)
  {
    printf("\nError! Option Repeat must be 1!");
    return FALSE;
  }  

  pConcat = (TConcatenation*)pRep->GroupOption.Rule.pConcat;

  /* Process Token and OneElem Group */
  for (i = 0; i < pConcat->iCount; i++)
    PreProcessRepet(pConcat->pRepetition + i);

  CountRepNumInConcat(pConcat, &count);  
  if (count.iOptCount > 0)
  {
    printf("\nError! No Option is allowed in Option!");
    return FALSE;
  }

  /* If Opt_Lst type, only one Group allowed */
  if (count.iGrpCount > 1)
  {
    printf("\nError! At most one Group is allowed in Option!");
    return FALSE;
  }
  
  if (count.iRuleCount > 1)
  {
    printf("\nError! Only one RuleName is allowed in Option!");
    return FALSE;
  }

  /* Only one string in Option is allowed */
  if (count.iRuleCount == 0)
  {
    if (count.iTotalCount == 1 && count.iStringCount ==1)
    {
      TRepetition *pStrRept = pConcat->pRepetition;

      if (pStrRept->iRepMin != 1 || pStrRept->iRepMax != 1)
      {
        printf("\nError! Repeat must be 1 in String Option!");
        return FALSE;
      }

      /* Convert Opt_String to be String type */
      pRep->iType = ELEMENT_STRING;
      pRep->pName = Strdup(pStrRept->pName);

      /* Min repeat become 0 */
      pRep->iRepMin = 0;
    }
    else
    {
      printf("\nError! No RuleName in Option!");
      return FALSE;
    }

    return TRUE;
  }

  /* Opt_Lst type process */
  if (count.iRuleCount == 1 && count.iGrpCount == 1 && count.iTotalCount == 2)
    return ProcessLstOpt(pRep, pRuleList, pNewRuleList);

  /* Only one RuleName, Opt_Rule type procsee */
  if (count.iRuleCount == 1 && count.iGrpCount == 0)
    return ProcessRuleOpt(pRep, pRuleList, pNewRuleList);

  /* Unsupported format */
  printf("\nError! The Option format is invalid!");
  return FALSE;
}

/***************************************************************************
 * Function          : ProcessGroupInOpt
 *
 * Description       : Check the validity of the Group repetition in an option
 *                     and return its name
 *                                        
 * Input parameters  : pGrpRep - the group repetiton in option
 *                     pRuleName - pointer to returned repet name
 *                     
 * Output parameters : pGrpRep - processed group repet
 *                     pRuleName - pointer to group repet name in option
 *
 * Return value      : TRUE  - group repetition is valid
 *                     FALSE - group repetition is invalid
 *
 * Comments          : The group repet is in the option which format is:
 *                     [IdName *(LSWP "," LSWP IdName)]
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL ProcessGroupInOpt(TRepetition *pGrpRep, char **pRuleName)
{
  int i,j;
  TConcatenation *pConcat;
  TRepetition *pRepinGrp;

  TElementCount count;
  memset(&count, 0, sizeof(TElementCount));

  /* Choice type, no support */
  if (pGrpRep->GroupOption.Rule.iCount > 1)
  {  
    printf("\nError! No Choice is allowed in Group!");
    return FALSE;
  }

  /* Sequence type */
  pConcat =  (TConcatenation*)pGrpRep->GroupOption.Rule.pConcat;

  /* Process Token and OneElem Group */
  for (j = 0; j < pConcat->iCount; j++)
    PreProcessRepet(pConcat->pRepetition + j);

  /* Check the Group's validity */
  CountRepNumInConcat(pConcat, &count);
  if (count.iGrpCount+count.iOptCount > 0)
  {
    printf("\nError! No Group/Option is allowed in Group!");
    return FALSE;
  }  
  if (count.iRuleCount > 1)
  {
    printf("\nError! Only one RuleName is allowed in Group!");
    return FALSE;
  }
  if (count.iRuleCount == 0)
  {
    printf("\nError! No RuleName in Group!");
    return FALSE;
  }

  /* Check repeat of Rule repetition in group*/
  for (i = 0; i < pConcat->iCount; i++)
  {
    pRepinGrp = pConcat->pRepetition + i;
  
    if (pRepinGrp->iType == ELEMENT_RULE)
    {
      /* Repeat of rule in group must be 1 */
      if (pRepinGrp->iRepMin == 1 && pRepinGrp->iRepMax == 1)
      {
        *pRuleName = pRepinGrp->pName;
        return TRUE;
      }
      else
      {
        printf("\nError! The repeat of Rule in Group must be 1!");
        return FALSE;
      }
    }
  }

  return FALSE;
}
/***************************************************************************
 * Function          : ProcessRuleOpt
 *
 * Description       : Check the validity of RuleOption repetitione, if the
 *                     format is valid, create a new rule into NewRuleList
 *                     and change its type.
 *                     
 * Input parameters  : pRep - pointer of Option repetition
 *                     g_pRuleList - parameter passed to internal function
 *                     g_pNewRuleList - parameter passed to internal function
 *
 * Output parameters : pRep - processed option repet
 *                     pRuleList - processed rule list
 *                     pNewRuleList - new rule list
 *
 * Return value      : TRUE  - Option repetition is valid
 *                     FALSE - Option repetition is invalid
 *
 * Comments          : The valid formats of RuleOption :
 *                     ELEMENT_OPT_RULE : [*WSP Rule "abcd"]
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL ProcessRuleOpt(TRepetition *pRep, TRuleList *g_pRuleList,
                         TRuleList *g_pNewRuleList)
{
  int i;
  TConcatenation *pConcat;
  TRepetition *pRepInOpt;
  
  pConcat = (TConcatenation*)pRep->GroupOption.Rule.pConcat;

  /* Check Rule repetition */
  for (i = 0; i < pConcat->iCount; i++)
  {
    pRepInOpt = pConcat->pRepetition + i;

	  /* Find the RuleName */
    if (pRepInOpt->iType == ELEMENT_RULE)
    {
      /* Repeat of rule in Option must be 1 */
      if (pRepInOpt->iRepMin == 1 && pRepInOpt->iRepMax == 1)
      {
        pRep->pName = Strdup(pRepInOpt->pName);
        pRep->iType = ELEMENT_OPT_RULE;
        
        CreateNewRule(pRep->pName, pRep,g_pRuleList, g_pNewRuleList);       
        return TRUE;
      }
  	  else
      {
        printf("\nError! The repeat of Rule in Opt must be 1!");
        return FALSE;
      }
    }
  }
  
  return FALSE;
}
/***************************************************************************
 * Function          : ProcessLstOpt
 *
 * Description       : Check the validity of OptLst repetition, if the format
 *                     is valid, create a new rule into NewRuleList and change
 *                     its type.
 *                     
 * Input parameters  : pRep - pointer of Option repetition
 *                     g_pRuleList - parameter passed to internal function
 *                     g_pNewRuleList - parameter passed to internal function
 *
 * Output parameters : pRep - processed option repet
 *                     pRuleList - processed rule list
 *                     pNewRuleList - new rule list
 *
 * Return value      : TRUE  - Option repetition is valid
 *                     FALSE - Option repetition is invalid
 *
 * Comments          : The valid formats of Option :
 *                     ELEMENT_OPT_LST  : [Rule *(*WSP RULE)]
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
BOOL ProcessLstOpt(TRepetition *pRep, TRuleList *pRuleList,
                        TRuleList *pNewRuleList)
{
  TRepetition *RuleRep, *GrpRep;	
  char *pGrpName = NULL;

  RuleRep = ((TConcatenation*)pRep->GroupOption.Rule.pConcat)->pRepetition;
  GrpRep = ((TConcatenation*)pRep->GroupOption.Rule.pConcat)->pRepetition + 1;

  /* First repet must be rule and repeat must be 1 */
  if (RuleRep->iType == ELEMENT_RULE
  	  && RuleRep->iRepMin == 1
  	  && RuleRep->iRepMax == 1)
  {
    /* Second repet must be GroupRule */
    if (GrpRep->iType == ELEMENT_GROUP)
    {
      if (!ProcessGroupInOpt(GrpRep, &pGrpName))
      {
        printf("\nError! The Group format in Opt is invalid!");
        return FALSE;
      }
      else
      {
        Assert(pGrpName);

        if (StrCaseCmp(RuleRep->pName,pGrpName) == 0)
  	    {
          /* Name of Rule repet is same as Group repet */
          pRep->pName = Strdup(RuleRep->pName);

⌨️ 快捷键说明

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