0

I want to make a regular expression for the language consisting of words whose length is at least 3 and which contain at most two $x$'s, that is,
$$\{w\in \{x,y\}^* \mid |w|\geq3\text{ and the number of }x\text{'s in }w \text{ is at most } 2\}$$

I wrote: $$ x^+(x+y)^+y^+ $$

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
  • 1
    Write it as a union according to the number of $x$, which are $0$, $1$ or $2$. Now, each of these three cases can be divided into further cases according to the minimum number of $y$ required at each side of each $x$. For example, the case of no $x$ is simple $yyy^+$. We need at least three $y$. For the case of one $x$ you have three subcases. You either have at least two $y$ to the left of $x$ and any number to the right, or you have at least one $y$ on each side, or at least two $y$ to the right and any number to the left. So, this case is $yy^+xy^|y^+xy^+|y^xyy^+$. – plop Mar 27 '21 at 02:31
  • Find out the case of exactly two $x$. – plop Mar 27 '21 at 02:32
  • https://cs.stackexchange.com/q/1331/755 – D.W. Mar 27 '21 at 02:32
  • @plop is the case when x =2 ... \begin{align} xx^+ yx^* \mid x^+ y x^+ \mid y^* yxx^+ \end{align} how can we do the union, i am a begginer in this. – Nicolette Lorenzo Mar 27 '21 at 13:07

1 Answers1

0

Your regular expression accepts the word $xxxy$, which contains too many $x$s.

We can break down words in your language into three different kinds:

  • No $x$s, and so at least three many $y$s.
  • One $x$. In that case, the word is of the form $y^ixy^j$, with $i+j \geq 2$. This means that either $i = 0$ and $j \geq 2$, or $i = 1$ and $j \geq 1$, or $i \geq 2$. 0 Two $x$s. In that case, the word is of the form $y^ixy^jxy^k$, with $i+j+k \geq 1$. This means that either $i=j=0$ and $k \geq 1$, or $i=0$ and $j \geq 1$, or $i \geq 1$.

Putting everything together, we reach $$ yyy^+ + xyy^+ + yxy^+ + yy^+xy^* + xxy^+ + xy^+xy^* + y^+xy^*xy^*. $$ As a bonus, this regular expression is "unambiguous", in the sense that each word belongs to exactly one summand.

We can shorten this expression in many ways. For example, notice that if we consider only words with exactly two $x$s, we can obtain all words in the language by optionally replacing an $x$ with a $y$. This gives us the simpler expression $$ (x+y)^2y^+ + (x+y)y^+(x+y)y^* + y^+(x+y)y^*(x+y)y^*. $$ If we want something more symmetric, we can take $$ y^+(x+y)y^*(x+y)y^* + y^*(x+y)y^+(x+y)y^* + y^*(x+y)y^*(x+y)y^+. $$

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503