0

I'm in a Formal languages class and have a grammar quiz coming up. I'm assuming something like this will appear.

Consider the alphabet $\Sigma$ = {a, b, c}. Construct a grammar that generates the language $L = \{bab^nabc^na^p \mid n ≥ 0, p ≥ 1\}$. Assume that the start variable is $S$.

Ran G.
  • 20,884
  • 3
  • 61
  • 117

1 Answers1

2

One possible grammar is: $G = (N, \Sigma, P, S)$, where

$N = \{S,U,V\}$

$\Sigma = \{a,b,c\}$

$P = \{ S \to baUV, U \to ab|bUc, V \to a|aV \}$

The key observation here is breaking $bab^nabc^na^p$ into 3 parts, $ba$, $b^nabc^n$, and $a^p$, one fixed, one growing bidirectionally, and one simple repeatition. Express the growing patterns first, then merge them along with the fixed parts.

Terence Hang
  • 789
  • 4
  • 7