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

modify

上级 e7c6daed
#ifndef REMOVELEFTRECURSIONNOREPLACE_H_ #ifndef REMOVELEFTRECURSIONNOREPLACE_H_
#define REMOVELEFTRECURSIONNOREPLACE_H_ #define REMOVELEFTRECURSIONNOREPLACE_H_
// //
// 在此处包含 C 标准库头文件 // 在此处包含 C 标准库头文件
// //
#include <stdio.h> #include <stdio.h>
// //
// 在此处包含其他头文件 // 在此处包含其他头文件
// //
// //
// 在此处定义数据结构 // 在此处定义数据结构
// //
#define MAX_STR_LENGTH 64 #define MAX_STR_LENGTH 64
struct _Rule; struct _Rule;
typedef struct _RuleSymbol{ typedef struct _RuleSymbol
{
struct _RuleSymbol* pNextSymbol; // 指向下一个 Symbol struct _RuleSymbol* pNextSymbol; // 指向下一个 Symbol
struct _RuleSymbol* pOther; // 指向下一个 Select struct _RuleSymbol* pOther; // 指向下一个 Select
int isToken; // 是否为终结符。1 表示终结符,0 表示非终结符 int isToken; // 是否为终结符。1 表示终结符,0 表示非终结符
...@@ -31,7 +27,8 @@ typedef struct _RuleSymbol{ ...@@ -31,7 +27,8 @@ typedef struct _RuleSymbol{
struct _Rule* pRule; // 指向 Symbol 对应的 Rule。isToken 为 0 时这个域有效 struct _Rule* pRule; // 指向 Symbol 对应的 Rule。isToken 为 0 时这个域有效
}RuleSymbol; }RuleSymbol;
typedef struct _Rule{ typedef struct _Rule
{
char RuleName[MAX_STR_LENGTH]; // 文法的名称 char RuleName[MAX_STR_LENGTH]; // 文法的名称
struct _RuleSymbol* pFirstSymbol; // 指向文法的第一个 Select 的第一个 Symbol struct _RuleSymbol* pFirstSymbol; // 指向文法的第一个 Select 的第一个 Symbol
struct _Rule* pNextRule; // 指向下一条文法 struct _Rule* pNextRule; // 指向下一条文法
...@@ -54,7 +51,6 @@ void RemoveLeftRecursion(Rule* pHead); ...@@ -54,7 +51,6 @@ void RemoveLeftRecursion(Rule* pHead);
void PrintRule(Rule* pHead); void PrintRule(Rule* pHead);
// //
// 在此声明全局变量 // 在此声明全局变量
// //
...@@ -62,6 +58,5 @@ void PrintRule(Rule* pHead); ...@@ -62,6 +58,5 @@ void PrintRule(Rule* pHead);
extern const char* VoidSymbol; extern const char* VoidSymbol;
extern const char* Postfix; extern const char* Postfix;
#endif /* REMOVELEFTRECURSIONNOREPLACE_H_ */ #endif /* REMOVELEFTRECURSIONNOREPLACE_H_ */
...@@ -99,12 +99,14 @@ void RemoveLeftRecursion(Rule* pHead) ...@@ -99,12 +99,14 @@ void RemoveLeftRecursion(Rule* pHead)
返回值: 返回值:
Rule 指针 Rule 指针
*/ */
typedef struct _SYMBOL{ typedef struct _SYMBOL
{
int isToken; int isToken;
char Name[MAX_STR_LENGTH]; char Name[MAX_STR_LENGTH];
}SYMBOL; }SYMBOL;
typedef struct _RULE_ENTRY{ typedef struct _RULE_ENTRY
{
char RuleName[MAX_STR_LENGTH]; char RuleName[MAX_STR_LENGTH];
SYMBOL Selects[64][64]; SYMBOL Selects[64][64];
}RULE_ENTRY; }RULE_ENTRY;
...@@ -112,7 +114,8 @@ typedef struct _RULE_ENTRY{ ...@@ -112,7 +114,8 @@ typedef struct _RULE_ENTRY{
static const RULE_ENTRY rule_table[] = static const RULE_ENTRY rule_table[] =
{ {
/* A -> Aa | bA | c | Ad */ /* A -> Aa | bA | c | Ad */
{ "A", { { "A",
{
{ { 0, "A" }, { 1, "a" } }, { { 0, "A" }, { 1, "a" } },
{ { 1, "b" }, { 0, "A" } }, { { 1, "b" }, { 0, "A" } },
{ { 1, "c" } }, { { 1, "c" } },
...@@ -136,33 +139,33 @@ Rule* InitRules() ...@@ -136,33 +139,33 @@ Rule* InitRules()
int i, j, k; int i, j, k;
Rule** pRulePtr = &pHead; Rule** pRulePtr = &pHead;
for(i=0; i<nRuleCount; i++) for (i=0; i<nRuleCount; i++)
{ {
*pRulePtr = CreateRule(rule_table[i].RuleName); *pRulePtr = CreateRule(rule_table[i].RuleName);
pRulePtr = &(*pRulePtr)->pNextRule; pRulePtr = &(*pRulePtr)->pNextRule;
} }
pRule = pHead; pRule = pHead;
for(i=0; i<nRuleCount; i++) for (i=0; i<nRuleCount; i++)
{ {
pSymbolPtr1 = &pRule->pFirstSymbol; 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; 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]; const SYMBOL* pSymbol = &rule_table[i].Selects[j][k];
*pSymbolPtr2 = CreateSymbol(); *pSymbolPtr2 = CreateSymbol();
(*pSymbolPtr2)->isToken = pSymbol->isToken; (*pSymbolPtr2)->isToken = pSymbol->isToken;
if(1 == pSymbol->isToken) if (1 == pSymbol->isToken)
{ {
strcpy((*pSymbolPtr2)->TokenName, pSymbol->Name); strcpy((*pSymbolPtr2)->TokenName, pSymbol->Name);
} }
else else
{ {
(*pSymbolPtr2)->pRule = FindRule(pHead, pSymbol->Name); (*pSymbolPtr2)->pRule = FindRule(pHead, pSymbol->Name);
if(NULL == (*pSymbolPtr2)->pRule) if (NULL == (*pSymbolPtr2)->pRule)
{ {
printf("Init rules error, miss rule \"%s\"\n", pSymbol->Name); printf("Init rules error, miss rule \"%s\"\n", pSymbol->Name);
exit(1); exit(1);
...@@ -193,18 +196,18 @@ Rule* InitRules_CI() ...@@ -193,18 +196,18 @@ Rule* InitRules_CI()
/* A -> Aa | bA | c | Ad */ /* A -> Aa | bA | c | Ad */
int nRuleCount = 0; int nRuleCount = 0;
for(int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
gets(rule_table_ci[i]); gets(rule_table_ci[i]);
int length = strlen(rule_table_ci[i]); int length = strlen(rule_table_ci[i]);
if(length == 0) if (length == 0)
{ {
break; 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'; ruleNameArr[i][j] = '\0';
break; break;
...@@ -221,21 +224,21 @@ Rule* InitRules_CI() ...@@ -221,21 +224,21 @@ Rule* InitRules_CI()
int i, j, k; int i, j, k;
Rule** pRulePtr = &pHead; Rule** pRulePtr = &pHead;
for(i=0; i<nRuleCount; i++) for (i=0; i<nRuleCount; i++)
{ {
*pRulePtr = CreateRule(ruleNameArr[i]); *pRulePtr = CreateRule(ruleNameArr[i]);
pRulePtr = &(*pRulePtr)->pNextRule; pRulePtr = &(*pRulePtr)->pNextRule;
} }
pRule = pHead; pRule = pHead;
for(i=0; i<nRuleCount; i++) for (i=0; i<nRuleCount; i++)
{ {
pSymbolPtr1 = &pRule->pFirstSymbol; pSymbolPtr1 = &pRule->pFirstSymbol;
int start = 0; 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 + 1] == '-'
&& rule_table_ci[i][j + 2] == '>' && rule_table_ci[i][j + 2] == '>'
&& rule_table_ci[i][j + 3] == ' ') && rule_table_ci[i][j + 3] == ' ')
...@@ -245,19 +248,19 @@ Rule* InitRules_CI() ...@@ -245,19 +248,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; pSymbolPtr1 = &(*pSymbolPtr1)->pOther;
pSymbolPtr2 = pSymbolPtr1; pSymbolPtr2 = pSymbolPtr1;
continue; continue;
} }
if(rule_table_ci[i][k] == ' ') if (rule_table_ci[i][k] == ' ')
{ {
continue; continue;
} }
if(k == start) if (k == start)
{ {
pSymbolPtr2 = pSymbolPtr1; pSymbolPtr2 = pSymbolPtr1;
} }
...@@ -268,20 +271,20 @@ Rule* InitRules_CI() ...@@ -268,20 +271,20 @@ Rule* InitRules_CI()
tokenName[0] = rule_table_ci[i][k]; tokenName[0] = rule_table_ci[i][k];
tokenName[1] = '\0'; tokenName[1] = '\0';
(*pSymbolPtr2)->isToken = 1; (*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)->isToken = 0;
(*pSymbolPtr2)->pRule = FindRule(pHead, tokenName); (*pSymbolPtr2)->pRule = FindRule(pHead, tokenName);
if(NULL == (*pSymbolPtr2)->pRule) if (NULL == (*pSymbolPtr2)->pRule)
{ {
printf("Init rules error, miss rule \"%s\"\n", tokenName); printf("Init rules error, miss rule \"%s\"\n", tokenName);
exit(1); exit(1);
} }
} }
} }
if((*pSymbolPtr2)->isToken == 1) if ((*pSymbolPtr2)->isToken == 1)
{ {
strcpy((*pSymbolPtr2)->TokenName, tokenName); strcpy((*pSymbolPtr2)->TokenName, tokenName);
} }
...@@ -352,9 +355,9 @@ RuleSymbol* CreateSymbol() ...@@ -352,9 +355,9 @@ RuleSymbol* CreateSymbol()
Rule* FindRule(Rule* pHead, const char* RuleName) Rule* FindRule(Rule* pHead, const char* RuleName)
{ {
Rule* pRule; 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; break;
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论