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

📄 compiler.c

📁 mgcp协议源代码和测试程序,还有一个编译器
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************
*      Copyright (C), 2005-2005 
*      All rights reserved
*
*      Authors                   :  Frank ZHANG
*      Description               :  
*
*
*      Date of creation          :  04/15/2005
*
*
*      History                   :
*      2005/04/15 Frank ZHANG    : - Creation
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "typedef.h"
#include "misc.h"
#include "compiler.h"
#include "list.h"
#include "debg.h"



/***************************************************************************
 * Function          : AddNewRule
 *
 * Description       : Add a new rule into the rule list.
 *                     
 * Input parameters  : pRuleList - pointer to the rule list to add rule
 *                     pRuleName - pointer to name of added rule
 *
 * Output parameters : pRuleList - modified rule list
 *
 * Return value      : The new created rule pointer
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 *  Date             : April 15 2005, Frank Zhang
 **************************************************************************/
TRule* AddNewRule(TRuleList* pRuleList, char* pRuleName)
{
  int iIndex = pRuleList->iCount;
  
  pRuleList->iCount++;
  pRuleList->pRule = (TRule*)realloc(pRuleList->pRule,
                                     sizeof(TRule)*pRuleList->iCount);
  Assert(pRuleList->pRule);
  memset(pRuleList->pRule+iIndex, 0, sizeof(TRule));
  pRuleList->pRule[iIndex].pRuleName = Strdup(pRuleName);

  return pRuleList->pRule+iIndex;
}
/***************************************************************************
 * Function          : AddNewConcatenation
 *
 * Description       : Add a new concatenation into the rule
 *                                       
 * Input parameters  : pRule - pointer to  the rule int which new concatenation
 *                             be added
 *                     
 * Output parameters : pRule - modified rule
 *
 * Return value      : The new concatenation pointer
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
TConcatenation* AddNewConcatenation(TRule* pRule)
{
  int iIndex = pRule->iCount;
  
  pRule->iCount++;
  pRule->pConcat = realloc(pRule->pConcat, sizeof(TConcatenation)*pRule->iCount);
  Assert(pRule->pConcat);
  memset((TConcatenation*)(pRule->pConcat)+iIndex, 0, sizeof(TConcatenation));
  
  return (TConcatenation*)pRule->pConcat+iIndex;
}

/***************************************************************************
 * Function          : AddNewRepetition
 *
 * Description       : Add a new Repetition into the concatenation
 *                                        
 * Input parameters  : pconcat - pointer to the concatenation into
 *                               which new Repetition be added into
 *                     
 * Output parameters : pConcat - modified concatenation
 *
 * Return value      : The new Repetition pointer
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
TRepetition* AddNewRepetition(TConcatenation* pConcat)
{	    
  int iIndex = pConcat->iCount;
  
  pConcat->iCount++;
  pConcat->pRepetition = (TRepetition*)realloc(pConcat->pRepetition,
                                               sizeof(TRepetition)*pConcat->iCount);
  Assert(pConcat->pRepetition);
  memset(pConcat->pRepetition+iIndex, 0, sizeof(TRepetition));
  
  return pConcat->pRepetition+iIndex;
}
/***************************************************************************
 * Function          : InitRuleList
 *
 * Description       : Initialize the ruleList
 *                                          
 * Input parameters  : pRuleList - pointer of the rule list 
 *                     
 * Output parameters : pRuleList - initialized rule list
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void InitRuleList(TRuleList *pRuleList)
{
  if (pRuleList != NULL)
  {
    pRuleList->iCount = 0;
    pRuleList->pRule = NULL;
    SListInit(&pRuleList->RulePntLst);
  }
}
/***************************************************************************
 * Function          : FreeRuleList
 *
 * Description       : Free allocated memories in rule list
 *                                          
 * Input parameters  : pRuleList - pointer to the rule list to be freed 
 *                     
 * Output parameters : pRuleList - all rules are freed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void FreeRuleList(TRuleList* pRuleList)
{
  if (pRuleList != NULL)
  {
    while(pRuleList->iCount > 0)
    {
      FreeRule(&(pRuleList->pRule[pRuleList->iCount-1]));      
      pRuleList->iCount--;  
    }

    if (pRuleList->pRule != NULL)
    {
      free(pRuleList->pRule);
      pRuleList->pRule = NULL;
    }

    SListDel(&pRuleList->RulePntLst);
  }
}
/***************************************************************************
 * Function          : FreeRule
 *
 * Description       : Free allocate memories in rule
 *                                          
 * Input parameters  : pRule - pointer to the rule to be freed
 *                     
 * Output parameters : pRule - all memories are freed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void FreeRule(TRule* pRule)
{
  if (pRule != NULL)
  {
    if (pRule->pRuleName != NULL)
    {
      free(pRule->pRuleName);
      pRule->pRuleName = NULL;
    }
    
    while(pRule->iCount > 0)
    {
      FreeConcatenation((TConcatenation*)pRule->pConcat + pRule->iCount-1);
      pRule->iCount--;
    }
    
    if (pRule->pConcat != NULL)
    {
      free(pRule->pConcat);      
      pRule->pConcat = NULL;
    }
  }  
}

/***************************************************************************
 * Function          : FreeConcatenation
 *
 * Description       : Free allocate memories in Concatenation
 *                                        
 * Input parameters  : pConcat - pointer to the Concatenation to be freed
 *                     
 * Output parameters : pConcat - all memories are freed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void FreeConcatenation(TConcatenation* pConcat)
{
  if (pConcat != NULL)
  {
    while(pConcat->iCount > 0)
    {
      FreeRepetition(&(pConcat->pRepetition[pConcat->iCount-1]));
  	  pConcat->iCount--;
    }
  
    if (pConcat->pRepetition != NULL)
    {
      free(pConcat->pRepetition);
      pConcat->pRepetition = NULL;
    }
  }
}
/***************************************************************************
 * Function          : FreeRepetition
 *
 * Description       : Free allocate memories in Repetition
 *                                         
 * Input parameters  : pRepet - pointer to the Repetition to be freed
 *                     
 * Output parameters : pRepet - all memories are freed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/

void FreeRepetition(TRepetition* pRepet)
{
  if (pRepet != NULL)
  {
    if (pRepet->pVaribleName != NULL)
    {
    	free(pRepet->pVaribleName);
      pRepet->pVaribleName = NULL;
    }
    
    if (pRepet->pName != NULL)
    {
    	free(pRepet->pName);
      pRepet->pName = NULL;
    }
    
    if (pRepet->pRuleName != NULL)
    {
    	free(pRepet->pRuleName);
      pRepet->pRuleName = NULL;
    }
    
    FreeGroupOption(&pRepet->GroupOption);
  }
}
/***************************************************************************
 * Function          : FreeGroupOption
 *
 * Description       : Free allocate memories in GroupOption
 *                                         
 * Input parameters  : pGrpOpt - pointer to the GroupOption to be freed
 *                     
 * Output parameters : pGrpOpt - all memories are freed
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void FreeGroupOption(TGroupOption* pGrpOpt)
{
  int iConcatNum = pGrpOpt->Rule.iCount;

  if (pGrpOpt->Rule.pRuleName != NULL)
  {
    free(pGrpOpt->Rule.pRuleName);
    pGrpOpt->Rule.pRuleName = NULL;
  }

  while(pGrpOpt->Rule.iCount > 0)
  {
    FreeConcatenation((TConcatenation*)pGrpOpt->Rule.pConcat + pGrpOpt->Rule.iCount-1);
    pGrpOpt->Rule.iCount--;
  }

  /* If have concatenations in group */
  if (iConcatNum > 0)
  {
    free(pGrpOpt->Rule.pConcat);
    pGrpOpt->Rule.pConcat = NULL;
  }
}
/***************************************************************************
 * Function          : InitStack
 *
 * Description       : Initialize stack
 *                                          
 * Input parameters  : pStack - pointer to stack
 *                     
 * Output parameters : pStack - initialized stack
 *
 * Return value      : None
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void InitStack(TStack *pStack)
{
  pStack->iCount = 0;
  memset(pStack->pItem, 0, sizeof(pStack->pItem));
}
/***************************************************************************
 * Function          : PushStack
 *
 * Description       : Push an item into stack
 *                                          
 * Input parameters  : pStack - pointer to stack
 *                     pItem - pointer to item to be pushed
 *                     
 * Output parameters : pStack - modified stack
 *
 * Return value      : FALSE - stack overflow
 *                     TRUE - succeed to push item 
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
int PushStack(TStack *pStack, void* pItem)
{
  if (pStack->iCount >= STACK_SIZE)
  	return FALSE;

  pStack->pItem[pStack->iCount] = pItem;
  pStack->iCount++;

  return TRUE;
}
/***************************************************************************
 * Function          : PopStack
 *
 * Description       : Pop item from stack
 *                                         
 * Input parameters  : pStack - pointer to stack
 *                     
 * Output parameters : pStack - modified stack
 *
 * Return value      : The item pointer popped, if no item in stack, return NULL.
 *
 * Comments          : 
 *
 * History           :
 *  2005/04/15       : Creation
 *
 * Date              : April 15 2005, Frank Zhang
 **************************************************************************/
void* PopStack(TStack *pStack)
{
  if (pStack->iCount <= 0)
    return NULL;
 
  pStack->iCount--;
  return pStack->pItem[pStack->iCount];;
}
/***************************************************************************
 * Function          : CreateNewRule

⌨️ 快捷键说明

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