0% found this document useful (0 votes)
48 views22 pages

Slide1 New Toc

The document discusses regular expressions and their use in defining patterns of strings over an alphabet. Some key points covered include: - Regular expressions use notations like *, +, | for operations like Kleene closure, concatenation, and union. - Examples show how regular expressions can define languages with certain properties, like containing a specified number of a character, or having a length within a range. - Precedence and associativity rules govern the order in which operations are applied in a regular expression. - Regular expressions provide a concise way to define the set of strings belonging to a formal language.

Uploaded by

gkaurbe21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views22 pages

Slide1 New Toc

The document discusses regular expressions and their use in defining patterns of strings over an alphabet. Some key points covered include: - Regular expressions use notations like *, +, | for operations like Kleene closure, concatenation, and union. - Examples show how regular expressions can define languages with certain properties, like containing a specified number of a character, or having a length within a range. - Precedence and associativity rules govern the order in which operations are applied in a regular expression. - Regular expressions provide a concise way to define the set of strings belonging to a formal language.

Uploaded by

gkaurbe21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UCS701

Theory of Computation

Chapter 1:
Regular Languages
& Finite Automata

Dr. Shashank Sheshar Singh


[email protected]

1/12/2024 Regular Languages & Finite Automata 1


Regular Expression

1/12/2024 Regular Languages & Finite Automata 2


Regular expression
• A regular expression is a sequence of characters that define
a pattern.
• Notational shorthand's
1. One or more occurrences: +
2. Zero or more occurrences: ∗
3. Alphabets: Σ
Regular expression
L = Zero or More Occurrences of a = a*

𝜖
a
aa
aaa Infinite …..
aaaa
aaaaa…..
Regular expression
L = One or More Occurrences of a = a+

a
aa
aaa Infinite …..
aaaa
aaaaa…..
Precedence and associativity of operators
Operator Precedence Associative
Kleene * 1 left
Concatenation 2 left
Union | 3 left
Regular expression examples
1. 0 or 1
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟏 𝑹. 𝑬. = 𝟎 | 𝟏

2. 0 or 11 or 111
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟏𝟏, 𝟏𝟏𝟏 𝑹. 𝑬. = 𝟎 𝟏𝟏 𝟏𝟏𝟏
3. String having zero or more a.

𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝜖, 𝒂, 𝒂𝒂, 𝒂𝒂𝒂, 𝒂𝒂𝒂𝒂 … . . 𝑹. 𝑬. = 𝒂
4. String having one or more a.
+
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂, 𝒂𝒂, 𝒂𝒂𝒂, 𝒂𝒂𝒂𝒂 … . . 𝑹. 𝑬. = 𝒂
5. Regular expression over Σ = {𝑎, 𝑏, 𝑐} that represent all string of length
3.
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒃𝒄, 𝒃𝒄𝒂, 𝒃𝒃𝒃, 𝒄𝒂𝒃, 𝒂𝒃𝒂 … . 𝑹. 𝑬. = 𝒂 𝒃 𝒄 𝒂 𝒃 𝒄 (𝒂 𝒃 𝒄)
6. All binary string.
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟏𝟏, 𝟏𝟎𝟏, 𝟏𝟎𝟏𝟎𝟏, 𝟏𝟏𝟏𝟏 … 𝑹. 𝑬. = (𝟎 | 𝟏) +
Regular expression examples
7. 0 or more occurrence of either a or b or both
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝜖, 𝒂, 𝒂𝒂, 𝒂𝒃𝒂𝒃, 𝒃𝒂𝒃 … 𝑹. 𝑬. = (𝒂 | 𝒃) ∗
8. 1 or more occurrence of either a or b or both
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂, 𝒂𝒂, 𝒂𝒃𝒂𝒃, 𝒃𝒂𝒃, 𝒃𝒃𝒃𝒂𝒂𝒂 … 𝑹. 𝑬. = (𝒂 | 𝒃) +
9. Binary no. ends with 0
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟏𝟎, 𝟏𝟎𝟎, 𝟏𝟎𝟏𝟎, 𝟏𝟏𝟏𝟏𝟎 … 𝑹. 𝑬. = (𝟎 | 𝟏) ∗ 𝟎

