跳到主要内容

京都大学 情報学研究科 知能情報学専攻 2023年8月実施 専門科目 S-6

Author

itsuitsuki

Description

We consider a grammar G=(Σ,N,P,S)G = (\Sigma, N, P, S), where Σ,N,P\Sigma, N, P, and SS are a finite set of terminal symbols, a finite set of nonterminal symbols, a finite set of production rules, and the start symbol, respectively.

Q.1

The following is a description on context-free grammar. Fill the blanks (1), (2), and (3).

  • A grammar GG is a context-free grammar when, for each production rule αβ\alpha \rightarrow \beta in PP, α(1)\alpha \in\boxed{\qquad(1)\qquad} and β(2)\beta \in\boxed{\qquad(2)\qquad}. A language LL on (3)\boxed{\qquad(3)\qquad} is a context-free language when it is generated by a context-free grammar GG.

Q.2

Prove that Lab={ambmcnm,n>0}L_{ab} = \{a^m b^m c^n \mid m, n > 0\} and Lbc={ambncnm,n>0}L_{bc} = \{a^m b^n c^n \mid m, n > 0\} are context-free languages.

Q.3

Prove that the class of context-free languages is closed under concatenation L1L2L_1 \cdot L_2 by constructing G3=(Σ,N3,P3,S3)G_3 = (\Sigma, N_3, P_3, S_3) from the context-free grammars G1=(Σ,N1,P1,S1)G_1 = (\Sigma, N_1, P_1, S_1) and G2=(Σ,N2,P2,S2)G_2 = (\Sigma, N_2, P_2, S_2) generating L1L_1 and L2L_2, respectively.

Q.4

Prove that the class of context-free languages is closed under union L1L2L_1 \cup L_2 by constructing G3=(Σ,N3,P3,S3)G_3 = (\Sigma, N_3, P_3, S_3) from the context-free grammars G1=(Σ,N1,P1,S1)G_1 = (\Sigma, N_1, P_1, S_1) and G2=(Σ,N2,P2,S2)G_2 = (\Sigma, N_2, P_2, S_2).

Q.5

Labc={akbkckk>0}L_{abc} = \{a^k b^k c^k \mid k > 0\} is not a context-free language. By using this fact prove that the class of context-free languages is not closed under complement L\overline{L}.

Q.6

Prove that the class of context-free languages is not closed under difference L1L2L_1 - L_2.

题目描述

考虑文法 G=(Σ,N,P,S)G=(\Sigma,N,P,S),分别由有限终结符集、有限非终结符集、有限产生式集和开始符号组成。

  1. 填空:若每条产生式 αβ\alpha\to\beta 均满足 α(1)\alpha\in\boxed{(1)}β(2)\beta\in\boxed{(2)},则 GG 是上下文无关文法;由上下文无关文法生成的、定义在 (3)\boxed{(3)} 上的语言为上下文无关语言。
  2. 证明 Lab={ambmcnm,n>0}L_{ab}=\{a^mb^mc^n\mid m,n>0\}Lbc={ambncnm,n>0}L_{bc}=\{a^mb^nc^n\mid m,n>0\} 是上下文无关语言。
  3. 给定生成 L1,L2L_1,L_2 的 CFG G1=(Σ,N1,P1,S1)G_1=(\Sigma,N_1,P_1,S_1)G2=(Σ,N2,P2,S2)G_2=(\Sigma,N_2,P_2,S_2),构造 G3G_3 证明 CFL 对串接 L1L2L_1L_2 封闭。
  4. 类似构造证明 CFL 对并集 L1L2L_1\cup L_2 封闭。
  5. 已知 Labc={akbkckk>0}L_{abc}=\{a^kb^kc^k\mid k>0\} 不是 CFL,利用该事实证明 CFL 对补集不封闭。
  6. 证明 CFL 对差集 L1L2L_1-L_2 不封闭。

考点

  • 上下文无关文法定义与构造:填写产生式两侧集合,并为两个计数语言显式设计 CFG。
  • CFL 封闭性质:通过新开始符号构造串接、并集文法。
  • 不封闭性归约:结合 CFL 对并集/交正则语言的封闭性及 akbkcka^kb^kc^k 反例,证明补集和差集不封闭。

