跳到主要内容

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

Author​

itsuitsuki, 祭音Myyura

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={ambmcn∣m,n>0}L_{ab} = \{a^m b^m c^n \mid m, n > 0\} and Lbc={ambncn∣m,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 L1⋅L2L_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 L1∪L2L_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={akbkck∣k>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 L1−L2L_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={ambmcn∣m,n>0}L_{ab}=\{a^mb^mc^n\mid m,n>0\} 与 Lbc={ambncn∣m,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 对并集 L1∪L2L_1\cup L_2 封闭。
  5. 已知 Labc={akbkck∣k>0}L_{abc}=\{a^kb^kc^k\mid k>0\} 不是 CFL,利用该事实证明 CFL 对补集不封闭。
  6. 证明 CFL 对差集 L1−L2L_1-L_2 不封闭。

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},{S,A,C},{S→AC,A→aAb,A→ab,C→cC,C→c},S)G_{ab}=(\{a,b,c\},\{S,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 S→ACS\to AC, (m−1)(m-1) times of A→aAbA\to aAb, (n−1)(n-1) times of C→cCC\to cC, once A→abA\to ab and once C→cC\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},{S,A,C},{S→AC,A→aA,A→a,C→bCc,C→bc},S)G_{bc}=(\{a,b,c\},\{S,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​

After renaming nonterminals if necessary, assume N1∩N2=∅N_1\cap N_2=\emptyset. Choose a new start symbol S∉N1∪N2S\notin N_1\cup N_2 and construct the CFG

G=(Σ,N1∪N2∪{S},P1∪P2∪{S→S1S2},S).G=(\Sigma, N_1\cup N_2\cup\{S\},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 L1⋅L2L_1\cdot L_2. Every string in L1⋅L2L_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 L1⋅L2L_1\cdot L_2 and it is a CFL.

Q.4​

After renaming nonterminals if necessary, assume N1∩N2=∅N_1\cap N_2=\emptyset. Choose a new start symbol S∉N1∪N2S\notin N_1\cup N_2 and construct the CFG

G=(Σ,N1∪N2∪{S},P1∪P2∪{S→S1,S→S2},S).G=(\Sigma,N_1\cup N_2\cup\{S\},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 S→S1S\to S_1 in the first step or falls in L2L_2 by S→S2S\to S_2; every string in language L1∪L2L_1\cup L_2 can find the generation rules in P1P_1 or P2P_2 plus the S→S1S\to S_1 or S→S2S\to S_2 rule.

So GG can generate L1∪L2L_1\cup L_2 and it is a CFL.

Q.5​

Since

Labc=Lab∩LbcL_{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 L1‾∪L2‾\overline{L_1}\cup \overline{L_2} is a CFL, and thus

L1∩L2=L1‾∪L2‾‾L_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 (for example, it is generated by S→aSS\to aS for every a∈Σa\in\Sigma, together with S→εS\to\varepsilon), let LL be a CFL. If the class of CFLs were closed under difference, then Σ∗−L\Sigma^*-L would be 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.