形式语言与自动机格雷巴赫范式转换
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Chomsky Normal Form
30
Step 3 Modify the rules so that the nonterminals are in ascending order. By “ascending order” we mean:
if Ai → AjX is a rule, then i < j
hAing-1he→sta-rXuYle.:NAon-w1
→ AnY. Substitute An with its rhs: that rule is in Greibach Normal Form.
Continue down the rules, doing the substitution.
rhs = right-hand side
9
对比不使用格雷巴赫范式表示的文法
不使用格雷巴赫范式表示的文法
S → aA A →B B→C C→a
10
格雷巴赫范式的本质是一种前缀表示法
使用格雷巴赫范式表示的文法
Expr → +AB A →5 B → *CD C→2 D →3
11
使用格雷巴赫范式的优点 ❖ 字符串可被快速解析 ❖ 表达式可被有效评估.
4
什么是格雷巴赫范式?
A context-free grammar is in Greibach Normal Form if the right-hand side of each rule has one terminal followed by zero or more non-terminals:
How to Convert a Context-Free Grammar to Greibach Normal Form
如何将上下文无关文法转换为格雷巴 赫范式
目标 1. 什么是格雷巴赫范式?
2
目标 1. 什么是格雷巴赫范式? 2. 将文法转换为格雷巴赫范式的优点是什么?
3
目标 1. 什么是格雷巴赫范式? 2. 将文法转换为格雷巴赫范式的优点是什么? 3. 怎样将上下文无关文法转换为格雷巴赫范式?
❖ Replace the rule Ak → AjX by these rules: Ak → YiX i = 0..m
❖Each Yi begins with either a terminal symbol or some Am where j < m.
❖Recursively repeat the substitution for each Yi that
18
左递归规则
❖算法要求文法不具有left-recursive rules ❖This is a left-recursive rule: Ak → Akα
|β ❖The alternative (β) allows a
derivation to “break out of” the recursion. ❖Every left-recursive rule must have an alternative (β) that allows breaking out of the recursion.
❖故Ak 可能产生: ▪ β1 followed by a string X: β1X X={α1,…,αr}+ … ▪ βs followed by a string X: βsα X={α1,…,αr}+
23
Rule to generate {α1,…, αr}+
We need rules to generate a string X:
A → aX
zero or more non-terminal symbols one terminal symbol
5
格雷巴赫范式示例
S → aB | bA A → a | aS | bAA B → b | bS | aBB
Every right-hand side consists of exactly one terminal followed by zero or more non-terminals.
27
Apply the Algorithm to this Grammar 将下述文法转换为格雷巴赫范式:
S → Ab A → aS A→a
First, what language does G generate? Answer: L(G) = anbn
28
Step 1
Change the names of the non-terminal symbols to Ai
❖We want to put this rule Ak → AjX into ascending order.
❖We must deal with two cases: 1. k > j 2. k = j (left-recursive rule)
33
Case 1: k > j
Ak → AjX
Replace this with the rhs of the rule(s) for Aj. If the resulting rule(s) is still not in ascending order, continue replacing until it is in ascending order.
Greibach Normal Form
15
算法 ❖ 我们已经看到了两个简单的语法转换为格雷巴赫
范式的例子。 ❖ 他们并未展示格雷巴赫范式转换的系统方法。 ❖ 下面的ppt展示了进行转换的系统方法(即算
法)。
16
But first … 在我们介绍算法之前,需要理解两个概念::
1. 乔姆斯基范式 2. 左递归规则
begins with Am and k > m.
36
Avoid getting into a loop!
… A7 → A7U A7 → A8X A8 → A15Y A9 → A7Z
34
Example
These rules are in ascending order
But this is not in ascending order
…
A7 → A10U A7 → A8X A8 → A15Y A9 → A7Z
Replace this with the rhs of A7
… A7 → A10U A7 → A8X A8 → A15Y A9 → A10UZ A9 → A8XZ
Ak → βAn+1 An+1 → αAn+1 | α ❖ 通过上述两个产生式便可消除左递归。
22
多重左递归替代
❖Ak 可能有多重左递归 Ak → Akα1 | Akα2 | … | Akαr
❖Ak may have multiple other alternatives: Ak → β1 | β2 | … | βs
rule: An+1 → αAn+1 | α
(Assume the grammar has “n” rules. So An+1 is a new rule that we just created.)
21
如何消除左递归 ❖语言βαn 可由如下的左递归规则产生:
Ak → Akα | β ❖ 该语言也可由如下的语法产生:
❖Replace this left-recursive rule:
Ak → Akα1 | Akα2 | … | Akαr | β1 | β2 | … | βs
❖ By these rules:
Ak → βi An+1 An+1 → αi An+1 → αi An+1
i = 1..s i = 1..r
S → Ab A → aS A→a
Change S to A1, A to A2
A1 → A2b A2 → aA1 A2 → a
29
Step 2
Convert the grammar to Chomsky Normal Form.
A1 → A2b A2 → aA1 A2 → a
A1 → A2A3 A2 → A4 A1 A2 → a A3 → b A4 → a
17
乔姆斯基范式
❖ 转换为格雷巴赫算法,首先要求将文法转换为乔 姆斯基范式。
❖A context-free grammar is in Chomsky Normal Form if each rule has one of these forms:
1. X → a 2. X → YZ
❖That is, the right-hand side is either a single terminal or two non-terminals.
▪ Convert the rules to ascending order: each rule is then of the
form: Ai → Ai+mX
▪ After ordering, the highest rule will be in Greibach Normal
Form: An → aX. The next-to-highest rule will depend on the
i = 1..r
26
转换为格雷巴赫范式的算法
❖ 首先, 将一个文法转换为乔姆斯基范式. 则所有的产生式的 格式符合:
1. Ai → a, 其中a是一个终止符 2. Ai → AjAk
1符合格雷巴赫范式,2不符合格雷巴赫范式
❖ 然后, order the rules followed by substitution:
An+1 → αi An+1 → αi An+1
i = 1..r i = 1..r
24
Rule to generate {α1,…, αr}+ And we need a rule to generate β1An+1,…,βsAn+1 :
Ak → βi An+1 i = 1..s
25
Beautiful definition of how to eliminate left-recursion
13
文法及等价的格雷巴赫范式
S → aBc B→b
convert
Not Greibach Normal Form
S → aBC B→b C→c
Greibach Normal Form
14
文法及等价的格雷巴赫范式
S → Bc B→b
convert
S → bC C→c
Not Greibach Normal Form
Now replace this with the rhs of A8. Repeat until A9 is in ascending order.
35
Beautiful definition of how to modify Ak → AjX, k > j
❖Let Aj be this rule: Aj → Y1 | … | Ym
19
消除左递归的算法 ❖Ak → Akα | β ❖产生的语言: βαn, n≥1
20
消除左递归
❖We want to eliminate the leftrecursion in this rule: Ak → Akα | β
❖And we know the rule produces βαn ❖We can easily generate αn with this
31
kth rule not in ascending order Suppose the first k-1 rules are in ascending order but the kth rule is not. Thus, Ak → AjX is a rule and k ≥ j.
32
2 cases
12
每个文法都可找到等价的格雷巴赫范式
To every ε-free context-free grammar we can find an equivalent grammar in Greibach normal form. 对于每一个不包含ε的上下文无关文法,都可找到等 价的格雷巴赫范式表示。
6
非格雷rminal at end is not allowed
Not in Greibach Normal Form
7
使用格雷巴赫范式的优点? •
8
推导示例
使用格雷巴赫范式的文法
S → aB | bA A → a | aS | bAA B → b | bS | aBB
30
Step 3 Modify the rules so that the nonterminals are in ascending order. By “ascending order” we mean:
if Ai → AjX is a rule, then i < j
hAing-1he→sta-rXuYle.:NAon-w1
→ AnY. Substitute An with its rhs: that rule is in Greibach Normal Form.
Continue down the rules, doing the substitution.
rhs = right-hand side
9
对比不使用格雷巴赫范式表示的文法
不使用格雷巴赫范式表示的文法
S → aA A →B B→C C→a
10
格雷巴赫范式的本质是一种前缀表示法
使用格雷巴赫范式表示的文法
Expr → +AB A →5 B → *CD C→2 D →3
11
使用格雷巴赫范式的优点 ❖ 字符串可被快速解析 ❖ 表达式可被有效评估.
4
什么是格雷巴赫范式?
A context-free grammar is in Greibach Normal Form if the right-hand side of each rule has one terminal followed by zero or more non-terminals:
How to Convert a Context-Free Grammar to Greibach Normal Form
如何将上下文无关文法转换为格雷巴 赫范式
目标 1. 什么是格雷巴赫范式?
2
目标 1. 什么是格雷巴赫范式? 2. 将文法转换为格雷巴赫范式的优点是什么?
3
目标 1. 什么是格雷巴赫范式? 2. 将文法转换为格雷巴赫范式的优点是什么? 3. 怎样将上下文无关文法转换为格雷巴赫范式?
❖ Replace the rule Ak → AjX by these rules: Ak → YiX i = 0..m
❖Each Yi begins with either a terminal symbol or some Am where j < m.
❖Recursively repeat the substitution for each Yi that
18
左递归规则
❖算法要求文法不具有left-recursive rules ❖This is a left-recursive rule: Ak → Akα
|β ❖The alternative (β) allows a
derivation to “break out of” the recursion. ❖Every left-recursive rule must have an alternative (β) that allows breaking out of the recursion.
❖故Ak 可能产生: ▪ β1 followed by a string X: β1X X={α1,…,αr}+ … ▪ βs followed by a string X: βsα X={α1,…,αr}+
23
Rule to generate {α1,…, αr}+
We need rules to generate a string X:
A → aX
zero or more non-terminal symbols one terminal symbol
5
格雷巴赫范式示例
S → aB | bA A → a | aS | bAA B → b | bS | aBB
Every right-hand side consists of exactly one terminal followed by zero or more non-terminals.
27
Apply the Algorithm to this Grammar 将下述文法转换为格雷巴赫范式:
S → Ab A → aS A→a
First, what language does G generate? Answer: L(G) = anbn
28
Step 1
Change the names of the non-terminal symbols to Ai
❖We want to put this rule Ak → AjX into ascending order.
❖We must deal with two cases: 1. k > j 2. k = j (left-recursive rule)
33
Case 1: k > j
Ak → AjX
Replace this with the rhs of the rule(s) for Aj. If the resulting rule(s) is still not in ascending order, continue replacing until it is in ascending order.
Greibach Normal Form
15
算法 ❖ 我们已经看到了两个简单的语法转换为格雷巴赫
范式的例子。 ❖ 他们并未展示格雷巴赫范式转换的系统方法。 ❖ 下面的ppt展示了进行转换的系统方法(即算
法)。
16
But first … 在我们介绍算法之前,需要理解两个概念::
1. 乔姆斯基范式 2. 左递归规则
begins with Am and k > m.
36
Avoid getting into a loop!
… A7 → A7U A7 → A8X A8 → A15Y A9 → A7Z
34
Example
These rules are in ascending order
But this is not in ascending order
…
A7 → A10U A7 → A8X A8 → A15Y A9 → A7Z
Replace this with the rhs of A7
… A7 → A10U A7 → A8X A8 → A15Y A9 → A10UZ A9 → A8XZ
Ak → βAn+1 An+1 → αAn+1 | α ❖ 通过上述两个产生式便可消除左递归。
22
多重左递归替代
❖Ak 可能有多重左递归 Ak → Akα1 | Akα2 | … | Akαr
❖Ak may have multiple other alternatives: Ak → β1 | β2 | … | βs
rule: An+1 → αAn+1 | α
(Assume the grammar has “n” rules. So An+1 is a new rule that we just created.)
21
如何消除左递归 ❖语言βαn 可由如下的左递归规则产生:
Ak → Akα | β ❖ 该语言也可由如下的语法产生:
❖Replace this left-recursive rule:
Ak → Akα1 | Akα2 | … | Akαr | β1 | β2 | … | βs
❖ By these rules:
Ak → βi An+1 An+1 → αi An+1 → αi An+1
i = 1..s i = 1..r
S → Ab A → aS A→a
Change S to A1, A to A2
A1 → A2b A2 → aA1 A2 → a
29
Step 2
Convert the grammar to Chomsky Normal Form.
A1 → A2b A2 → aA1 A2 → a
A1 → A2A3 A2 → A4 A1 A2 → a A3 → b A4 → a
17
乔姆斯基范式
❖ 转换为格雷巴赫算法,首先要求将文法转换为乔 姆斯基范式。
❖A context-free grammar is in Chomsky Normal Form if each rule has one of these forms:
1. X → a 2. X → YZ
❖That is, the right-hand side is either a single terminal or two non-terminals.
▪ Convert the rules to ascending order: each rule is then of the
form: Ai → Ai+mX
▪ After ordering, the highest rule will be in Greibach Normal
Form: An → aX. The next-to-highest rule will depend on the
i = 1..r
26
转换为格雷巴赫范式的算法
❖ 首先, 将一个文法转换为乔姆斯基范式. 则所有的产生式的 格式符合:
1. Ai → a, 其中a是一个终止符 2. Ai → AjAk
1符合格雷巴赫范式,2不符合格雷巴赫范式
❖ 然后, order the rules followed by substitution:
An+1 → αi An+1 → αi An+1
i = 1..r i = 1..r
24
Rule to generate {α1,…, αr}+ And we need a rule to generate β1An+1,…,βsAn+1 :
Ak → βi An+1 i = 1..s
25
Beautiful definition of how to eliminate left-recursion
13
文法及等价的格雷巴赫范式
S → aBc B→b
convert
Not Greibach Normal Form
S → aBC B→b C→c
Greibach Normal Form
14
文法及等价的格雷巴赫范式
S → Bc B→b
convert
S → bC C→c
Not Greibach Normal Form
Now replace this with the rhs of A8. Repeat until A9 is in ascending order.
35
Beautiful definition of how to modify Ak → AjX, k > j
❖Let Aj be this rule: Aj → Y1 | … | Ym
19
消除左递归的算法 ❖Ak → Akα | β ❖产生的语言: βαn, n≥1
20
消除左递归
❖We want to eliminate the leftrecursion in this rule: Ak → Akα | β
❖And we know the rule produces βαn ❖We can easily generate αn with this
31
kth rule not in ascending order Suppose the first k-1 rules are in ascending order but the kth rule is not. Thus, Ak → AjX is a rule and k ≥ j.
32
2 cases
12
每个文法都可找到等价的格雷巴赫范式
To every ε-free context-free grammar we can find an equivalent grammar in Greibach normal form. 对于每一个不包含ε的上下文无关文法,都可找到等 价的格雷巴赫范式表示。
6
非格雷rminal at end is not allowed
Not in Greibach Normal Form
7
使用格雷巴赫范式的优点? •
8
推导示例
使用格雷巴赫范式的文法
S → aB | bA A → a | aS | bAA B → b | bS | aBB