MCS-212 · Block 2 Unit 2

Automata and Languages

Section 3 of 8

Block 2 · Unit 2Automata and Languages11 min · Section 3 of 8

2.3Regular expressions

Covers IGNOU MCS-212 §2.2.3, Ex.3, Ex.4, Ex.6, Ex.7

Many useful languages are infinite, so we cannot list them. A regular expression is a short formula that describes such a language exactly. The same idea, with extra conveniences, is used in every programming language (grep, Python’s re, JavaScript’s /…/) to search and validate text.

r+r^+ is used as a shorthand for rr∗rr^*. Every regular expression rr describes a language L(r)L(r):

ExpressionLanguage LL
∅\emptyset∅\emptyset
λ\lambda{λ}\{\lambda\}
aa{a}\{a\}
rsrsL(r)L(s)L(r)L(s) (product)
r+sr + sL(r)∪L(s)L(r) \cup L(s)
r∗r^*L(r)∗L(r)^*

Precedence: ∗^* is applied first, then concatenation, then ++. So ab∗+cab^* + c means (a(b∗))+c(a(b^*)) + c.

Reading regular expressions

Regular expression over {0,1}\{0, 1\} or {a,b}\{a, b\}Language
(0+1)∗(0 + 1)^*all strings over {0,1}\{0, 1\}
0∗10∗10∗0^*10^*10^*strings with exactly two 1s
(0+1)∗11(0 + 1)^*11strings ending in 1111
ab∗aab^*astrings that start and end with aa with only bb‘s between: aa,aba,abba,…aa, aba, abba, \dots
a∗b∗a^*b^*all aa‘s (if any) come before all bb‘s (if any)
(a+b)∗a(a+b)∗(a + b)^*a(a + b)^*strings containing at least one aa

Key takeaways

  • Regular expressions are built from ∅\emptyset, λ\lambda and symbols with ++, concatenation and ∗^*.
  • Precedence: star, then concatenation, then union.
  • L(r+s)=L(r)∪L(s)L(r + s) = L(r) \cup L(s), L(rs)=L(r)L(s)L(rs) = L(r)L(s), L(r∗)=L(r)∗L(r^*) = L(r)^*.
  • Test every regular expression on strings that should and should not be in the language.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which string belongs to L(0∗10∗10∗)L(0^*10^*10^*)?

Show answer

Answer: B. 00101000010100

The language is strings with exactly two 1s; 00101000010100 has two. Option 1 has four 1s and option 3 only one.

Question 2 · multiple choice

Which regular expression describes all strings over {a,b}\{a, b\} that end in bb?

Show answer

Answer: C. (a+b)∗b(a + b)^*b

Anything followed by a final bb. Option 2 misses strings like babbab; option 4 describes strings that start with bb.

Question 3 · multiple choice

Which string is NOT in L(a∗b∗)L(a^*b^*)?

Show answer

Answer: D. abaaba

In a∗b∗a^*b^* no aa may come after a bb. λ\lambda is in the language (zero aa‘s and zero bb‘s).

Question 4 · multiple choice

What does ab∗+cab^* + c mean, by the precedence rules?

Show answer

Answer: A. (a(b∗))+c(a(b^*)) + c

Star binds tightest, then concatenation, then ++. Option 2 would star the whole abab.

Question 5 · multiple choice

Which expression describes strings over {a,b}\{a, b\} with at least one aa?

Show answer

Answer: D. (a+b)∗a(a+b)∗(a + b)^*a(a + b)^*

Some string, then an aa, then some string. Option 2 also contains bb and λ\lambda, which have no aa.

Question 6 · short answer

Write a regular expression for binary numerals without leading zeros (except 0 itself), and check it on 00, 101101 and 011011.

Show worked answer

0+1(0+1)∗0 + 1(0 + 1)^*. 00 matches the first alternative. 101101 = 11 followed by 01∈L((0+1)∗)01 \in L((0 + 1)^*), so it matches the second. 011011 starts with 0 but is not exactly “0”, so it matches neither alternative — correctly rejected.

↑ ↓ move↵ openTab switch type