京都大学 情報学研究科 知能情報学専攻 2019年2月実施 専門科目 F1-2
Author
祭音Myyura (co-authored with GPT 5.2 extended thinking)
Description
Give context-free grammars that generate the following languages. In all parts, the alphabet (terminal character set) is {0,1}.
Q.1 {w∣w contains at least three 1s}
Q.2 {w∣the length of w is odd and its middle symbol is a 0}
Q.3 {w∣w=wR, where R denotes writing backwards, that is, w is a palindrome}
题目描述
对下列每种语言分别给出一个上下文无关文法;所有问题的终结符字母表均为 {0,1}。
- {w∣w 至少含三个 1};
- {w∣∣w∣ 为奇数且正中间符号为 0};
- {w∣w=wR},其中 wR 表示逆序书写 w,即该语言为所有二进制回文串。
- 上下文无关文法构造:分别用非终结符记录至少三个指定符号、围绕中心同步生成两侧字符,以及递归生成回文结构。
Kai
Q.1
- Terminals: Σ=0,1
- Nonterminals: {S,A}
- Start symbol: S
- Productions:
SA→A 1 A 1 A 1 A→0A∣1A∣ε
Here A generates any binary string, i.e. L(A)=Σ∗.
Q.2
A string has odd length and middle symbol 0 iff it can be written as
w=x 0 ywith ∣x∣=∣y∣.
Grammar (G_2)
- Terminals: Σ={0,1}
- Nonterminals: {S}
- Start symbol: S
- Productions:
S→0∣0S0∣0S1∣1S0∣1S1
Intuition: Start with the center 0. Each step adds one symbol on the left and one on the right, preserving odd length and keeping the center fixed.
Q.3
- Terminals: Σ={0,1}
- Nonterminals: {S}
- Start symbol: S
- Productions:
S→ε∣0∣1∣0S0∣1S1