Kai

Q.1

(1): NN

(2): (NΣ)(N\cup \Sigma)^*

(3): Σ\Sigma (by definition, it's not Σ\Sigma^*)

Q.2

We construct a grammar that can generate all strings in a corresponding language, where all strings generated by the grammar are in the language.

For LabL_{ab}, we construct a CFG (context-free grammar)

Gab=({a,b,c},{A,C},{SAC,AaAb,Aab,CcC,Cc},S)G_{ab}=(\{a,b,c\},\{A,C\},\{S\to AC,A\to aAb,A\to ab,C\to cC,C\to c\},S)

and for n,m>0\forall n,m>0, a string in the language ambmcna^mb^mc^n can be generated by applying once SACS\to AC, (m1)(m-1) times of AaAbA\to aAb, (n1)(n-1) times of CcCC\to cC, once AabA\to ab and once CcC\to c by order.

For a string generated, say ambmcna^mb^mc^n, it must be in LabL_{ab} by the reversed logic above.

So LabL_{ab} is a CFL (context-free language).

Similarly, by a CFG

Gbc=({a,b,c},{A,C},{SAC,AaA,Aa,CbCc,Cbc},S)G_{bc}=(\{a,b,c\},\{A,C\},\{S\to AC,A\to aA,A\to a,C\to bCc,C\to bc\},S)

LbcL_{bc} is generated by GbcG_{bc}, thus being a CFL.

Q.3

By constructing a new CFG

G=(Σ,N1N2,P1P2{SS1S2},S)G=(\Sigma, N_1\cup N_2,P_1\cup P_2\cup\{S\to S_1S_2\},S)

every generated string has a part of substrings (at left) in L1L_1 (derived from S1S_1), while the right part is in L2L_2 derived from S2S_2, forming L1L2L_1\cdot L_2. Every string in L1L2L_1\cdot L_2 can also be generated by GG by separating into 2 parts and finding the corresponding P1P_1 and P2P_2 rules from S1S_1 and S2S_2.

So GG can generate L1L2L_1\cdot L_2 and it is a CFL.

Q.4

By constructing a new CFG

G=(Σ,N1N2,P1P2{SS1,SS2},S)G=(\Sigma,N_1\cup N_2,P_1\cup P_2\cup\{S\to S_1,S\to S_2\},S)

every generated string either falls in L1L_1 by applying SS1S\to S_1 in the first step or falls in L2L_2 by SS2S\to S_2; every string in language L1L2L_1\cup L_2 can find the generation rules in P1P_1 or P2P_2 plus the SS1S\to S_1 or SS2S\to S_2 rule.

So GG can generate L1L2L_1\cup L_2 and it is a CFL.

Q.5

Since

Labc=LabLbcL_{abc}=L_{ab}\cap L_{bc}

because in RHS, the number of a's and b's must be equal and that of b's and c's are also equal, so the intersection is by definition LabcL_{abc}, and LabcL_{abc} is not a CFL, if the class of CFLs are closed under complement, then for L1,L2\forall L_1,L_2 CFL, L1\overline{L_1} and L2\overline{L_2} are CFLs, and L1L2\overline{L_1}\cup \overline{L_2} is a CFL, and thus

L1L2=L1L2L_1\cap L_2=\overline{\overline{L_1}\cup\overline{L_2}}

is a CFL (contradiction), hence under complement the class is not closed.

Q.6

Since Σ\Sigma^* is a CFL with a context-free grammar of Σ\Sigma, an arbitrary NN, a rule set PP with every possible rule based on Σ\Sigma and NN and a start symbol SS, let LL be a CFL, assume under difference the class of CFLs is closed, then ΣL\Sigma^*-L is a CFL.

However, L=ΣL\overline L=\Sigma^*-L is by definition not necessarily a CFL because in Q.5, complement cannot make the class closed (contradiction).

So under difference the class of CFLs is not closed.