10. Binary no. ends with 1


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏, 𝟏𝟎𝟏, 𝟏𝟎𝟎𝟏, 𝟏𝟎𝟏𝟎𝟏, … 𝑹. 𝑬. = (𝟎 | 𝟏) ∗ 𝟏
11. Binary no. starts and ends with 1
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏𝟏, 𝟏𝟎𝟏, 𝟏𝟎𝟎𝟏, 𝟏𝟎𝟏𝟎𝟏, … 𝑹. 𝑬. = 𝟏 (𝟎 | 𝟏) ∗ 𝟏
12. String starts and ends with same character
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎, 𝟏𝟎𝟏, 𝒂𝒃𝒂, 𝒃𝒂𝒂𝒃 … 𝑹. 𝑬. = 𝟏 (𝟎 | 𝟏) ∗ 𝟏 𝐨𝐫 𝟎 (𝟎 | 𝟏) ∗ 𝟎
∗ ∗
𝒂 (𝒂 | 𝒃) 𝒂 𝐨𝐫 𝒃 (𝒂 | 𝒃) 𝒃
Regular expression examples
13. All string of a and b starting with a
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂, 𝒂𝒃, 𝒂𝒂𝒃, 𝒂𝒃𝒃… 𝑹. 𝑬. = 𝒂(𝒂 | 𝒃) ∗

14. String of 0 and 1 ends with 00


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎, 𝟏𝟎𝟎, 𝟎𝟎𝟎, 𝟏𝟎𝟎𝟎, 𝟏𝟏𝟎𝟎… 𝑹. 𝑬. = (𝟎 | 𝟏) ∗ 𝟎𝟎

15. String ends with abb


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒃𝒃, 𝒃𝒂𝒃𝒃, 𝒂𝒃𝒂𝒃𝒃… 𝑹. 𝑬. = (𝒂 | 𝒃) ∗ 𝒂𝒃𝒃

16. String starts with 1 and ends with 0


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏𝟎, 𝟏𝟎𝟎, 𝟏𝟏𝟎, 𝟏𝟎𝟎𝟎, 𝟏𝟏𝟎𝟎… 𝑹. 𝑬. = 𝟏(𝟎 | 𝟏) ∗ 𝟎

17. All binary string with at least 3 characters and 3rd character should be
zero 𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟎, 𝟏𝟎𝟎, 𝟏𝟏𝟎𝟎, 𝟏𝟎𝟎𝟏… 𝑹. 𝑬. = 𝟎 𝟏 𝟎 𝟏 𝟎(𝟎 | 𝟏) ∗
18. Language which consist of exactly two b’s over the set Σ = {𝑎, 𝑏}
∗ ∗ ∗
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒃𝒃, 𝒃𝒂𝒃, 𝒂𝒂𝒃𝒃, 𝒂𝒃𝒃𝒂… 𝑹. 𝑬. = 𝒂 𝒃 𝒂 𝒃 𝒂
Regular expression examples
19. The language with Σ = {𝑎, 𝑏} such that 3rd character from right end of
the string is always
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒂𝒂, 𝒂𝒃𝒂, 𝒂𝒂𝒃𝒂, 𝒂𝒃𝒃… 𝑹. 𝑬. = (𝒂 | 𝒃) ∗ 𝒂(𝒂|𝒃)(𝒂|𝒃)

20. Any no. of 𝑎 followed by any no. of 𝑏 followed by any no. of 𝑐


∗ ∗ ∗
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝜖, 𝒂𝒃𝒄, 𝒂𝒂𝒃𝒃𝒄𝒄, 𝒂𝒂𝒃𝒄, 𝒂𝒃𝒃… 𝑹. 𝑬. = 𝒂 𝒃 𝒄

