提交 4a7e5e28 创建 作者: 赵鹏翀's avatar 赵鹏翀

init template

上级
/Debug
/Release
<?xml version="1.0" encoding="gb2312"?>
<OSLProject Version="1.00" Name="Follow" SubjectID="11c951f4-9b13-40e1-8b73-39ba7d73b89b" ProjectTemplateID="8ef10a3c-678d-458d-b801-d9c2ed7a4b25">
<Configurations>
<Configuration Name="Debug">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="_DEBUG" GenerateDebugInformation="-1" AdditionalOptions=""/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool" AdditionalDependencies="&quot;$(CPLInstallDir)Dump\lib\Follow_Demo.o&quot;"/>
<Tool Name="PostBuildEventTool"/>
<VisualContext>
<WatchPoints>
<WatchPoint FunctionName="Follow" ObserverID="EFE8FF23-1ED3-490d-82B6-6BE404A1D4D9">
</WatchPoint>
</WatchPoints>
</VisualContext>
</Configuration>
<Configuration Name="Release">
<Tool Name="PreBuildEventTool"/>
<Tool Name="CustomBuildTool"/>
<Tool Name="GCCCompilerTool" PreprocessorDefinitions="NDEBUG"/>
<Tool Name="PreLinkEventTool"/>
<Tool Name="GCCLinkerTool"/>
<Tool Name="PostBuildEventTool"/>
</Configuration>
</Configurations>
<Files>
<Filter Name="ͷļ" Filter="h;hpp;hxx">
<File RelativePath=".\Follow.h">
</File>
</Filter>
<Filter Name="Դļ" Filter="cpp;c;cc;cxx">
<File RelativePath=".\main.c">
</File>
</Filter>
</Files>
</OSLProject>
#ifndef _FOLLOW_H_
#define _FOLLOW_H_
//
// 在此处包含 C 标准库头文件
//
#include <stdio.h>
//
// 在此处包含其他头文件
//
//
// 在此处定义数据结构
//
#define MAX_STR_LENGTH 64
struct _Rule;
typedef struct _RuleSymbol{
struct _RuleSymbol* pNextSymbol; // 指向下一个 Symbol
int isToken; // 是否为终结符。1 表示终结符,0 表示非终结符
char SymbolName[MAX_STR_LENGTH]; // 终结符和非终结符的名称。
}RuleSymbol;
typedef struct _Rule{
char RuleName[MAX_STR_LENGTH]; // 文法的名称
struct _RuleSymbol* pFirstSymbol;// 指向文法的第一个 Symbol
struct _Rule* pNextRule; // 指向下一条文法
}Rule;
typedef struct _Set{
char Name[MAX_STR_LENGTH]; // 集合的名称
char Terminal[32][MAX_STR_LENGTH];// 终结符数组
int nTerminalCount; // 数组元素个数
}Set;
typedef struct _SetList{
Set Sets[32]; // 集合数组
int nSetCount; // 数组元素个数
}SetList;
//
// 在此处声明函数
//
void First(const Rule* pHead, SetList* pFirstSetList);
void Follow(const Rule* pHead, SetList* pFollowSetList, SetList* pFirstSetList);
Set* GetSet(SetList* pSetList, const char* pName);
void AddOneSet(SetList* pSetList, const char* pName);
int AddTerminalToSet(Set* pSet, const char* pTerminal);
int AddSetToSet(Set* pDesSet, const Set* pSrcSet);
int SetHasVoid(const Set* pSet);
Rule* InitRules();
Rule* CreateRule(const char* pRuleName);
RuleSymbol* CreateSymbol();
void PrintRule(const Rule* pHead);
//
// 在此声明全局变量
//
extern const char* VoidSymbol;
const char* DollarSymbol;
#endif /* _FOLLOW_H_ */
添加文件
#include "Follow.h"
const char* VoidSymbol = "#"; // "ε"
const char* DollarSymbol = "$";
int main(int argc, char* argv[])
{
//
// 调用 InitRules 函数初始化文法
//
Rule* pHead = InitRules();
//
// 初始化 First 集合、Follow 集合
//
SetList FirstSetList, FollowSetList;
FirstSetList.nSetCount = 0;
FollowSetList.nSetCount = 0;
//
// 调用 Follow 函数求文法的 First 集合、Follow 集合
//
Follow(pHead, &FollowSetList, &FirstSetList);
//
// 输出文法
//
PrintRule(pHead);
return 0;
}
/*
功能:
添加一个 Set 到 SetList。
参数:
pSetList -- SetList 指针。
pName -- 集合名称字符串指针。
*/
void AddOneSet(SetList* pSetList, const char* pName)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
根据名称在 SetList 中查找。
参数:
pSetList -- SetList 指针。
pName -- 集合名称字符串指针。
返回值:
如果找到返回 Set 指针,否则返回 NULL。
*/
Set* GetSet(SetList* pSetList, const char* pName)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
添加一个终结符到 Set。
参数:
pSet -- Set 指针。
pTerminal -- 终结符名称指针。
返回值:
添加成功返回 1,否则返回 0。
*/
int AddTerminalToSet(Set* pSet, const char* pTerminal)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
将源 Set 添加到目标 Set 中,忽略ε。
参数:
pDesSet -- 目标 Set 指针。
pSrcSet -- 源 Set 指针。
返回值:
添加成功返回 1,否则返回 0。
*/
int AddSetToSet(Set* pDesSet, const Set* pSrcSet)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
判断 Set 中是否含有ε。
参数:
pSet -- Set 指针。
返回值:
存在返回 1。
不存在返回 0。
*/
int SetHasVoid(const Set* pSet)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
求文法的 First 集合。
参数:
pHead -- 文法的头指针。
pFirstSetList -- First 集合指针。
*/
void First(const Rule* pHead, SetList* pFirstSetList)
{
const Rule* pRule; // Rule 指针
int isChange; // 集合是否被修改的标志
RuleSymbol* pSymbol;// Symbol 游标
//
// TODO: 在此添加代码
//
}
/*
功能:
求文法的 Follow 集合。
参数:
pHead -- 文法的头指针。
pFollowSetList -- Follow 集合指针。
pFirstSetList -- First 集合指针。
*/
void Follow(const Rule* pHead, SetList* pFollowSetList, SetList* pFirstSetList)
{
const Rule* pRule; // Rule 指针
int isChange; // 集合是否被修改的标志
RuleSymbol* pSymbol;// Symbol 游标
// 调用 First 函数求文法的 First 集合
First(pHead, pFirstSetList);
//
// TODO: 在此添加代码
//
}
typedef struct _SYMBOL{
int isToken;
char SymbolName[MAX_STR_LENGTH];
}SYMBOL;
typedef struct _RULE_ENTRY{
char RuleName[MAX_STR_LENGTH];
SYMBOL Symbols[64];
}RULE_ENTRY;
static const RULE_ENTRY rule_table[] =
{
/* exp -> exp addop term| term */
{ "exp", { { 0, "exp" }, { 0, "addop"}, { 0, "term"} } },
{ "exp", { { 0, "term" } } },
/* addop -> + | - */
{ "addop", { { 1, "+" } } },
{ "addop", { { 1, "-" } } },
/* term -> term mulop factor | factor */
{ "term", { { 0, "term" }, { 0, "mulop"}, { 0, "factor"} } },
{ "term", { { 0, "factor" } } },
/* mulop -> * */
{ "mulop", { { 1, "*" } } },
/* factor -> (exp) | number */
{ "factor", { { 1, "(" }, { 0, "exp"}, { 1, ")"} } },
{ "factor", { { 1, "number" } } },
};
/*
功能:
初始化文法链表。
返回值:
文法的头指针。
*/
Rule* InitRules()
{
Rule *pHead, *pRule;
RuleSymbol **pSymbolPtr, *pNewSymbol;
int nRuleCount = sizeof(rule_table) / sizeof(rule_table[0]);
int i, j;
Rule** pRulePtr = &pHead;
for(i=0; i<nRuleCount; i++)
{
*pRulePtr = CreateRule(rule_table[i].RuleName);
pRulePtr = &(*pRulePtr)->pNextRule;
}
pRule = pHead;
for(i=0; i<nRuleCount; i++)
{
pSymbolPtr = &pRule->pFirstSymbol;
for(j=0; rule_table[i].Symbols[j].SymbolName[0] != '\0'; j++)
{
const SYMBOL* pSymbol = &rule_table[i].Symbols[j];
pNewSymbol = CreateSymbol();
pNewSymbol->isToken = pSymbol->isToken;
strcpy(pNewSymbol->SymbolName, pSymbol->SymbolName);
*pSymbolPtr = pNewSymbol;
pSymbolPtr = &pNewSymbol->pNextSymbol;
}
pRule = pRule->pNextRule;
}
return pHead;
}
/*
功能:
创建一个新的文法。
参数:
pRuleName -- 文法的名字。
返回值:
文法的指针。
*/
Rule* CreateRule(const char* pRuleName)
{
Rule* pRule = (Rule*)malloc(sizeof(Rule));
strcpy(pRule->RuleName, pRuleName);
pRule->pFirstSymbol = NULL;
pRule->pNextRule = NULL;
return pRule;
}
/*
功能:
创建一个新的符号。
返回值:
符号的指针。
*/
RuleSymbol* CreateSymbol()
{
RuleSymbol* pSymbol = (RuleSymbol*)malloc(sizeof(RuleSymbol));
pSymbol->pNextSymbol = NULL;
pSymbol->isToken = -1;
pSymbol->SymbolName[0] = '\0';
return pSymbol;
}
/*
功能:
输出文法。
参数:
pHead -- 文法的头指针。
*/
void PrintRule(const Rule* pHead)
{
const Rule* pRule;
for(pRule = pHead; pRule != NULL; pRule = pRule->pNextRule)
{
printf("%s ->", pRule->RuleName);
RuleSymbol* pRuleSymbol;
for(pRuleSymbol = pRule->pFirstSymbol; pRuleSymbol != NULL; pRuleSymbol = pRuleSymbol->pNextSymbol)
{
printf(" %s", pRuleSymbol->SymbolName);
}
printf("\n");
}
}
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论