1

Let's suppose I want to put an equal number of P balls and D balls in a box. The last ball must always be a D, and the number of D balls in the box can never be greater than the number of P balls.I want to present a context free grammar. Until now I have come with:

S → ε | PS | DS |

But it does not cover cases in which D is bigger than P. I also tried:

S → PSD | ɛ

But like that you can't have patterns like: PDPD, only like PPDD, you can't interleave the P's and D's that wayI would appreciate some help. Thanks.

Nathaniel
  • 18,309
  • 2
  • 30
  • 58
U8500
  • 19
  • 2

2 Answers2

1

You already know the Dyck language, the Dyck-1 language over the alphabet $\Sigma = \{P, D\}$ is defined as $$\mathbb{D} = \{v \in \Sigma^* : |v|_D = |v|_P \land ( \forall w \text{ prefix of } v . |w|_D \leq |w|_P) \}$$

and can be generated by the grammar (with start variable $S$)

$$G: S \to SPSD | \varepsilon.$$

Your language (call it $L$) is the subset of words in $\mathbb{D}$ ending in $D$, i.e. $L = \mathbb{D} \cap (P|D)^*D$¹ which is just $\{v \in \mathbb{D} : v \neq \varepsilon\}$ since all non-empty words in $\mathbb{D}$ end in $D$.

So if you think about it, the words of $L$ must be exactly those generated by $G$ starting with $S \Rightarrow SPSD \Rightarrow ...$. A simple trick to "force" such derivations is to introduce a new start symbol $S'$ with rules

$$S' \to SPSD.$$


[1] You could technically find a grammar for $L$ algorithmically since $(P|D)^*D$ is regular, see here.

Knogger
  • 2,049
  • 3
  • 15
0

S → ε may or may not be appropriate.
The alternatives got to make sure both remaining requirements are met:

S → ND introduces a new non-terminal N (Needing one more P) and takes care of last must be D (Unfortunate to have terminal symbols in upper case as well as non-terminals…)
N → P is one way to produce that symbol.

That leaves producing no more Ds than Ps.

greybeard
  • 1,172
  • 2
  • 9
  • 24