Finding regular expressions for languages otherwise described

拥有回忆 提交于 2019-12-11 01:39:04

问题


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 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.


回答1:


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.



来源:https://stackoverflow.com/questions/7626800/finding-regular-expressions-for-languages-otherwise-described

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!