Formal Languages and Compilers (形式语言和编译器) 的 自学笔记兼学习教程。
笔记作者介绍:大爽歌, b站小UP主 ,编程1对1辅导老师。
2.1 Context-Free Grammars
上下文无关语法
A context-free grammar (CFG) is a structure G = ( N , ∑ , P , S ) G=(N, \sum, P, S) G=(N,∑,P,S) where
-
N N N: a finite set, the non-terminals.
each element v ∈ N v \in N v∈N is called a nonterminal characher on a variable.
Each variable represents a different type of phrase or clause in the sentence. Variables are also sometimes called syntactic categories.
Each variable defines a sub-language of the language defined by G G G.
一个有限集合,非终结符。
其元素为非终结字符或变量。
每个变量代表句子中不同类型的短语或从句。
变量有时也称为句法类别。
每个变量定义了 G G G定义的语言的一种子语言。 -
∑ \sum ∑: a finite set of the terminals, disjoint form N.
The set of terminals is the alphabet of the language defined by the grammar G.
一个有限集合, 终结符,与 N N N不相交。
终结符集合是语法 G G G定义的语言的字母表。 -
P P P: P ⊆ N × ( N ∪ ∑ ) ∗ P \subseteq N \times (N \cup \sum)* P⊆N×(N∪∑)∗ is a finite set of productions.(a finite relation).
The member of P P P are called the rules or productions if the grammar.
是生成式的有限集合。(关系的有限集合)。P P P 的成员称为语法的规则或生成式。
-
S S S: S ∈ N S \in N S∈N is the start symbol(or start variable), used to represent the whole sentence(or program).
S S S为起始变量(或起始符号),用于表示整个句子(或程序)。
Productions are denoted as follows:
Productions表示如下:
- A production ( A , w ) ∈ P (A, w)\in P (A,w)∈P is written A → w A\rightarrow w A→w.
- Serveral productions $A \rightarrow w_1,…, A \rightarrow w_n $ are written A → w 1 ∣ . . . ∣ w n A\rightarrow w_1|...|w_n A→w1∣...∣wn
- The right-hand side may be empty: an ε \varepsilon ε-production is written A → w A \rightarrow w A→w.
Example 1
能表示所有算术表达式的
G
G
G
G
=
(
N
,
∑
,
P
,
E
)
G = (N, \sum, P, E)
G=(N,∑,P,E) for arithmetic expressions
- N = { E , T , F } N=\{E, T, F\} N={E,T,F}
- ∑ = + , − , ∗ , / , ( , ) , n \sum={+, -, *, /, (, ), n} ∑=+,−,∗,/,(,),n
- E E E: start symbol
-
P
P
P:
E → T ∣ T + E ∣ T − E E \rightarrow T | T + E | T - E E→T∣T+E∣T−E
T → F ∣ T ∗ F ∣ T / F T \rightarrow F | T * F | T / F T→F∣T∗F∣T/F
$F \rightarrow (E) | n $
其中n代表number
E代表Expression
T代表Term
F代表Factor
Example 2
回文字符(生成所有a和b构成的回文字符)
G
2
=
(
{
S
}
,
{
a
,
b
}
,
P
,
S
)
G_2 = (\{S\}, \{a,b\}, P, S)
G2=({S},{a,b},P,S)
P:
- S → a S a S \rightarrow aSa S→aSa
- S → b S b S \rightarrow bSb S→bSb
- S → a S \rightarrow a S→a
- S → b S \rightarrow b S→b
- S → ε S \rightarrow \varepsilon S→ε
A typical derivation in this grammar is
S
→
a
S
a
→
a
a
S
a
a
→
a
a
b
S
b
a
a
→
a
a
b
b
a
a
S \rightarrow aSa \rightarrow aaSaa \rightarrow aabSbaa \rightarrow aabbaa
S→aSa→aaSaa→aabSbaa→aabbaa
The language is context-free, however, it can be proved that it is not regular.
derivable
生成(导出,派生)
Consider a CFG G = ( N , ∑ , P , S ) G=(N, \sum, P, S) G=(N,∑,P,S)
- Let u , v , w ∈ ( N ∪ ∑ ) ∗ u, v, w \in (N \cup \sum )^* u,v,w∈(N∪∑)∗ and A → w ∈ P A \rightarrow w \in P A→w∈P.
- Then u A v uAv uAv yields uwv in one step, by replacing A A A with w w w; this is denoted u A v ⇒ G 1 u w v uAv \Rightarrow_G^1 uwv uAv⇒G1uwv.
- Alternatively, u w v uwv uwv is derivable form u A w uAw uAw in one step.
- Each of u u u, v v v, w w w may be ε \varepsilon ε.
- A A A can be replaced with w w w irrespective of the context u u u, v v v in which A occurs.
Alternatively: 或者, 换言之
简单来讲,就是:
u
A
v
⇒
G
1
u
w
v
uAv \Rightarrow_G^1 uwv
uAv⇒G1uwv 表示:
u
w
v
uwv
uwv is derivable form
u
A
w
uAw
uAw in 1 step.
u
w
v
uwv
uwv 可由
u
A
w
uAw
uAw 一步生成(导出,派生).
进一步拓展
Derivability is a relation on
(
N
∪
∑
)
∗
(N \cup \sum)^*
(N∪∑)∗
Derivability: 可派生性
- Let x i ∈ ( N ∪ ∑ ) ∗ x_i \in (N \cup \sum)^* xi∈(N∪∑)∗ for each $ i\in \mathbb N$
- x n x_n xn is derivable form x 0 x_0 x0 in n n n steps if x i ⇒ G 1 x i + 1 x_i \Rightarrow_G^1 x_{i+1} xi⇒G1xi+1 for each 0 ≤ i < n 0 \leq i < n 0≤i<n; this is denoted $x_0 \Rightarrow_G^n x_n $
- x ⇒ G 0 y x \Rightarrow_G^0 y x⇒G0y if and only if x = y x=y x=y
- y y y is derivable from x x x if it is derivable in any number of steps.
- Alternatively, x x x generates y y y or x x x yields y y y.
- x ⇒ G ∗ y x \Rightarrow_G^* y x⇒G∗y if x ⇒ G n y x \Rightarrow_G^n y x⇒Gny for some $ n \in \mathbb N$
- The relation ⇒ G ∗ \Rightarrow_G^* ⇒G∗ is the reflexive-transitive closure of the relation ⇒ G 1 \Rightarrow_G^1 ⇒G1
reflexive-transitive closure: 自反传递闭包
举例说明
以前面例2中的CFG
G
2
G2
G2 为例
derivation:
a
S
a
→
a
b
a
aSa \rightarrow aba
aSa→aba
所以
a
S
a
⇒
G
1
a
b
a
aSa \Rightarrow_G^1 aba
aSa⇒G1aba
S → a S a → a a S a a → a a b S b a a → a a b b a a S \rightarrow aSa \rightarrow aaSaa \rightarrow aabSbaa \rightarrow aabbaa S→aSa→aaSaa→aabSbaa→aabbaa
所以
a
a
S
a
a
⇒
G
2
a
a
b
b
a
a
aaSaa \Rightarrow_G^2 aabbaa
aaSaa⇒G2aabbaa
a
S
a
⇒
G
3
a
a
b
b
a
a
aSa \Rightarrow_G^3 aabbaa
aSa⇒G3aabbaa
S
⇒
G
4
a
a
b
b
a
a
S \Rightarrow_G^4 aabbaa
S⇒G4aabbaa
Context-Free Languages
A context-free grammar generates a context-free language (CFL).
- A sentential form is any x ∈ ( N ∪ ∑ ) ∗ x \in (N \cup \sum)^* x∈(N∪∑)∗ deriable from the start symbol S, that is, S ⇒ G ∗ x S\Rightarrow_G^*x S⇒G∗x
- A sentence is a sentential form that consists only of terminal symbols: x ∈ ∑ ∗ x \in \sum^* x∈∑∗
- L ( G ) = { x ∈ ∑ ∗ ∣ S ⇒ G ∗ x } L(G) = \{x \in \sum^* | S \Rightarrow_G^* x \} L(G)={x∈∑∗∣S⇒G∗x} is the language generated by G.
-
A
⊆
∑
∗
A \subseteq \sum^*
A⊆∑∗ is context-free if
A
=
L
(
G
)
A=L(G)
A=L(G)
for some CFG G G G.
A sentential form: 一个句子形式
简单来讲,CFG G G G 生成的语言就是 CFL
Example 3
A
=
{
a
n
b
n
∣
n
∈
N
}
A = \{ a^nb^n | n \in \mathbb N \}
A={anbn∣n∈N} is context-free.
A
=
L
(
G
)
A = L(G)
A=L(G) for the CFG,
G
=
(
{
S
}
,
{
a
,
b
}
,
P
,
S
)
G=(\{S\}, \{a, b\}, P, S)
G=({S},{a,b},P,S)
with production P:
- S → a S b S \rightarrow aSb S→aSb
- S → ε S \rightarrow \varepsilon S→ε
Futher examples of CFLs:
A
=
{
a
i
b
j
c
k
∣
(
i
=
j
o
r
j
=
k
)
a
n
d
i
,
j
,
k
≥
1
}
A=\{a^ib^jc^k | (i=j\;or\; j=k)\; and\; i, j, k \geq 1 \}
A={aibjck∣(i=jorj=k)andi,j,k≥1} is generated by
G
=
(
{
S
,
T
,
U
,
A
,
C
}
,
{
a
,
b
,
c
}
,
P
,
S
)
G=(\{S, T, U, A, C\}, \{a, b, c\}, P, S)
G=({S,T,U,A,C},{a,b,c},P,S) with production P:
- S → T C ∣ A U S \rightarrow TC | AU S→TC∣AU
- T → a T b ∣ a b T \rightarrow aTb | ab T→aTb∣ab
- U → b U c ∣ b c U \rightarrow bUc | bc U→bUc∣bc
- A → a ∣ a A A \rightarrow a | aA A→a∣aA
- C → c ∣ c C C \rightarrow c | cC C→c∣cC
The languages of balanced parentheses is generated by G = ( { S , T , U , A , C } , { ( , ) } , P , S ) G=(\{S, T, U, A, C\}, \{(, )\}, P, S) G=({S,T,U,A,C},{(,)},P,S) with production P:
- S → ( S ) ∣ S S ∣ ε S \rightarrow (S)|SS|\varepsilon S→(S)∣SS∣ε
balanced paratheses: 平衡括号
Regular Grammars
A regular grammar is a CFG G = ( N , ∑ , P , S ) G=(N, \sum, P, S) G=(N,∑,P,S) where for each A → w ∈ P A \rightarrow w \in P A→w∈P
- w = ε w=\varepsilon w=ε, or
- w ∈ ∑ N w \in \sum N w∈∑N
- The right-hand side of each production is either
ε
\varepsilon
ε or a terminal followed by a non-terminal.
即每个产生式的右边要么是 ε \varepsilon ε,要么是一个终结符后面是一个非终结符(即以非终结符结尾)。
Example 4
An example of a regular grammar
G
=
(
N
,
∑
,
P
,
S
)
G=(N, \sum, P, S)
G=(N,∑,P,S)
- N = { S , A } N=\{S, A\} N={S,A}
- ∑ = { a , b , c } \sum=\{a, b, c\} ∑={a,b,c}
- P:
S → a S ∣ b A S \rightarrow aS | bA S→aS∣bA
A → c A ∣ ε A \rightarrow cA | \varepsilon A→cA∣ε
This grammar describes the same language as the regular expression
a
∗
b
c
∗
a^*bc^*
a∗bc∗, viz. the set of all strings consisting of arbitrarily many "a"
s, followed by a single "b"
, followed by arbitrarily many "c"
s.
Every regular language is generated by a regular grammar.
Proof:
- Consider the DFA M = ( Q , ∑ , δ , q 0 , F ) M=(Q, \sum, \delta, q_0, F) M=(Q,∑,δ,q0,F).
- Construct the regular grammar
G
=
(
Q
,
∑
,
P
,
q
0
)
G=(Q, \sum, P, q_0)
G=(Q,∑,P,q0) with the following productions P:
$q_i \rightarrow aq_j \quad if ; \delta(q_i, a) = q_j $
$q_i \rightarrow \varepsilon \quad if ; q_i \in F $ - Then for each w ∈ ∑ ∗ w \in \sum^* w∈∑∗,
w = a 1 a 2 . . . a n − 1 a n ∈ L ( M ) ⇔ δ ^ ( q 0 , w ) ∈ F ⇔ δ ^ ( q i − 1 , a i ) = q i f o r e a c h 1 ≤ i ≤ n a n d q n ∈ F ⇔ q i − 1 → a i q i ∈ P f o r e a c h 1 ≤ i ≤ n a n d q n → ε ∈ F ⇔ q 0 ⇒ a 1 q 1 ⇒ a 1 a 2 q 2 ⇒ . . . ⇒ a 1 a 2 . . . a n − 1 a n q n ⇒ a 1 a 2 . . . a n − 1 a n ⇔ w = a 1 a 2 . . . a n − 1 a n ∈ L ( G ) \begin{align} &w = a_1a_2...a_{n-1}a_n \in L(M) \\ \Leftrightarrow \quad &\hat \delta (q_0, w) \in F \\ \Leftrightarrow \quad &\hat \delta (q_{i-1}, a_i) = q_i \; for\; each\; 1 \leq i \leq n \;and\; q_n \in F \\ \Leftrightarrow \quad & q_{i-1} \rightarrow a_iq_i \in P \;for\; each\; 1 \leq i \leq n \;and\; q_n \rightarrow \varepsilon \in F \\ \Leftrightarrow \quad & q_0 \Rightarrow a_1q_1 \Rightarrow a_1a_2q_2 \Rightarrow ... \Rightarrow a_1a_2...a_{n-1}a_nq_n \Rightarrow a_1a_2...a_{n-1}a_n \\ \Leftrightarrow \quad & w = a_1a_2...a_{n-1}a_n \in L(G) \end{align} ⇔⇔⇔⇔⇔w=a1a2...an−1an∈L(M)δ^(q0,w)∈Fδ^(qi−1,ai)=qiforeach1≤i≤nandqn∈Fqi−1→aiqi∈Pforeach1≤i≤nandqn→ε∈Fq0⇒a1q1⇒a1a2q2⇒...⇒a1a2...an−1anqn⇒a1a2...an−1anw=a1a2...an−1an∈L(G)
- Hence L ( M ) = L ( G ) L(M) = L(G) L(M)=L(G)
Chomsky Normal Form (CNF)
A CFG G = ( Q , ∑ , P , S ) G=(Q, \sum, P, S) G=(Q,∑,P,S) is in Chomsky normal form if every production has the form
- A → B C A \rightarrow BC A→BC, where B , C ∈ N B, C \in N B,C∈N, or
- A → a A \rightarrow a A→a, where a ∈ N a\in N a∈N.
- The right-hand side of each production is either two non-terminals or a terminal.
简单来讲,每个生成式结果为两个非终结符( Q Q Q中的)或者一个终结符( ∑ \sum ∑中的)。
RHS: right-hand side
LHS: left-hand side
Example
G
1
=
(
{
S
,
A
,
B
}
,
{
a
,
b
,
c
}
,
P
,
S
)
G_1 = (\{S, A, B\}, \{a,b,c\}, P, S)
G1=({S,A,B},{a,b,c},P,S) with
P
P
P:
- S → A B S \rightarrow AB S→AB
- S → c S \rightarrow c S→c
- A → a A \rightarrow a A→a
- B → b B \rightarrow b B→b
G 1 = ( { S , A , B } , { a , b , c } , P , S ) G_1 = (\{S, A, B\}, \{a,b,c\}, P, S) G1=({S,A,B},{a,b,c},P,S) with P P P:
- S → a A S \rightarrow aA S→aA
- A → a A \rightarrow a A→a
- B → c B \rightarrow c B→c
G
1
G_1
G1 is in CNF
G
2
G_2
G2 is not in CNF
For every CFG G with ε ∉ L ( G ) \varepsilon \notin L(G) ε∈/L(G) there is a CFG G ′ G' G′ in Chomsky normal form with L ( G ) = L ( G ′ ) L(G)=L(G') L(G)=L(G′)
- Eliminate ε \varepsilon ε-productions of the form A → ε A \rightarrow \varepsilon A→ε.
- Eliminate unit-productions of the form A → B A \rightarrow B A→B.
- Eliminate non-generating non-terminals.
- Eliminate non-reachable non-terminals.
- Eliminate terminals form right-hand sides of length at least 2.
- Eliminate right-hand sides of length at least 3.
Eliminate: 消除
non-generating: 非生成的
non-reachable: 不可到达
Example
Convert the given CFG to CNF. Consider the given grammar G 1 G_1 G1:
G 1 = ( { S , A , B } , { a , b , c } , P , S ) G_1 = (\{S, A, B\}, \{a,b,c\}, P, S) G1=({S,A,B},{a,b,c},P,S) with P P P:
- S → T b T S \rightarrow TbT S→TbT
- T → a U T \rightarrow aU T→aU
- T → U T \rightarrow U T→U
- T → V T \rightarrow V T→V
- U → ε U \rightarrow \varepsilon U→ε
- V → b V \rightarrow b V→b
2.2 The Cocke-Younger-Kasami Algorithm
Given a string w ∈ ∑ ∗ w \in \sum^* w∈∑∗ and a CFL A A A, is $ w \in A$?
分析
- This is the test for membership in a CFL.
- Checking all derivations does not work, since there might be infinitely many.
- It suffices to consider derivations that introduce up to ∣ w ∣ |w| ∣w∣ non-terminals.
- This gives an upper bound on the length of derivations that need to be checked.
- The number of derivations might still be exponential in the length of w w w.
A technique to improve the running time is dynamic programming.
The CYK algorithm solves the membership problem w ∈ L ( G ) w \in L(G) w∈L(G)
Assume that
G
G
G is in chomsky normal form, for example:
S
→
B
B
∣
A
S
∣
a
S \rightarrow BB\; |\; AS |\; a
S→BB∣AS∣a
A
→
B
C
A \rightarrow BC
A→BC
B
→
B
S
∣
B
B \rightarrow BS \; | \; B
B→BS∣B
C
→
a
C \rightarrow a
C→a
- let n n n be the length of w w w: for example, n = 6 n=6 n=6 for w = b b a b a b w=bbabab w=bbabab.
- mark the positions that separate symbols in w w w:
- Let w i j w_{ij} wij be the substring of w w w between positions i i i and j j j: for example, w 25 = a b a w_{25}=aba w25=aba and w 06 = w w_{06} = w w06=w.
- N i j = { A ∈ N ∣ A ⇒ ∗ w i j } N_{ij} = \{A \in N \;|\; A \Rightarrow ^* w_{ij}\} Nij={A∈N∣A⇒∗wij} contains the non-terminals that generate w i j w_{ij} wij
- The CYK algorithm calculates N i j N_{ij} Nij for each 0 ≤ i ≤ j ≤ n 0\leq i \leq j \leq n 0≤i≤j≤n
- Then w ∈ L ( G ) w \in L(G) w∈L(G) if and only if S ∈ N 0 n S \in N_{0n} S∈N0n
The CYK algorithm fills a table with
N
i
j
N_{ij}
Nij in column
i
i
i, row
j
j
j.
The calculation proceeds in increasing order of substring length.
Example
CFG
G
2
=
(
{
S
,
A
,
B
,
C
}
,
{
a
,
b
}
,
S
,
P
)
G_2 = (\{S, A, B, C\}, \{a, b\}, S, P)
G2=({S,A,B,C},{a,b},S,P) with P:
S
→
A
B
∣
B
C
S \rightarrow AB\; |\; BC
S→AB∣BC
A
→
B
A
∣
a
A \rightarrow BA\; |\; a
A→BA∣a
B
→
C
C
∣
b
B \rightarrow CC\; |\; b
B→CC∣b
C
→
A
B
∣
a
C \rightarrow AB\; |\; a
C→AB∣a
check b b a b a a ∈ L ( G 2 ) bbabaa \in L(G_2) bbabaa∈L(G2) ?
下来用CYK算法绘制表格
- 初始表格
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | - | - | - | - | - | |
j = 2 j=2 j=2 | - | - | - | - | ||
j = 3 j=3 j=3 | - | - | - | |||
j = 4 j=4 j=4 | - | - | ||||
j = 5 j=5 j=5 | - | |||||
j = 6 j=6 j=6 |
- 处理表格中长度为1的。
First come the substrings of length 1, that is, j = i + 1 j = i + 1 j=i+1.
- The 1-symbol substring w i , i + 1 w_{i, i+1} wi,i+1 can be generated form A A A if A → w i , i + 1 ∈ P A \rightarrow w_{i, i+1} \in P A→wi,i+1∈P.
- For each production A → a A \rightarrow a A→a where a = w i , i + 1 a = w_{i, i+1} a=wi,i+1, add A to the entry at column i i i, row j j j.
- These entries form the main diagonal of the table.
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | { B } \{B\} {B} | - | - | - | - | - |
j = 2 j=2 j=2 | { B } \{B\} {B} | - | - | - | - | |
j = 3 j=3 j=3 | { A , C } \{A, C\} {A,C} | - | - | - | ||
j = 4 j=4 j=4 | { B } \{B\} {B} | - | - | |||
j = 5 j=5 j=5 | { A , C } \{A, C\} {A,C} | - | ||||
j = 6 j=6 j=6 | { A , C } \{A, C\} {A,C} |
- 处理表格中长度为2的。
Then come the substrings of length 2, that is, j = i + 2 j = i + 2 j=i+2.
- The 2-symbol substring w i , i + 2 w_{i, i+2} wi,i+2 is broken up into two 1-symbol substrings w i , i + 1 w_{i, i+1} wi,i+1 and w i + 1 , i + 2 w_{i+1, i+2} wi+1,i+2.
- If B ∈ N i , i + 1 B \in N_{i, i+1} B∈Ni,i+1 and C ∈ N i + 1 , i + 2 C \in N_{i+1, i+2} C∈Ni+1,i+2 and A → B C ∈ P A \rightarrow BC \in P A→BC∈P, then add A A A to N i , i + 2 N_{i, i+2} Ni,i+2.
- These entries form the diagonal below the main diagonal.
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | { B } \{B\} {B} | - | - | - | - | - |
j = 2 j=2 j=2 | - | { B } \{B\} {B} | - | - | - | - |
j = 3 j=3 j=3 | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | - | - | |
j = 4 j=4 j=4 | { S , C } \{S, C\} {S,C} | { B } \{B\} {B} | - | - | ||
j = 5 j=5 j=5 | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | |||
j = 6 j=6 j=6 | { B } \{B\} {B} | { A , C } \{A, C\} {A,C} |
-
计算 w 02 w_{02} w02, 将其拆分成 w 01 w_{01} w01 和 w 12 w_{12} w12.
N 01 = { B } N_{01} = \{B\} N01={B}, N 12 = { B } N_{12} = \{B\} N12={B}, 没有 A ∈ P → B B A \in P \rightarrow BB A∈P→BB, 所有不存在 -
计算 w 13 w_{13} w13, 将其拆分成 w 12 w_{12} w12 和 w 23 w_{23} w23.
N 12 = { B } N_{12} = \{B\} N12={B}, N 23 = { A , C } N_{23} = \{A, C\} N23={A,C}, A → B A ∈ P A \rightarrow BA \in P A→BA∈P, S → B C ∈ P S \rightarrow BC \in P S→BC∈P, 所以 N 13 = { S , A } N_{13} = \{S, A\} N13={S,A} -
计算 w 24 w_{24} w24, 将其拆分成 w 23 w_{23} w23 和 w 34 w_{34} w34.
N 23 = { A , C } N_{23} = \{A, C\} N23={A,C}, N 34 = { B } N_{34} = \{B\} N34={B}, S → A B ∈ P S \rightarrow AB \in P S→AB∈P, C → A B ∈ P C \rightarrow AB \in P C→AB∈P,所以 N 24 = { S , C } N_{24} = \{S, C\} N24={S,C}
- 处理表格中长度为3的。
The 3-symbol substring w i , i + 3 w_{i, i+3} wi,i+3 can be broken up in two ways.
-
Consider how to generate w i , i + 3 w_{i, i+3} wi,i+3 using a production A → B C A\rightarrow BC A→BC, that is, A ⇒ B C ⇒ ∗ w i , i + 3 A \Rightarrow BC \Rightarrow^* w_{i, i+3} A⇒BC⇒∗wi,i+3
-
This follows from B ⇒ ∗ w i , i + 1 B \Rightarrow^* w_{i, i+1} B⇒∗wi,i+1 and C ⇒ ∗ w i + 1 , i + 3 C \Rightarrow^*w_{i+1, i+3} C⇒∗wi+1,i+3 or from B ⇒ ∗ w i , i + 2 B \Rightarrow^* w_{i, i+2} B⇒∗wi,i+2 and C ⇒ ∗ w i + 2 , i + 3 C \Rightarrow^*w_{i+2, i+3} C⇒∗wi+2,i+3
-
If B ∈ N i , i + 1 B \in N_{i, i+1} B∈Ni,i+1 and C ∈ N i + 1 , i + 3 C \in N_{i+1, i+3} C∈Ni+1,i+3 and A → B C ∈ P A \rightarrow BC \in P A→BC∈P, then add A A A to N i , i + 3 N_{i, i+3} Ni,i+3.
-
If B ∈ N i , i + 2 B \in N_{i, i+2} B∈Ni,i+2 and C ∈ N i + 2 , i + 3 C \in N_{i+2, i+3} C∈Ni+2,i+3 and A → B C ∈ P A \rightarrow BC \in P A→BC∈P, then add A A A to N i , i + 3 N_{i, i+3} Ni,i+3.
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | { B } \{B\} {B} | - | - | - | - | - |
j = 2 j=2 j=2 | - | { B } \{B\} {B} | - | - | - | - |
j = 3 j=3 j=3 | { A } \{A\} {A} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | - | - |
j = 4 j=4 j=4 | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { B } \{B\} {B} | - | - | |
j = 5 j=5 j=5 | { B } \{B\} {B} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | ||
j = 6 j=6 j=6 | - | { B } \{B\} {B} | { A , C } \{A, C\} {A,C} |
-
w
03
w_{03}
w03 可以分成
w
01
w_{01}
w01+
w
13
w_{13}
w13, 计算出
A
A
A
或分成 w 02 w_{02} w02+ w 23 w_{23} w23, w 02 w_{02} w02空,没有 -
w
14
w_{14}
w14 可以分成
w
12
w_{12}
w12+
w
24
w_{24}
w24, 计算出
S
S
S
或分成 w 13 w_{13} w13+ w 34 w_{34} w34, 计算出 S , C S, C S,C
- 处理表格中长度为4的。
此时每个 w i , i + 4 w_{i, i+4} wi,i+4有三种分法
- w i , i + 1 w_{i, i+1} wi,i+1 和 w i + 1 , i + 4 w_{i+1, i+4} wi+1,i+4
- w i , i + 2 w_{i, i+2} wi,i+2 和 w i + 2 , i + 4 w_{i+2, i+4} wi+2,i+4
- w i , i + 3 w_{i, i+3} wi,i+3 和 w i + 3 , i + 4 w_{i+3, i+4} wi+3,i+4
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | { B } \{B\} {B} | - | - | - | - | - |
j = 2 j=2 j=2 | - | { B } \{B\} {B} | - | - | - | - |
j = 3 j=3 j=3 | { A } \{A\} {A} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | - | - |
j = 4 j=4 j=4 | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { B } \{B\} {B} | - | - |
j = 5 j=5 j=5 | { B } \{B\} {B} | { B } \{B\} {B} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | |
j = 6 j=6 j=6 | { A , S } \{A, S\} {A,S} | - | { B } \{B\} {B} | { A , C } \{A, C\} {A,C} |
- 处理表格中长度为5的。
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | { B } \{B\} {B} | - | - | - | - | - |
j = 2 j=2 j=2 | - | { B } \{B\} {B} | - | - | - | - |
j = 3 j=3 j=3 | { A } \{A\} {A} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | - | - |
j = 4 j=4 j=4 | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { B } \{B\} {B} | - | - |
j = 5 j=5 j=5 | { B } \{B\} {B} | { B } \{B\} {B} | { B } \{B\} {B} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - |
j = 6 j=6 j=6 | { A , S } \{A, S\} {A,S} | { A , S } \{A, S\} {A,S} | - | { B } \{B\} {B} | { A , C } \{A, C\} {A,C} |
- 处理表格中长度为6的。
i | 0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|---|
a i a_i ai | b | b | a | b | a | a |
j = 1 j=1 j=1 | { B } \{B\} {B} | - | - | - | - | - |
j = 2 j=2 j=2 | - | { B } \{B\} {B} | - | - | - | - |
j = 3 j=3 j=3 | { A } \{A\} {A} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - | - | - |
j = 4 j=4 j=4 | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { S , C } \{S, C\} {S,C} | { B } \{B\} {B} | - | - |
j = 5 j=5 j=5 | { B } \{B\} {B} | { B } \{B\} {B} | { B } \{B\} {B} | { S , A } \{S,A\} {S,A} | { A , C } \{A, C\} {A,C} | - |
j = 6 j=6 j=6 | { A , S } \{A, S\} {A,S} | { A , S } \{A, S\} {A,S} | { A , S } \{A, S\} {A,S} | - | { B } \{B\} {B} | { A , C } \{A, C\} {A,C} |
最后
S
∈
N
06
S \in N_{06}
S∈N06
所以
b
b
a
b
a
a
bbabaa
bbabaa可以由该语法生成。