京都大学 情報学研究科 知能情報学専攻 2023年8月実施 専門科目 S-6
Author
itsuitsuki, 祭音Myyura
Description
We consider a grammar G=(Σ,N,P,S), where Σ,N,P, and S 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 G is a context-free grammar when, for each production rule α→β in P, α∈(1) and β∈(2). A language L on (3) is a context-free language when it is generated by a context-free grammar G.
Q.2
Prove that Lab={ambmcn∣m,n>0} and Lbc={ambncn∣m,n>0} are context-free languages.
Q.3
Prove that the class of context-free languages is closed under concatenation L1⋅L2 by constructing G3=(Σ,N3,P3,S3) from the context-free grammars G1=(Σ,N1,P1,S1) and G2=(Σ,N2,P2,S2) generating L1 and L2, respectively.
Q.4
Prove that the class of context-free languages is closed under union L1∪L2 by constructing G3=(Σ,N3,P3,S3) from the context-free grammars G1=(Σ,N1,P1,S1) and G2=(Σ,N2,P2,S2).
Q.5
Labc={akbkck∣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.
Q.6
Prove that the class of context-free languages is not closed under difference L1−L2.
题目描述
考虑文法 G=(Σ,N,P,S),分别由有限终结符集、有限非终结符集、有限产生式集和开始符号组成。
- 填空:若每条产生式 α→β 均满足
α∈(1)、β∈(2),则 G 是上下文无关文法;由上下文无关文法生成的、定义在
(3) 上的语言为上下文无关语言。
- 证明
Lab={ambmcn∣m,n>0} 与
Lbc={ambncn∣m,n>0} 是上下文无关语言。
- 给定生成 L1,L2 的 CFG
G1=(Σ,N1,P1,S1)、
G2=(Σ,N2,P2,S2),构造 G3 证明 CFL 对串接
L1L2 封闭。
- 类似构造证明 CFL 对并集 L1∪L2 封闭。
- 已知
Labc={akbkck∣k>0} 不是 CFL,利用该事实证明 CFL 对补集不封闭。
- 证明 CFL 对差集 L1−L2 不封闭。
Kai
Q.1
(1): N
(2): (N∪Σ)∗
(3): Σ (by definition, it's not Σ∗)
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 Lab, 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)
and for ∀n,m>0, a string in the language ambmcn can be generated by applying once S→AC, (m−1) times of A→aAb, (n−1) times of C→cC, once A→ab and once C→c by order.
For a string generated, say ambmcn, it must be in Lab by the reversed logic above.
So Lab 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)
Lbc is generated by Gbc, thus being a CFL.
Q.3
After renaming nonterminals if necessary, assume N1∩N2=∅. Choose a new start symbol S∈/N1∪N2 and construct the CFG
G=(Σ,N1∪N2∪{S},P1∪P2∪{S→S1S2},S).
every generated string has a part of substrings (at left) in L1 (derived from S1), while the right part is in L2 derived from S2, forming L1⋅L2. Every string in L1⋅L2 can also be generated by G by separating into 2 parts and finding the corresponding P1 and P2 rules from S1 and S2.
So G can generate L1⋅L2 and it is a CFL.
Q.4
After renaming nonterminals if necessary, assume N1∩N2=∅. Choose a new start symbol S∈/N1∪N2 and construct the CFG
G=(Σ,N1∪N2∪{S},P1∪P2∪{S→S1,S→S2},S).
every generated string either falls in L1 by applying S→S1 in the first step or falls in L2 by S→S2; every string in language L1∪L2 can find the generation rules in P1 or P2 plus the S→S1 or S→S2 rule.
So G can generate L1∪L2 and it is a CFL.
Q.5
Since
Labc=Lab∩Lbc
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 Labc, and Labc is not a CFL, if the class of CFLs are closed under complement, then for ∀L1,L2 CFL, L1 and L2 are CFLs, and L1∪L2 is a CFL, and thus
L1∩L2=L1∪L2
is a CFL (contradiction), hence under complement the class is not closed.
Q.6
Since Σ∗ is a CFL (for example, it is generated by S→aS for every a∈Σ, together with S→ε), let L be a CFL. If the class of CFLs were closed under difference, then Σ∗−L would be a CFL.
However, L=Σ∗−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.