提交 c09dd6f6 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 aefff949
#ifndef PICKUPLEFTFACTOR_H_
#define PICKUPLEFTFACTOR_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
//
// 在此处定义数据结构
//
......@@ -22,7 +18,8 @@
#define MAX_STR_LENGTH 64
struct _Rule;
typedef struct _RuleSymbol{
typedef struct _RuleSymbol
{
struct _RuleSymbol* pNextSymbol; // 指向下一个 Symbol
struct _RuleSymbol* pOther; // 指向下一个 Select
int isToken; // 是否为终结符。1 表示终结符,0 表示非终结符
......@@ -30,13 +27,13 @@ typedef struct _RuleSymbol{
struct _Rule* pRule; // 指向 Symbol 对应的 Rule。isToken 为 0 时这个域有效
}RuleSymbol;
typedef struct _Rule{
typedef struct _Rule
{
char RuleName[MAX_STR_LENGTH]; // 文法的名称
struct _RuleSymbol* pFirstSymbol; // 指向文法的第一个 Select 的第一个 Symbol
struct _Rule* pNextRule; // 指向下一条文法
}Rule;
//
// 在此处声明函数
//
......@@ -57,7 +54,6 @@ RuleSymbol* CreateSymbol();
Rule* FindRule(Rule* pHead, const char* RuleName);
void PrintRule(Rule* pHead);
//
// 在此声明全局变量
//
......
......@@ -54,10 +54,13 @@ RuleSymbol* GetSymbol(RuleSymbol* pSelect, int index)
{
int i = 0;
RuleSymbol* pRuleSymbol;
for(pRuleSymbol = pSelect, i = 0; pRuleSymbol != NULL; pRuleSymbol = pRuleSymbol->pNextSymbol, i++)
for (pRuleSymbol = pSelect, i = 0; pRuleSymbol != NULL;
pRuleSymbol = pRuleSymbol->pNextSymbol, i++)
{
if(i == index)
if (i == index)
{
return pRuleSymbol;
}
}
return NULL;
......@@ -151,9 +154,9 @@ void AddSelectToRule(Rule* pRule, RuleSymbol* pNewSelect)
void GetUniqueRuleName(Rule* pHead, char* pRuleName)
{
Rule* pRuleCursor = pHead;
for(; pRuleCursor != NULL;)
for (; pRuleCursor != NULL;)
{
if(0 == strcmp(pRuleCursor->RuleName, pRuleName))
if (0 == strcmp(pRuleCursor->RuleName, pRuleName))
{
strcat(pRuleName, Postfix);
pRuleCursor = pHead;
......@@ -207,12 +210,14 @@ void PickupLeftFactor(Rule* pHead)
返回值:
文法的头指针
*/
typedef struct _SYMBOL{
typedef struct _SYMBOL
{
int isToken;
char Name[MAX_STR_LENGTH];
}SYMBOL;
typedef struct _RULE_ENTRY{
typedef struct _RULE_ENTRY
{
char RuleName[MAX_STR_LENGTH];
SYMBOL Selects[64][64];
}RULE_ENTRY;
......@@ -220,7 +225,8 @@ typedef struct _RULE_ENTRY{
static const RULE_ENTRY rule_table[] =
{
/* A -> abC | abcD | abcE */
{ "A", {
{ "A",
{
{ { 1, "a" }, { 1, "b" }, { 1, "C" } },
{ { 1, "a" }, { 1, "b" }, { 1, "c" }, { 1, "D" } },
{ { 1, "a" }, { 1, "b" }, { 1, "c" }, { 1, "E" } }
......@@ -243,33 +249,33 @@ Rule* InitRules()
int i, j, k;
Rule** pRulePtr = &pHead;
for(i=0; i<nRuleCount; i++)
for (i=0; i<nRuleCount; i++)
{
*pRulePtr = CreateRule(rule_table[i].RuleName);
pRulePtr = &(*pRulePtr)->pNextRule;
}
pRule = pHead;
for(i=0; i<nRuleCount; i++)
for (i=0; i<nRuleCount; i++)
{
pSymbolPtr1 = &pRule->pFirstSymbol;
for(j=0; rule_table[i].Selects[j][0].Name[0] != '\0'; j++)
for (j=0; rule_table[i].Selects[j][0].Name[0] != '\0'; j++)
{
pSymbolPtr2 = pSymbolPtr1;
for(k=0; rule_table[i].Selects[j][k].Name[0] != '\0'; k++)
for (k=0; rule_table[i].Selects[j][k].Name[0] != '\0'; k++)
{
const SYMBOL* pSymbol = &rule_table[i].Selects[j][k];
*pSymbolPtr2 = CreateSymbol();
(*pSymbolPtr2)->isToken = pSymbol->isToken;
if(1 == pSymbol->isToken)
if (1 == pSymbol->isToken)
{
strcpy((*pSymbolPtr2)->TokenName, pSymbol->Name);
}
else
{
(*pSymbolPtr2)->pRule = FindRule(pHead, pSymbol->Name);
if(NULL == (*pSymbolPtr2)->pRule)
if (NULL == (*pSymbolPtr2)->pRule)
{
printf("Init rules error, miss rule \"%s\"\n", pSymbol->Name);
exit(1);
......@@ -297,18 +303,18 @@ Rule* InitRules()
Rule* InitRules_CI()
{
int nRuleCount = 0;
for(int i = 0; i < 20; i++)
for (int i = 0; i < 20; i++)
{
gets(rule_table_ci[i]);
int length = strlen(rule_table_ci[i]);
if(length == 0)
if (length == 0)
{
break;
}
for(int j = 0; j < length; j++)
for (int j = 0; j < length; j++)
{
if(rule_table_ci[i][j] == ' ')
if (rule_table_ci[i][j] == ' ')
{
ruleNameArr[i][j] = '\0';
break;
......@@ -325,21 +331,21 @@ Rule* InitRules_CI()
int i, j, k;
Rule** pRulePtr = &pHead;
for(i=0; i<nRuleCount; i++)
for (i=0; i<nRuleCount; i++)
{
*pRulePtr = CreateRule(ruleNameArr[i]);
pRulePtr = &(*pRulePtr)->pNextRule;
}
pRule = pHead;
for(i=0; i<nRuleCount; i++)
for (i=0; i<nRuleCount; i++)
{
pSymbolPtr1 = &pRule->pFirstSymbol;
int start = 0;
for(int j=0; rule_table_ci[i][j] != '\0'; j++)
for (int j=0; rule_table_ci[i][j] != '\0'; j++)
{
if(rule_table_ci[i][j] == ' '
if (rule_table_ci[i][j] == ' '
&& rule_table_ci[i][j + 1] == '-'
&& rule_table_ci[i][j + 2] == '>'
&& rule_table_ci[i][j + 3] == ' ')
......@@ -349,19 +355,19 @@ Rule* InitRules_CI()
}
}
for(k = start; rule_table_ci[i][k] != '\0'; k++)
for (k = start; rule_table_ci[i][k] != '\0'; k++)
{
if(rule_table_ci[i][k] == '|')
if (rule_table_ci[i][k] == '|')
{
pSymbolPtr1 = &(*pSymbolPtr1)->pOther;
pSymbolPtr2 = pSymbolPtr1;
continue;
}
if(rule_table_ci[i][k] == ' ')
if (rule_table_ci[i][k] == ' ')
{
continue;
}
if(k == start)
if (k == start)
{
pSymbolPtr2 = pSymbolPtr1;
}
......@@ -372,20 +378,20 @@ Rule* InitRules_CI()
tokenName[0] = rule_table_ci[i][k];
tokenName[1] = '\0';
(*pSymbolPtr2)->isToken = 1;
for(int m = 0; m < nRuleCount; m++)
for (int m = 0; m < nRuleCount; m++)
{
if(strcmp(tokenName,ruleNameArr[m]) == 0)
if (strcmp(tokenName, ruleNameArr[m]) == 0)
{
(*pSymbolPtr2)->isToken = 0;
(*pSymbolPtr2)->pRule = FindRule(pHead, tokenName);
if(NULL == (*pSymbolPtr2)->pRule)
if (NULL == (*pSymbolPtr2)->pRule)
{
printf("Init rules error, miss rule \"%s\"\n", tokenName);
exit(1);
}
}
}
if((*pSymbolPtr2)->isToken == 1)
if ((*pSymbolPtr2)->isToken == 1)
{
strcpy((*pSymbolPtr2)->TokenName, tokenName);
}
......@@ -455,9 +461,9 @@ RuleSymbol* CreateSymbol()
Rule* FindRule(Rule* pHead, const char* RuleName)
{
Rule* pRule;
for(pRule = pHead; pRule != NULL; pRule = pRule->pNextRule)
for (pRule = pHead; pRule != NULL; pRule = pRule->pNextRule)
{
if(0 == strcmp(pRule->RuleName, RuleName))
if (0 == strcmp(pRule->RuleName, RuleName))
{
break;
}
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论