21. String should contain at least three 1


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏𝟏𝟏, 𝟎𝟏𝟏𝟎𝟏, 𝟎𝟏𝟎𝟏𝟏𝟏𝟎…. 𝑹. 𝑬. = (𝟎|𝟏)∗ 𝟏 (𝟎|𝟏)∗ 𝟏 (𝟎|𝟏)∗ 𝟏 (𝟎|𝟏)∗
22. String should contain exactly two 1
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏𝟏, 𝟎𝟏𝟎𝟏, 𝟏𝟏𝟎𝟎, 𝟎𝟏𝟎𝟎𝟏𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = 𝟎∗ 𝟏𝟎∗ 𝟏𝟎∗
23. Length of string should be at least 1 and at most 3
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟏, 𝟏𝟏, 𝟎𝟏, 𝟏𝟏𝟏, 𝟎𝟏𝟎, 𝟏𝟎𝟎…. 𝑹. 𝑬. = 𝟎|𝟏 𝟎|𝟏 𝟎|𝟏 𝟎|𝟏 𝟎|𝟏 𝟎|𝟏
24. No. of zero should be multiple of 3
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟎, 𝟎𝟏𝟎𝟏𝟎𝟏, 𝟏𝟏𝟎𝟏𝟎𝟎, 𝟎𝟎𝟎𝟎𝟎𝟎, 𝟏𝟎𝟎𝟎𝟏𝟎𝟎𝟏𝟎…. 𝑹. 𝑬. = (𝟏∗ 𝟎𝟏∗ 𝟎𝟏∗ 𝟎𝟏∗ )∗
Regular expression examples
25. The language with Σ = {𝑎, 𝑏, 𝑐} where 𝑎 should be multiple of 3
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒂𝒂, 𝒃𝒂𝒂𝒂, 𝒃𝒂𝒄𝒂𝒃𝒂, 𝒂𝒂𝒂𝒂𝒂𝒂. . 𝑹. 𝑬. = ( 𝒃|𝒄 ∗ 𝒂 𝒃|𝒄 ∗ 𝒂 𝒃|𝒄 ∗ 𝒂 𝒃|𝒄 ∗ )∗

26. Even no. of 0


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎, 𝟎𝟏𝟎𝟏, 𝟎𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = (𝟏∗ 𝟎𝟏∗ 𝟎𝟏∗ )∗

27. String should have odd length


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟎𝟏𝟎, 𝟏𝟏𝟎, 𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎…. 𝑹. 𝑬. = 𝟎|𝟏 ( 𝟎 𝟏 (𝟎|𝟏))∗

28. String should have even length


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎, 𝟎𝟏𝟎𝟏, 𝟎𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = ( 𝟎 𝟏 (𝟎|𝟏))∗

29. String start with 0 and has odd length


𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎, 𝟎𝟏𝟎, 𝟎𝟏𝟎, 𝟎𝟎𝟎, 𝟎𝟎𝟎𝟏𝟎…. 𝑹. 𝑬. = 𝟎 ( 𝟎 𝟏 (𝟎|𝟏))∗
30. String start with 1 and has even length
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟏𝟎, 𝟏𝟏𝟎𝟎, 𝟏𝟎𝟎𝟎, 𝟏𝟎𝟎𝟏𝟎𝟎…. 𝑹. 𝑬. = 𝟏(𝟎|𝟏)( 𝟎 𝟏 (𝟎|𝟏))∗
Regular expression examples
31. All string begins or ends with 00 or 11

𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟏𝟎𝟏, 𝟏𝟎𝟏𝟎𝟎, 𝟏𝟏𝟎, 𝟎𝟏𝟎𝟏𝟏 … 𝑹. 𝑬. = (𝟎𝟎|𝟏𝟏)(𝟎 | 𝟏) ∗ | 𝟎 𝟏 (𝟎𝟎|𝟏𝟏)
32. Language of all string containing both 11 and 00 as substring
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟎𝟏𝟏, 𝟏𝟏𝟎𝟎, 𝟏𝟎𝟎𝟏𝟏𝟎, 𝟎𝟏𝟎𝟎𝟏𝟏 …
𝑹. 𝑬. = ((𝟎|𝟏)∗ 𝟎𝟎(𝟎|𝟏)∗ 𝟏𝟏(𝟎|𝟏)∗ ) | ((𝟎|𝟏)∗ 𝟏𝟏(𝟎|𝟏)∗ 𝟎𝟎(𝟎|𝟏)∗ )
33. String ending with 1 and not contain 00
+
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝟎𝟏𝟏, 𝟏𝟏𝟎𝟏, 𝟏𝟎𝟏𝟏 … . 𝑹. 𝑬. = 𝟏 𝟎𝟏
34. Language of C identifier
𝑺𝒕𝒓𝒊𝒏𝒈𝒔: 𝒂𝒓𝒆𝒂, 𝒊, 𝒓𝒆𝒅𝒊𝒐𝒖𝒔, 𝒈𝒓𝒂𝒅𝒆𝟏 … . 𝑹. 𝑬. = (_ + 𝑳)(_ + 𝑳 + 𝑫)∗
𝒘𝒉𝒆𝒓𝒆 𝑳 𝒊𝒔 𝑳𝒆𝒕𝒕𝒆𝒓 & 𝐃 𝐢𝐬 𝐝𝐢𝐠𝐢𝐭
Finite Automata

1/12/2024 Regular Languages & Finite Automata 13


Finite Automata
• A finite automaton, or finite state machine is a 5-tuple
(𝑄, Σ, 𝑞0 , 𝐴, 𝛿) where
• 𝑄 is finite set of states;
• Σ is finite alphabet of input symbols;
• 𝑞0 𝜖 𝑄 (initial state);
• 𝐴 ⊆ 𝑄 (the set of accepting states);
• 𝛿 is a function from 𝑄 × Σ 𝑡𝑜 𝑄 (the transition function).
• For any element 𝑞 of 𝑄 and any symbol 𝑎 𝜖 Σ, we interpret 𝛿(𝑞, 𝑎)
as the state to which the FA moves, if it is in state 𝑞 and receives
the input 𝑎.
Example: Finite Automata
• 𝑀 = (𝑄, Σ, 𝑞0 , 𝐴, 𝛿)
• 𝑄 = {𝑞0 , 𝑞1 , 𝑞2 } δ Input
• Σ = {0,1} State 𝟎 𝟏
• 𝑞0 = 𝑞0 𝑞0 𝑞1 𝑞0
• 𝐴 = {𝑞2 } 𝑞1 𝑞2 𝑞0
• 𝛿 is defined as 𝑞2 𝑞2 𝑞0

1 1 0

0 0
𝑞0 𝑞1 𝑞2

1
Applications of FA
• Lexical analysis phase of a compiler.
• Design of digital circuit.
• String matching.
• Communication Protocol for information exchange.
FA Examples
• The string with next to last symbol as 0.
0

1
𝐶
0

0 1
𝐴 𝐵
1
𝐷
0

1
FA Examples
• The strings ending with 10.

0 1

1
𝐴 𝐵 𝐶
1

0
FA Examples
• The string with number of 0’s and number of 1’s are odd

1
odd 0 odd 0
odd 1 1 even 1

0 0 0 0

1
odd 1 even 1
even 0 1 even 0
FA Examples
• The string ending in 10 or 11 1

C
0
1

1
A B 0

0
1

D
0
FA Examples
• The string corresponding to Regular expression {00}*{11}*

0,1
1
B D

0 0 0
0 1

1
A C E
1
FA Examples
• (a+b)*baaa b

a B a
b
b
A C

b b
a a

a
E D

You might also like