计算理论:上下文无关文法
Context-Free Grammars
Context-Free Grammar
Definition
A context-free grammar
is a finite set, each element is called a nonterminal character or a variable. Each variable defines a sub-language of the language defined by is a finite set of terminals, disjoint from . The set of terminals is the alphabet of the language defined by the grammar is a finite relation from to , where the asterisk represents the Kleene star operation. The members of are called the (rewrite) rules or productions of the grammar. is the start variable (or start symbol), used to present the whole sentence. It must be an element of
A production has the form: variable (head)
Derivation
有两种应用 CFG 的产生式来推断某个特定 string 是否在某个特定 variable 定义的语言之中的方法
recursive inference: 选择 body 中各个 variable 的 language 中的串,将其与 body 中的 terminals 以正确的顺序连接,其结果在 head 中的 variable 定义的语言中。
derivation: 将开始符号用某个产生式展开,再将结果中的 variable 用其产生式展开,重复上述过程直至得到一个全部由 terminal 组成的串,所有这样的串组成了 CFG 定义的语言。
为了定义 derivation,定义一个新的符号
设
可以递归定义零次或多次 derivation
Basis.
Induction. If
为了限制 derivation 时的选择,引入 leftmost derivation 与 rightmost derivation
- leftmost derivation: at each step we replace the leftmost variable by one of its production bodies,
- rightmost derivation: at each step we replace the rightmost variable by one of its production bodies,
The Language of a Grammar
If
For a CFG
sentential form:
left-sentential form:
CFG 定义的语言集合比 RE 定义的语言集合要广
BNF Notation
BNF (Backus-Naur Form) is a notation technique for CFG
- Variables are words in
- Terminals are multicharacter strings indecated by boldface or underline
is used for is used for "or" is used for one or more. e.g. BNF: CFG: - Symbols surrounded by
is optional. e.g. BNF: CFG: - Grouping:
Parse Trees
Definition
Given a grammar
- Each interior node is labeled by a variable in
- Each leaf is labeled by either a variable, a terminal, or
. However, if the leaf is labeled , then it must be the only child of its parent - If an interior node is labeled
, and its children are labeled , then is a production in
将 parse tree 叶节点的 labels 从左向右连接起来,得到的串称为这颗树的 yield。yield 是从 root variable 推导得到的。
有一种特殊的 parse tree 满足
- The yield is a terminal string
- The root is labeled by start symbol
这样的 parse tree,其 yield 是属于该文法的串
Inference, Derivations, and Parse trees
Given a grammar
- The recursive infererence procedure determines that terminal string
is in the language of - There is a parse tree with root
and yield
等价性的证明将按照下图的顺序
From Inferences to Trees
Let
Proof.
Induction on the number of step used to infer that
Basis. 只用一步推断,故有生成式
Induction. 假设经过
令
- 如果
是 terminal, - 如果
是 variable,则 是经过推断得出在 的语言中的一个 string。在 步推断出 在 的过程中这次推断最多有 步。故根据 I.H. ,存在一颗 parse tree 根为 ,yield 为
故可以构造出一颗 parse tree 根为
如此构造出的 parse tree 根为
Q.E.D.
From Trees to Derivations
Let
Proof.
perform an induction on the height of tree.
Basis. 考虑高度为 1 的情况,则这棵树以
Induction. 考虑高度为
为 terminal,定义 为只有 的 string 为 variable,则其一定为某颗子树的根,且有 yield 。显然子树的高度小于 ,根据 I.H. 有
显然有
则可以构造出一个最左推导,第一步是
对
Proof.
Induction on
Basis.
I.H.
Induction.
如果
是 terminal,则 ,显然有 如果
是 variable,根据之前的 I.H. 有 ,令其为 ,则有 可得
Q.E.D.
根据上述证明,在
Q.E.D.
Let
证明同 leftmost derivation,事实上对于一般的 derivation 也有上述结论
From Derivations to Recursive Inference
Let
Proof.
Induction on the length of the derivation
Basis. 推导仅有一步时必有产生式
Induction. 假设推导有
- 如果
是 terminal, - 如果
是 variable,则 的推导肯定小于 步,则根据 I.H. ,recursive inference 得出 在 的语言中
根据以上结论,易得根据 recursive inference 有
Q.E.D.
Ambiguity in Grammars and Languages
Ambiguous Grammars
对于同一个串可以得到一颗以上 parse tree 的文法就是 ambiguous 的,显然根据不同的 parse tree 可以得到不同的最左/最右推导
ambiguous 是 grammar 而非 language 的属性。有些 ambiguous 的文法在修改后可以得到定义同样语言但 unambiguous 的文法
LL(1) grammars are unambiguous
消歧:结合性/优先级/修改文法
Inherent Ambiguity
Certain CFLs are inherently ambiguous, meaning that every grammar for the language is ambiguous
e.g.
Normal Forms for CFGs
Eliminating Useless Symbols
A symbol
从文法中删去 useless 的 symbol 并不会改变 CFG 定义的语言。一个 useful 的 symbol 具有以下两种属性
- generating:
for some terminal string . Every terminal is generating - reachable:
for some and
先删去所有非 generating 的 symbol,再删去所有 unreachable 的 symbol 即可使其余的 symbol 均为 useful
Let
- First eliminate nongenerating symbols and all productions involving one or more of those symbols. Let
be this new grammar. must be generating, since - Second, eliminate all symbols that are not reachable in the grammar
Then
Proof.
考虑
同样的,可以知道
即任意取
只需证明
: trivial,我们通过消除产生式和符号得到 : 若 ,存在一个推导 ,显然这个推导路径上所有符号都是 generating 且 reachable。故
Q.E.D.
找出所有的 generating symbol 是一个递归的过程
Basis. 所有
Induction. 考虑
上述算法可以找出
Proof.
显然可以得出所有算法找出的 symbol 都是 generating。只需证明所有 generating symbol 都会被算法找出。考虑
Basis. 0 步的推导,则
Induction. 考虑推导
Q.E.D.
找出 reachable symbol 的过程同样是一个递归的过程
Basis.
Induction. 若
上述算法可以找出
Eliminating -Productions
If language
Nullable: A variable
寻找 nullable symbol 的算法是一个递归的过程
Basis. If
Induction. 考虑
上述算法可以找到
Proof.
根据算法的归纳过程易得所有算法找出的 variable 都是 nullable,只需证明所有 nullable variable 都会被算法找出。证明的过程是对
Basis. 仅有一步推导,则
Induction. 考虑推导
Q.E.D.
找出所有 nullable variable 后即可构造没有
设经过上述过程后生成的新文法为
Proof.
只需证明对于任意
(
Basis. 一步推导,则
Induction. 考虑
(
一般情况的 variable 可证,令
Q.E.D.
Eliminating Unit Productions
Unit production:
Unit pair:
可以递归地构造 unit pair
Basis.
Induction. 若
上述算法可以找出所有 unit pair
Proof.
只用证明文法中所有 unit pair 都被算法找到。根据
Basis. 0 步,则
Induction. 考虑
Q.E.D.
找到所有 unit pair 之后,可以构造没有 unit pair 的文法
对于每个
可以证明通过上述过程得到的新文法
Proof.
只需证明
(
(
Q.E.D.
Chomsky Normal Form
简化一个 CFG 只要按顺序
- Eliminate
-production - Eliminate unit production
- Eliminate useless symbols
即可得到没有
可以证明任何没有
,其中 都是 variable ,其中 是 variable, 是 terminal
且
将文法按照上述步骤简化后,产生式只有两种可能
,已经满足 CNF ,其中
对于第二种情况,可以按照以下步骤处理
- 将 body 转变为只含 variable
- 将长度大于 2 的 body 拆成一个链
对于第一步,考虑一个长度大于等于 2 的 body 其中的 terminal
对于第二步,考虑产生式
若
证明见课本 P.245