Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab07
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
CP Lab Group
实验项目模板
Lab07
提交
87393e2c
提交
87393e2c
12月 18, 2018
创建
作者:
赵鹏翀
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
init template
上级
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
387 行增加
和
0 行删除
+387
-0
.gitignore
.gitignore
+2
-0
First.cplproj
First.cplproj
+37
-0
First.h
First.h
+74
-0
First.puo
First.puo
+0
-0
main.c
main.c
+274
-0
没有找到文件。
.gitignore
0 → 100644
浏览文件 @
87393e2c
/Debug
/Release
First.cplproj
0 → 100644
浏览文件 @
87393e2c
<?xml version="1.0" encoding="gb2312"?>
<OSLProject
Version=
"1.00"
Name=
"First"
SubjectID=
"11c951f4-9b13-40e1-8b73-39ba7d73b89b"
ProjectTemplateID=
"93a41859-86a9-430d-82b4-8b3f7ee052c2"
>
<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=
""$(CPLInstallDir)Dump\lib\First_Demo.o""
/>
<Tool
Name=
"PostBuildEventTool"
/>
<VisualContext>
<WatchPoints>
<WatchPoint
FunctionName=
"First"
ObserverID=
"05F99628-8A10-4609-AEB4-9973FDB4ABF1"
>
</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=
".\First.h"
>
</File>
</Filter>
<Filter
Name=
"Դļ"
Filter=
"cpp;c;cc;cxx"
>
<File
RelativePath=
".\main.c"
>
</File>
</Filter>
</Files>
</OSLProject>
First.h
0 → 100644
浏览文件 @
87393e2c
#ifndef _FIRST_H_
#define _FIRST_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
);
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
;
#endif
/* _FIRST_H_ */
First.puo
0 → 100644
浏览文件 @
87393e2c
添加文件
main.c
0 → 100644
浏览文件 @
87393e2c
#include "First.h"
const
char
*
VoidSymbol
=
"$"
;
// "ε"
int
main
(
int
argc
,
char
*
argv
[])
{
//
// 调用 InitRules 函数初始化文法
//
Rule
*
pHead
=
InitRules
();
//
// 输出文法
//
PrintRule
(
pHead
);
//
// 调用 First 函数求文法的 First 集合
//
SetList
FirstSet
;
FirstSet
.
nSetCount
=
0
;
First
(
pHead
,
&
FirstSet
);
return
0
;
}
/*
功能:
添加一个 Set 到 SetList。
参数:
pSetList -- SetList 指针。
pName -- 集合名称字符串指针。
*/
void
AddOneSet
(
SetList
*
pSetList
,
const
char
*
pName
)
{
//
// TODO: 在此添加代码
//
}
/*
功能:
根据名称在 SetList 中查找 Set。
参数:
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: 在此添加代码
//
}
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
)
{
//
// TODO: 在此添加代码
//
}
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论