📄 compiler.c
字号:
pRep->iType = ELEMENT_OPT_LST;
pRep->iRepMin = GrpRep->iRepMin;
pRep->iRepMax = GrpRep->iRepMax;
CreateNewRule(RuleRep->pName, pRep,pRuleList, pNewRuleList);
return TRUE;
}
else
{
printf("\nError! Name of Grp in LST_Opt must be same as first RuleName!");
return FALSE;
}
}
}
else
{
printf("\nError! The Second Repet must be Grp in LstOpt!");
return FALSE;
}
}
else
{
printf("\nError! The First Repet must be One RuleName in LstOpt!");
return FALSE;
}
}
/***************************************************************************
* Function : CountRepNumInConcat
*
* Description : Count repetition number in an concatenation
*
* Input parameters : pConcat - concatenation pointer
* count - pointer to Element counter
*
* Output parameters : count - element numbers in concat
*
* Return value : None
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
void CountRepNumInConcat(TConcatenation *pConcat, TElementCount *count)
{
int i;
for (i = 0; i < pConcat->iCount; i++)
{
switch (pConcat->pRepetition[i].iType)
{
case ELEMENT_RULE:
count->iRuleCount++;
count->iTotalCount++;
break;
case ELEMENT_TOKEN:
count->iTokenCount++;
count->iTotalCount++;
break;
case ELEMENT_CHAR:
count->iCharCount++;
count->iTotalCount++;
break;
case ELEMENT_STRING:
count->iStringCount++;
count->iTotalCount++;
break;
case ELEMENT_GROUP:
count->iGrpCount++;
count->iTotalCount++;
break;
case ELEMENT_OPTION:
count->iOptCount++;
count->iTotalCount++;
break;
case ELEMENT_RULE_LST_RULE:
count->iRuleLstRule++;
count->iTotalCount++;
break;
case ELEMENT_RULE_LST_GRP:
count->iRuleLstGrp++;
count->iTotalCount++;
break;
case ELEMENT_GRP_RULE:
count->iRuleGrpCount++;
count->iTotalCount++;
break;
case ELEMENT_OPT_RULE:
count->iOptRuleCount++;
count->iTotalCount++;
break;
case ELEMENT_OPT_LST:
count->iOptLstCount++;
count->iTotalCount++;
break;
default:
printf("\nError! Unknown element type %d !", pConcat->pRepetition[i].iType);
printf("element name: %s \n", pConcat->pRepetition[i].pName);
break;
}
}
}
/***************************************************************************
* Function : ProcessSeqRule
*
* Description : Decide the type of Sequence rule
*
* Input parameters : pRule - pointer to sequence rule
*
* Output parameters : pRule - processed rule
*
* Return value : Always return TRUE
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
BOOL ProcessSeqRule(TRule *pRule)
{
TElementCount count;
TConcatenation *pConcat = (TConcatenation*)pRule->pConcat;
memset(&count, 0, sizeof(TElementCount));
CountRepNumInConcat(pConcat, &count);
/* OneElement type check */
if (count.iRuleCount == 1 && count.iTotalCount == 1)
{
/* Only one element in SeqRule */
if (pConcat->pRepetition->iRepMax == 1 && pConcat->pRepetition->iRepMin == 1)
pRule->iType = RULE_ONE_ELEM;
else pRule->iType = RULE_SEQUENCE;
return TRUE;
}
/* String type check */
if (count.iRuleCount == 0 && count.iRuleGrpCount == 0
&& count.iOptLstCount == 0 && count.iOptRuleCount == 0
&& count.iRuleLstGrp == 0)
pRule->iType = RULE_STRING;
else
{
pRule->iType = RULE_SEQUENCE;
}
return TRUE;
}
/***************************************************************************
* Function : ProcessChoiceRule
*
* Description : Check validity of Choice rule and decide its type
*
* Input parameters : pRule - pointer to choice rule
*
* Output parameters : pRule - processed rule
*
* Return value : If choice rule format is valid return TRUE, otherwise
* return FALSE.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
BOOL ProcessChoiceRule(TRule *pRule)
{
TRepetition *pRep, *pPreRep, *pNextRep;
TConcatenation *pConcat;
int i,j;
char ChoiceName[200];
TElementCount count;
memset(&count, 0, sizeof(TElementCount));
/* Process every Concatenation in choice */
for (i = 0; i < pRule->iCount; i++)
{
pConcat = (TConcatenation*)pRule->pConcat + i;
/* Only One repetition is allowed in every concatenation of the choice*/
if (pConcat->iCount != 1)
{
printf("\nError! Only one repetition is allowed in concatenation of Choice: %s !",
pRule->pRuleName);
return FALSE;
}
pRep = pConcat->pRepetition;
/* Repeat must be 1 */
if (pRep->iRepMin != 1 || pRep->iRepMax != 1)
{
printf("\nError, Repetition's repeat must be 1 in concatenation of choice rule: %s !",
pRule->pRuleName);
return FALSE;
}
/* Decide choice rule type */
switch (pRep->iType)
{
case ELEMENT_RULE:
pConcat->iType = CHOICE_RULE;
count.iRuleCount++;
/* Store the Choice variable name */
memset(ChoiceName, 0, sizeof(ChoiceName));
ConvertName(ChoiceName, pRep->pName);
pRep->pVaribleName = Strdup(ChoiceName);
break;
case ELEMENT_STRING:
pConcat->iType = CHOICE_STR;
/* Store the Choice variable name */
memset(ChoiceName, 0, sizeof(ChoiceName));
ConvertName(ChoiceName, pRep->pName);
pRep->pVaribleName = Strdup(ChoiceName);
break;
case ELEMENT_TOKEN:
pConcat->iType = CHOICE_TOKEN;
/* Store the Choice variable name */
memset(ChoiceName, 0, sizeof(ChoiceName));
ConvertName(ChoiceName, pRep->pName);
pRep->pVaribleName = Strdup(ChoiceName);
break;
case ELEMENT_CHAR:
pConcat->iType = CHOICE_CHAR;
count.iCharCount++;
break;
default:
printf("\nError, Unsupported Choice concat type %d in %s!",
pRep->iType, pRule->pRuleName);
return FALSE;
}
}
/* Choice Rule type ? */
if (count.iRuleCount != 0)
{
if (count.iCharCount != 0)
{
/* Rule and char are all in choice, no support */
printf("\nError, Value type is not allowed in choice rule: %s!", pRule->pRuleName);
return FALSE;
}
/* Only one rule type repet in choice concat */
pRule->iType = RULE_CHOICE;
/* Check if Choice Name is dup */
for (i = 0; i < pRule->iCount-1; i++)
{
pPreRep = ((TConcatenation*)pRule->pConcat + i)->pRepetition;
for (j = i+1; j < pRule->iCount; j++)
{
pNextRep = ((TConcatenation*)pRule->pConcat + j)->pRepetition;
if (StrCaseCmp(pPreRep->pName, pNextRep->pName)== 0)
{
printf("\nError, Duplicate Choice name: %s in %s!",
pPreRep->pName, pRule->pRuleName);
return FALSE;
}
}
}
return TRUE;
}
else
{
/* No rule type in choice, Choice String type */
pRule->iType = RULE_STRING_CHOICE;
return TRUE;
}
}
/***************************************************************************
* Function : ProcessRule
*
* Description : Check the validity of the rule and decide its type
*
* Input parameters : pRule - pointer of rule
* pRuleList - parameter passed to internal function
* pNewRuleList - parameter passed to internal function
*
* Output parameters : pRule - processed rule
* pRuleList - processed rule list
* pNewRuleList - new rule list
*
* Return value : If rule format is valid return TRUE, otherwise
* return FALSE.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
BOOL ProcessRule(TRule *pRule, TRuleList *pRuleList, TRuleList *pNewRuleList)
{
int i;
TElementCount count;
memset(&count, 0, sizeof(TElementCount));
Assert(pRule);
Assert(pRuleList);
Assert(pNewRuleList);
/* Process concats in rule */
for (i = 0; i < pRule->iCount; i++)
{
if (!ProcessConcat(((TConcatenation*)pRule->pConcat)+i, pRuleList,
pNewRuleList))
{
printf("\nFail to process rule: %s!", pRule->pRuleName);
return FALSE;
}
}
/* Process Sequenc rule */
if (pRule->iCount == 1)
return ProcessSeqRule(pRule);
else /* Process choice type */
return ProcessChoiceRule(pRule);
}
/***************************************************************************
* Function : ProcessRuleList
*
* Description : 1)Check validity and decide type of every rule in rule
* list, after processed, some new created rules mybe
* added into the original rule list.
*
* 2)If all rules are valid, find associate rule of every
* repetition in every rule and store the pointers of associate
* rules into the pointer list.
*
* Input parameters : pRuleList - pointer to rule list
*
* Output parameters : pRuleList - processed rule list
*
* Return value : If format of all rules are valid and are reordered
* successfully, return TRUE, otherwise return FALSE.
*
* Comments :
*
* History :
* 2005/04/15 : Creation
*
* Date : April 15 2005, Frank Zhang
**************************************************************************/
BOOL ProcessRuleList(TRuleList *pRuleList)
{
int i;
TRuleList NewRuleList;
TRule *pTempRule;
InitRuleList(&NewRuleList);
/*Check validity of every rule and decide its type*/
for (i = 0; i < pRuleList->iCount; i++)
{
if (!ProcessRule(pRuleList->pRule+i, pRuleList, &NewRuleList))
return FALSE;
}
/* Merger the original rule list and the rule list storing the new created rules */
MergeRules(pRuleList, &NewRuleList);
/* Find the repetition's link rule of every rule*/
if (!LinkRule(pRuleList))
{
printf("\nError! Fail to Link rule!");
return FALSE;
}
/* Check dupplicate rule name */
pTempRule = CheckDupRuleName(pRuleList);
if (pTempRule != NULL)
{
printf("\nError! Duplicate rule name: %s !", pTempRule->pRuleName);
return FALSE;
}
/* Reorder the rules and store its pointer into pointer list with new order */
if (SortRule(pRuleList, &pRuleList->RulePntLst))
return TRUE;
else return FALSE;
}
/***************************************************************************
* Function : SortRule
*
* Description : Sort rule according its association. In the created
* c style header file which defines the structure of
* rules, structs must have specific sequence according
* their association, so before print the head file, all
* rules must be reordered.
*
* Input parameters : pRuleList - pointer to original rule list to be sorted
* pArray - pointer to single list which store pointers of
* sorted rule.
*
* Output parameters : pArray - list store the reordered rule pointer
*
* Return value : TRUE if all rules are reordered, otherwise return FALSE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -