开发者

Finding regular expressions for languages otherwise described

开发者 https://www.devze.com 2023-04-09 18:51 出处:网络
Letting {a b} be the alphabet set, write a regular expression for: 1) The language of all those words in which the number of a\'s and the number of b\'s are both 开发者_StackOverflow中文版odd;

Letting {a b} be the alphabet set, write a regular expression for:

1) The language of all those words in which the number of a's and the number of b's are both 开发者_StackOverflow中文版odd;

2) The language of all those words whose length is odd and which contain the substring ab.

Also, if possible, please help me find two different expressions for each so as to help strengthen my understanding of how to go about solving such problems.


For the first one, there's an easy 4-state DFA you can construct to recognize the language. Then, you can use the algorithm recoverable from Kleene's theorem (the part where he says all languages recognized by a FA are generated by a RE) to get an RE that works... or just reason it out from the diagram.

For the second one, you know that (ab) is part of the RE; now, you need to think of all the unique ways you could add an odd number of characters to this (front or back), and connect all those possibilities with + for an easy, correct RE.

I don't think anybody particularly likes the idea of just giving you the answer.

EDIT:

So now that some time has passed, I'll work through the answer to the first one to show interested readers how it can be done.

Our first FA is this:

   Q s f(Q, s)
  -- - -------
  EE a     OE
  EE b     EO
  OE a     EE
  OE b     OO
  EO a     OO
  EO b     EE
  OO a     EO
  OO b     OE

We will remove states from this and replace s with a regular expression to cover that state. We start with an easy one... let's get rid of OE. Here's the table for that...

   Q                  regex f(Q, s)
  -- ---------------------- -------
  EE                     aa      EE
  EE                     ab      OO
  EE                      b      EO
  EO                      a      OO
  EO                      b      EE
  OO                      a      EO
  OO                     ba      EE
  OO                     bb      OO

Convince yourself this is correct before continuing. Next, we get rid of EO:

   Q                  regex f(Q, s)
  -- ---------------------- -------
  EE                  aa+bb      EE
  EE                  ab+ba      OO
  OO                  ab+ba      EE
  OO                  aa+bb      OO

To make the next step simpler, we introduce a new start set X and a new accepting state Y; OO is no longer accepting. We eliminate the need for OO:

   Q                        regex f(Q, s)
  -- ---------------------------- -------
   X                        empty      EE
  EE aa+bb+(ab+ba)(aa+bb)*(ab+ba)      EE
  EE              (ab+ba)(aa+bb)*       Y

Therefore, the final regex is

  (aa+bb+(ab+ba)(aa+bb)*(ab+ba))*(ab+ba)(aa+bb)*

We can begin trying to list the smallest strings this generates, just as a basic sanity check: {ab, ba, aaab, aaba, bbab, bbba, abaa, abbb, baaa, babb, ...} Looks good to me!

The rules for reducing at each step can be formalized, or you can just apply careful reasoning to ensure that you're getting the right thing. Check a proof of Kleene's theorem for a careful analysis. Also, Martin's Introduction to Formal Languages or something has good examples of using this algorithm.

0

精彩评论

暂无评论...
验证码 换一张
取 消