Search code examples
compiler-constructionregular-language

Given a binary alphabet {0,1}, write a regular expression that recognises all words that have an even number of ’1’s and starts and ends with a ’0’


I'm working through some exam questions for a module in college. I've been asked to create a regular expression that recognises all words that have an even number of ’1’s and starts and ends with a ’0’ when given a binary alphabet {0, 1}. However, the solutions are not available online and I want to verify that my solution is correct as I then must convert create an NFA -> DFA using Subset Closure from this regular expresion.

I'm wondering if the following is correct?

0*, (1, 0*, 1)*, 0*


Solution

  • Actually I think you solution has some flaws.

    1. It's not checking whether your word starts or ends with 0 (should be + instead of *
    2. It does not work, e.g. for 0110110, since the 0 in the middle can not be matched (include possibility to match only 0)
    3. It checks whether there is an occurance of the searched string in your word and not whether the whole word matches the string (include ^ and $)

    My suggestion is to use the following RegEx: ^0+((10*1)|0*)*0+$