Full Transcript

University of Science and Technology Faculty of Computer Science and Information Technology Computer Science Department Semester 7 Theory of Computation Lecture (6) Instructor: Prof. Noureldien Abdelrahman Date 7-12-2022 **Regular Expression** The language accepted by finite automata can be...

University of Science and Technology Faculty of Computer Science and Information Technology Computer Science Department Semester 7 Theory of Computation Lecture (6) Instructor: Prof. Noureldien Abdelrahman Date 7-12-2022 **Regular Expression** The language accepted by finite automata can be easily described by simple expressions called Regular Expressions. It is the most effective way to represent any language. The languages accepted by some regular expression are referred to as Regular languages. A regular expression can also be described as a sequence of pattern that defines a string. Regular expressions are used to match character combinations in strings. String searching algorithm used this pattern to find the operations on a string. **[Symbols Used:]** In a regular expression, x\* means zero or more occurrence of x. It can generate {€, x, xx, xxx, xxxx, \.....} In a regular expression, x^+^ means one or more occurrence of x. It can generate {x, xx, xxx, xxxx, \.....} **[Operations on Regular Language]** The various operations on regular language are: **Union:** If L and M are two regular languages then their union L U M is also a regular language. 1. L U M = {s \| s is in L or s is in M}   **Intersection:** If L and M are two regular languages then their intersection is also a regular language. 2\.  L ⋂ M = {st \| s is in L and t is in M}   **Kleen closure:** If L is a regular language then its Kleen closure L1\* will also be a regular language. 3\.  L\* = Zero or more occurrence of language L.   Example 1: Write the regular expression for the language accepting all combinations of a\'s, over the set ∑ = {a} **Solution:** All combinations of a\'s means a may be zero, single, double and so on. If a is appearing zero times, that means a null string. That is we expect the set of {ε, a, aa, aaa, \....}. So we give a regular expression for this as: R = a\*  , That is Kleen closure of a. Example 2: Write the regular expression for the language accepting all combinations of a\'s except the null string, over the set ∑ = {a} **Solution:** The regular expression has to be built for the language L = {a, aa, aaa, \....}   This set indicates that there is no null string. So we can denote regular expression as: Example 3: Write the regular expression for the language accepting all the string containing any number of a\'s and b\'s. **Solution:** The regular expression will be: R = (a + b)\*   This will give the set as L = {ε, a, aa, b, bb, ab, ba, aba, bab, \.....}, any combination of a and b. The (a + b)\* shows any combination with a and b except the null string. **Examples of Regular Expression** Example 1: Write the regular expression for the language accepting all the string which are starting with 1 and ending with 0, over ∑ = {0, 1}. **Solution:** In a regular expression, the first symbol should be 1, and the last symbol should be 0. The r.e. is as follows: R = 1 (0+1)\* 0   Example 2: Write the regular expression for the language starting and ending with **a** and having any combination of b\'s in between. **Solution:** The regular expression will be: R = a b\* a   **Example 3:** Write the regular expression for the language starting with **a** but not having consecutive b\'s. **Solution:** The regular expression has to be built for the language: 1. L = {a, aba, aab, aba, aaa, abab, \.....}   The regular expression for the above language is: R = {a + ab}\*   **Example 4:** Write the regular expression for the language accepting all the string in which any number of **a**\'s is followed by any number of **b**\'s is followed by any number of **c**\'s. **Solution:** As we know, any number of a\'s means a\* any number of b\'s means b\*, any number of c\'s means c\*. Since as given in problem statement, b\'s appear after a\'s and c\'s appear after b\'s. So the regular expression could be: R = a\* b\* c\*   **Example 5:** Write the regular expression for the language over ∑ = {0} having **even** length of the string. **Solution:** The regular expression has to be built for the language: L = {ε, 00, 0000, 000000, \...\...}   The regular expression for the above language is: R = (00)\*   **Example 6:** Describe the language denoted by following regular expression R = (b\* (aaa)\* b\*)\*   **Solution:** The language can be predicted from the regular expression by finding the meaning of it. We will first split the regular expression as: R = (any combination of b\'s) (aaa)\* (any combination of b\'s) L = {The language consists of the string in which a\'s appear triples, there is no restriction on the number of b\'s} **Example 7:** Write the regular expression for the language L over ∑ = {0, 1} such that all the string do not contain the substring 01. **Solution:** The Language is as follows: L = {ε, 0, 1, 00, 11, 10, 100, \.....}   The regular expression for the above language is as follows: R = (1\* 0\*)   **Example 8:** Write the regular expression for the language containing the string over {0, 1} in which there are **at least two occurrences of 1\'s** between any **two occurrences of 0\'s**. **Solution:** At least two 1\'s between two occurrences of 0\'s can be denoted by (0111\*0)\*. Similarly, if there is no occurrence of 0\'s, then any number of 1\'s are also allowed. Hence the regular expression for required language is: R = (1 + (0111\*0))\*   **Example 9:** Write the regular expression for the language containing the string in which every 0 is immediately followed by 11. **Solution:** The regular expectation will be: R = (011 + 1)\*   **Converting Regular Expressions to NFA** EXAMPLE **1.56** We convert the regular expression (ab Ua)\*to an NFA in a sequence of stages. We build up from the smallest subexpressions to larger subexpressions until we have an NFA for the original expression, as shown in the following diagram. Note that this procedure generally doesn't give the NFA with the fewest states. EXAMPLE **1.58** In Figure 1.59, we convert the regular expression (a U b)\*aba to an NFA.

Use Quizgecko on...
Browser
Browser