Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
L
Lab08
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
CP Lab Group
实验项目模板
Lab08
提交
f15493f6
提交
f15493f6
8月 09, 2019
创建
作者:
宋海霞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify
上级
5b9e6f41
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
35 行增加
和
38 行删除
+35
-38
Follow.h
Follow.h
+8
-11
main.c
main.c
+27
-27
没有找到文件。
Follow.h
浏览文件 @
f15493f6
#ifndef FOLLOW_H_
#ifndef FOLLOW_H_
#define FOLLOW_H_
#define FOLLOW_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
int
isToken
;
// 是否为终结符。1 表示终结符,0 表示非终结符
int
isToken
;
// 是否为终结符。1 表示终结符,0 表示非终结符
char
SymbolName
[
MAX_STR_LENGTH
];
// 终结符和非终结符的名称。
char
SymbolName
[
MAX_STR_LENGTH
];
// 终结符和非终结符的名称。
}
RuleSymbol
;
}
RuleSymbol
;
typedef
struct
_Rule
{
typedef
struct
_Rule
{
char
RuleName
[
MAX_STR_LENGTH
];
// 文法的名称
char
RuleName
[
MAX_STR_LENGTH
];
// 文法的名称
struct
_RuleSymbol
*
pFirstSymbol
;
// 指向文法的第一个 Symbol
struct
_RuleSymbol
*
pFirstSymbol
;
// 指向文法的第一个 Symbol
struct
_Rule
*
pNextRule
;
// 指向下一条文法
struct
_Rule
*
pNextRule
;
// 指向下一条文法
}
Rule
;
}
Rule
;
typedef
struct
_Set
{
typedef
struct
_Set
{
char
Name
[
MAX_STR_LENGTH
];
// 集合的名称
char
Name
[
MAX_STR_LENGTH
];
// 集合的名称
char
Terminal
[
32
][
MAX_STR_LENGTH
];
// 终结符数组
char
Terminal
[
32
][
MAX_STR_LENGTH
];
// 终结符数组
int
nTerminalCount
;
// 数组元素个数
int
nTerminalCount
;
// 数组元素个数
}
Set
;
}
Set
;
typedef
struct
_SetList
{
typedef
struct
_SetList
{
Set
Sets
[
32
];
// 集合数组
Set
Sets
[
32
];
// 集合数组
int
nSetCount
;
// 数组元素个数
int
nSetCount
;
// 数组元素个数
}
SetList
;
}
SetList
;
//
//
// 在此处声明函数
// 在此处声明函数
//
//
...
@@ -68,7 +66,6 @@ void PrintRule(const Rule* pHead);
...
@@ -68,7 +66,6 @@ void PrintRule(const Rule* pHead);
void
PrintFollowSet
(
SetList
*
pFollowSetList
);
void
PrintFollowSet
(
SetList
*
pFollowSetList
);
//
//
// 在此声明全局变量
// 在此声明全局变量
//
//
...
...
main.c
浏览文件 @
f15493f6
...
@@ -186,12 +186,14 @@ void Follow(const Rule* pHead, SetList* pFollowSetList, SetList* pFirstSetList)
...
@@ -186,12 +186,14 @@ void Follow(const Rule* pHead, SetList* pFollowSetList, SetList* pFirstSetList)
}
}
typedef
struct
_SYMBOL
{
typedef
struct
_SYMBOL
{
int
isToken
;
int
isToken
;
char
SymbolName
[
MAX_STR_LENGTH
];
char
SymbolName
[
MAX_STR_LENGTH
];
}
SYMBOL
;
}
SYMBOL
;
typedef
struct
_RULE_ENTRY
{
typedef
struct
_RULE_ENTRY
{
char
RuleName
[
MAX_STR_LENGTH
];
char
RuleName
[
MAX_STR_LENGTH
];
SYMBOL
Symbols
[
64
];
SYMBOL
Symbols
[
64
];
}
RULE_ENTRY
;
}
RULE_ENTRY
;
...
@@ -233,17 +235,17 @@ Rule* InitRules()
...
@@ -233,17 +235,17 @@ Rule* InitRules()
int
i
,
j
;
int
i
,
j
;
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
++
)
{
{
pSymbolPtr
=
&
pRule
->
pFirstSymbol
;
pSymbolPtr
=
&
pRule
->
pFirstSymbol
;
for
(
j
=
0
;
rule_table
[
i
].
Symbols
[
j
].
SymbolName
[
0
]
!=
'\0'
;
j
++
)
for
(
j
=
0
;
rule_table
[
i
].
Symbols
[
j
].
SymbolName
[
0
]
!=
'\0'
;
j
++
)
{
{
const
SYMBOL
*
pSymbol
=
&
rule_table
[
i
].
Symbols
[
j
];
const
SYMBOL
*
pSymbol
=
&
rule_table
[
i
].
Symbols
[
j
];
...
@@ -265,18 +267,18 @@ Rule* InitRules_CI()
...
@@ -265,18 +267,18 @@ Rule* InitRules_CI()
{
{
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
;
...
@@ -290,21 +292,21 @@ Rule* InitRules_CI()
...
@@ -290,21 +292,21 @@ Rule* InitRules_CI()
RuleSymbol
**
pSymbolPtr
,
*
pNewSymbol
;
RuleSymbol
**
pSymbolPtr
,
*
pNewSymbol
;
Rule
**
pRulePtr
=
&
pHead
;
Rule
**
pRulePtr
=
&
pHead
;
for
(
int
i
=
0
;
i
<
nRuleCount
;
i
++
)
for
(
int
i
=
0
;
i
<
nRuleCount
;
i
++
)
{
{
*
pRulePtr
=
CreateRule
(
ruleNameArr
[
i
]);
*
pRulePtr
=
CreateRule
(
ruleNameArr
[
i
]);
pRulePtr
=
&
(
*
pRulePtr
)
->
pNextRule
;
pRulePtr
=
&
(
*
pRulePtr
)
->
pNextRule
;
}
}
pRule
=
pHead
;
pRule
=
pHead
;
for
(
int
i
=
0
;
i
<
nRuleCount
;
i
++
)
for
(
int
i
=
0
;
i
<
nRuleCount
;
i
++
)
{
{
pSymbolPtr
=
&
pRule
->
pFirstSymbol
;
pSymbolPtr
=
&
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
]
==
' '
)
...
@@ -314,9 +316,9 @@ Rule* InitRules_CI()
...
@@ -314,9 +316,9 @@ Rule* InitRules_CI()
}
}
}
}
for
(
int
k
=
start
;
rule_table_ci
[
i
][
k
]
!=
'\0'
;
k
++
)
for
(
int
k
=
start
;
rule_table_ci
[
i
][
k
]
!=
'\0'
;
k
++
)
{
{
if
(
rule_table_ci
[
i
][
k
]
==
' '
)
if
(
rule_table_ci
[
i
][
k
]
==
' '
)
{
{
continue
;
continue
;
}
}
...
@@ -324,9 +326,9 @@ Rule* InitRules_CI()
...
@@ -324,9 +326,9 @@ Rule* InitRules_CI()
pNewSymbol
=
CreateSymbol
();
pNewSymbol
=
CreateSymbol
();
char
tokenName
[
MAX_STR_LENGTH
]
=
{};
char
tokenName
[
MAX_STR_LENGTH
]
=
{};
for
(
int
m
=
0
;
;
m
++
)
for
(
int
m
=
0
;
;
m
++
)
{
{
if
(
rule_table_ci
[
i
][
k
]
==
' '
||
rule_table_ci
[
i
][
k
]
==
'\0'
||
rule_table_ci
[
i
][
k
]
==
'\n'
)
if
(
rule_table_ci
[
i
][
k
]
==
' '
||
rule_table_ci
[
i
][
k
]
==
'\0'
||
rule_table_ci
[
i
][
k
]
==
'\n'
)
{
{
tokenName
[
m
]
=
'\0'
;
tokenName
[
m
]
=
'\0'
;
break
;
break
;
...
@@ -339,9 +341,9 @@ Rule* InitRules_CI()
...
@@ -339,9 +341,9 @@ Rule* InitRules_CI()
strcpy
(
pNewSymbol
->
SymbolName
,
tokenName
);
strcpy
(
pNewSymbol
->
SymbolName
,
tokenName
);
pNewSymbol
->
isToken
=
1
;
pNewSymbol
->
isToken
=
1
;
for
(
int
n
=
0
;
n
<
nRuleCount
;
n
++
)
for
(
int
n
=
0
;
n
<
nRuleCount
;
n
++
)
{
{
if
(
strcmp
(
pNewSymbol
->
SymbolName
,
ruleNameArr
[
n
])
==
0
)
if
(
strcmp
(
pNewSymbol
->
SymbolName
,
ruleNameArr
[
n
])
==
0
)
{
{
pNewSymbol
->
isToken
=
0
;
pNewSymbol
->
isToken
=
0
;
break
;
break
;
...
@@ -409,12 +411,13 @@ RuleSymbol* CreateSymbol()
...
@@ -409,12 +411,13 @@ RuleSymbol* CreateSymbol()
void
PrintRule
(
const
Rule
*
pHead
)
void
PrintRule
(
const
Rule
*
pHead
)
{
{
const
Rule
*
pRule
;
const
Rule
*
pRule
;
for
(
pRule
=
pHead
;
pRule
!=
NULL
;
pRule
=
pRule
->
pNextRule
)
for
(
pRule
=
pHead
;
pRule
!=
NULL
;
pRule
=
pRule
->
pNextRule
)
{
{
printf
(
"%s ->"
,
pRule
->
RuleName
);
printf
(
"%s ->"
,
pRule
->
RuleName
);
RuleSymbol
*
pRuleSymbol
;
RuleSymbol
*
pRuleSymbol
;
for
(
pRuleSymbol
=
pRule
->
pFirstSymbol
;
pRuleSymbol
!=
NULL
;
pRuleSymbol
=
pRuleSymbol
->
pNextSymbol
)
for
(
pRuleSymbol
=
pRule
->
pFirstSymbol
;
pRuleSymbol
!=
NULL
;
pRuleSymbol
=
pRuleSymbol
->
pNextSymbol
)
{
{
printf
(
" %s"
,
pRuleSymbol
->
SymbolName
);
printf
(
" %s"
,
pRuleSymbol
->
SymbolName
);
}
}
...
@@ -432,12 +435,12 @@ void PrintRule(const Rule* pHead)
...
@@ -432,12 +435,12 @@ void PrintRule(const Rule* pHead)
void
PrintFollowSet
(
SetList
*
pFollowSetList
)
void
PrintFollowSet
(
SetList
*
pFollowSetList
)
{
{
printf
(
"
\n
The Follow Set:
\n
"
);
printf
(
"
\n
The Follow Set:
\n
"
);
for
(
int
i
=
0
;
i
<
pFollowSetList
->
nSetCount
;
i
++
)
for
(
int
i
=
0
;
i
<
pFollowSetList
->
nSetCount
;
i
++
)
{
{
printf
(
"Follow(%s) = { "
,
pFollowSetList
->
Sets
[
i
].
Name
);
printf
(
"Follow(%s) = { "
,
pFollowSetList
->
Sets
[
i
].
Name
);
for
(
int
j
=
0
;
j
<
pFollowSetList
->
Sets
[
i
].
nTerminalCount
;
j
++
)
for
(
int
j
=
0
;
j
<
pFollowSetList
->
Sets
[
i
].
nTerminalCount
;
j
++
)
{
{
if
(
j
==
pFollowSetList
->
Sets
[
i
].
nTerminalCount
-
1
)
if
(
j
==
pFollowSetList
->
Sets
[
i
].
nTerminalCount
-
1
)
{
{
printf
(
"%s "
,
pFollowSetList
->
Sets
[
i
].
Terminal
[
j
]);
printf
(
"%s "
,
pFollowSetList
->
Sets
[
i
].
Terminal
[
j
]);
}
}
...
@@ -450,6 +453,3 @@ void PrintFollowSet(SetList* pFollowSetList)
...
@@ -450,6 +453,3 @@ void PrintFollowSet(SetList* pFollowSetList)
printf
(
"}
\n
"
);
printf
(
"}
\n
"
);
}
}
}
}
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论