Flat All Units

Download as pdf or txt
Download as pdf or txt
You are on page 1of 82

UNIT-1

Introduction
Symbols, Alphabets, Strings, Languages, operations on Strings and Languages, Finite State
Machine, Definitions, Model representation of a Finite Automata, Acceptance of Strings and
Languages, Deterministic finite automaton(DFA) and Non-deterministic Finite
Automaton(NFA),Transition diagrams and Language recognizers.(Proofs Not Required)

Symbols: Symbol is the smallest building block, which can be any alphabet, letter or any picture.
Ex: a,b,c, …, 0,1, 2, … [Formal language does not deal with the “meaning” of the symbols.]
A finite state machine (sometimes called a finite state automaton) is a computation model that can
be implemented with hardware or software and can be used to simulate sequential logic and some
computer programs. Finite state automata generate regular languages. Finite state machines can
be used to model problems in many fields including mathematics, artificial intelligence, games, and
linguistics.

Model representation of a Finite Automata


Finite Automata (FA) is the simplest machine to recognize patterns. The finite automata (FA) or finite
state machine (FSM) is an abstract machine which has five elements or tuple. It has a set of states
and rules for moving from one state to another but it depends upon the applied input symbol.
Basically it is an abstract model of digital computer. Following figure shows some essential features
of a general automation.

The machine has


 Input tape in which input is placed occupying one character in each cell.
 The tape head reads the symbol from the input tape.
 The finite control is always one of internal states which decide what will be the next state
after reading the input by the tape header.
There are two types of finite state machines (FSMs):
 deterministic finite state machines, called deterministic finite automata (DFA)
 non-deterministic finite state machines, called non-deterministic finite automata (NFA)
For example, below DFA with Σ = {0, 1} accepts all
strings ending with 0.

By definition, DFA recognize, or accept, regular


languages, and a language is regular if a
deterministic finite automaton accepts it.

FSMs are usually taught using languages made up of


binary strings that follow a particular pattern. Both regular and non-regular languages can be
made out of binary strings.
Example of a binary string language is:
L: the language of all strings that have a 0 as the first character.
In this language, 001, 010, 0, and 01111 are valid strings (along with many others), but strings
like 111, 10000, 1, and 11001100 (along with many others) are not in this language.
a) Number of characters is even

b) Number of ones is even


Input is a string over Σ= {0, 1}.
Accept: all strings in which the number of ones is even.

c) Number of characters is divisible by 3


Input: Σ = {0}.
Accept: all strings in which the number of characters is divisible by 3.
d) Number of zero and ones is always within two of each other
Input is a string over Σ = {0, 1}.
Accept: all strings in which the difference between the number of ones and zeros in any prefix of the string
is in the range −2, . . . , 2.
For example, the language contains , 0, 001, and 1101. You even have an extended sequence of one
character e.g. 001111, but it depends what preceded it. So 111100 isn’t in the language.

The language of this DFA is

e) The input is strings over Σ = {0, 1}. Accept: all strings of the form 00w, where w contains an even
number of ones.
Example 3.3: Let Σ= {a, b} and consider the following DFA M, whose language L(M) contains strings
consisting of one or more a’s followed by one or more b’s.

Tables and state diagrams are most useful for small automata. Formulas are helpful for summarizing a
group of transitions that fit a common pattern. They are also helpful for describing algorithms that modify
automatas.
Example
a) Draw a DFA to accept string of 0’s and 1’s ending with the string 011.
L= {011, 0011, 1011, 00011, 01011, 10011, 11011, ...},

b) Obtain a DFA to accept strings of a’s and b’s having a sub string aa

Example 3.4:
Example: construct a DFA strings ending with 100
Non Deterministic Automata
NFA is a mathematical model in which there can be more than one transitions with the same input to more
than one distinct states.
Definition:
A nondeterministic finite automaton is a 5 - tupple (Q, ,  , s, F ), where
Q is a finite set called the states,
 is a finite set called the alphabet,
 : Q    2Q is the transition function,
s  Q is the start state, and
F  Q is the set of accept/final states. (there can be multiple states)
Note: For any alphabet ∑ we write
The formal definition of an NFA is similar to that of a DFA. Both have states, an
alphabet, transition function, one start state, and a collection of accept states
Differ in one essential way: the transition functions:

Example:
1. Q  {q1 , q2 , q3 , q4 },
2.   {0,1}
0 1 
q1 {q1} {q1 , q2 } 
3.  is given as q2 {q3 }  {q3 }
q3  {q4 } 
q4 {q4 } {q4 } 
4. q1 is the start state.
5. F  {q4 }.

Example: What does this NFA accept?

Ans:
It accepts any binary string that contains 00 or 11 as a
substring.

Ans: Accepts all binary strings where the last symbol is 0 or that contain only 1’s.
Computation performed by an NFA

Condition 1 says that the machine starts its computation in the start state
Condition 2 says that state ri+1 is one of the allowable new states when is in state ri and reads yi+1. Note that

is a set
Condition 3 says that the machine accepts the input if the last state is in the accept state set.
An important property: Any NFA N can be converted to an equivalent NFA that has a single accept
state

Short Questions
1. Define: (i) Finite Automaton(FA) (ii)Transition diagram
2. What are the components of Finite automaton model?
3. What are the applications of automata theory?
4. How to check acceptance of string by finite automata?
5. What is a string? Write about concatenation of two strings?
6. What is Kleene Closure and Positive Closure?
7. Differentiate NFA and DFA
8. Explain transition diagram, transition table with example.
9. Define transition function of DFA.
10. Define ε –transitions.
11. Design DFA to accept strings with ‘c’ and ‘d’ such that number of d’s are divisible by 4.
12. Design DFA for the following over {a, b}
i) All strings containing not more than three a’s.
ii) All strings that has at least two occurrences of b between any two occurrences of a.
13. a) Design DFA for the following over {a,b}.
i) All strings containing not more than three a’s.
ii) All strings that has at least two occurrences of b between any two occurrences of a.
b) Construct a DFA accepting the set of all strings ending with 00?
14. Construct a DFA accepting the language
15. Construct a DFA accepting the language has neither aa nor bb as substring
16. a) What is NFA? Explain the transitions of NFA?
b) Construct an NFA that accepts the set of all strings over
{0,1} that start with 0 or 1 and end with 10 or 01.
c) Construct a DFA equivalent to the NFA given below

17. Construct DFA, which accepts set of all strings over {0, 1} which interpreted as binary number
is divisible by 2.
Ans: Consider the following inputs, {0, 01, 10, 11, 100, 101, 110........}
The state transition diagram of the language will be like:

In this DFA there are two states q0 and q1 and the input is strings of {0, 1} which is interpreted as
binary number.
The state q0 is final state and q1 is non-final state. State q0 will be representing all the numbers that are
divisible by 2, that is {0, 10, 100, 110…..}.
State q1 will be representing all the numbers that are not divisible by 2, that is {01, 11, 101, ……}.

Construct DFA, which accepts set of all strings over {0, 1} which interpreted as binary number is
divisible by 3.

Construct DFA, which accepts set of all strings over {0, 1} which interpreted as binary number is
divisible by 4.
Consider the following inputs, {0, 01, 10, 11, 100, 101, 110........}
The state transition diagram of the language will be like:

In this DFA there are three states q0, q1, q2, q3 and the input is strings of {0, 1} which is interpreted as
binary number. The state q0 is final state and q1, q2, q3 are non-final state.
 State q0 will be representing all the numbers that are divisible by 4, that is {0, 100, 1000…..}.
 State q1 will be representing all the numbers that are not divisible by 4 and give remainder 1 when
divided by 4, that is {01, 101,,……}.
 State q2 will be representing all the numbers that are not divisible by 4 and give remainder 2 when
divided by 4, that is {10, 110, ……}.
 State q4 will be representing all the numbers that are not divisible by 4 and give remainder 3 when
divided by 4, that is {11, 111, ……}.
The above automata will accept set of all strings over {0, 1} which when interpreted as binary number is
divisible by 4.
 State Whenever the number is not divisible by 4 and gives remainder of 1 then it will go to state q1.
 State Whenever the number is not divisible by 4 and gives remainder of 2 then it will go to state q2.
 State Whenever the number is not divisible by 4 and gives remainder of 3 then it will go to state q3.
 State Whenever the number is divisible by 4, then it will go to state q0 or if it was initially in q0 then
it will accept it.

Divisibility by 2
A number is divisible by 2 if its units place is either 0 or multiple of 2.
In other words, a number is divisible by 2, if the digit at ones place is an even number, that is the number
ends in 0, 2, 4 or 8.

Divisibility by 3
A number is divisible by 3 if the sum of digits is a multiple of 3.
In other words, a number is divisible by 3, if sum of all its digits is divisible by 3.
For example:
79851 is divisible by 3 as the sum of its digits, i.e., 7 + 9 + 8 + 5 + 1 = 30 is divisible by 3.

Divisibility by 4
A number is divisible by 4 if the number formed by its digits in tens and units place is divisible by 4.
In other words, a number is divisible by 4, if the number formed by its last 2 digits is divisible by 4.
Example: 88312 is divisible by 4 because the number formed by its last two digits i.e., 12 is divisible by 4.

Divisibility by 5
A number is divisible by 5 if its units place is 0 or 5.
In other words, a number is divisible by 5, if it ends in 0 or 5.
For example: 75325 is divisible by 5 as its last digit is 5.
UNIT-2

Acceptance of languages, Equivalence of NFA and DFA, NFA to DFA conversion, NFA
with Ɛ-transitions, Significance, Conversion of NFA with Ɛ-transitions to NFA without Ɛ-
transitions, Minimization of finite automata, Equivalence between two DFA’s.

Equivalence of NFA and DFA


 DFAs and NFAs recognize the same class of languages
This equivalence is both surprising and useful
 It is surprising because NFAs appears to have more power than DFA, so we might expect that
NFA recognizes more languages
 It is useful because describing an NFA for a given language sometimes is much easier than
describing a DFA
NFA to DFA Conversion
NFA with Epsilon to DFA conversion
Conversion of NFA with Epsilon to NFA without Epsilon
Equivalence between Two Automata’s
Two automata A and B are said to be equivalent if both accept exactly the same set of input
strings. Formally, if two automata A and B are equivalent then the steps to identify
equivalence:
1. For any pair of states {qi, qj} the transition for input is defined by {qa, qb}

where
The two automata are not equivalent if for a pair {q a, qb} one is INTERMEDIATE state
and the other is FINAL state.
2. If Initial State is Final State of one automaton, then in second automaton also Initial
State must be Final State for them to be equivalent.

Example:
Questions

1. Describe the procedure of converting NFA to DFA with a suitable example..


2. What is the significance of NFA with €- transitions? Explain.
3. Show the equivalence between NFA with and without €- transitions
4. Explain the procedure to test the equivalence of two finite state machines over set of strings
‘S’ with an example.
5. Explain the procedure for converting DFA to NFA.
6. Briefly discuss about Finite Automata with Epsilon- Transitions.
7. Write the Algorithm for minimizing DFA?
8. What is minimal DFA? Write the minimization Algorithm for DFA?
9. Reduce the DFA given below

10. Convert the following NFA-€ to NFA


UNIT-3
Regular Languages: Regular sets, Regular expressions, Operations and applications of regular
expressions, Identity rules, Inter-Conversion of a given Regular Expression and Finite Automaton,
Pumping Lemma for Regular Languages (Proofs Not Required)
Grammar Formalism: Regular grammars, Right linear and left linear grammars, Conversion
from left linear to right linear grammars, Equivalence of regular grammar and finite automata,
Inter-conversion. (Proofs Not Required)
https://www.sanfoundry.com/automata-theory-mcqs-regular-language-expression-2/
Regular Language
A language is regular if and only if it can be obtained from finite languages by applying the three
operators ∪ (Union), · (Concatenation), * (Star) a finite number of times.

Regular Set
Any set that denotes the value of the Regular Expression is called a "Regular Set".
Regular sets have various properties:
Property 1) The union of two regular sets is also a regular set
Let there be two regular expressions RE1 = 0(00)* and RE2 = (00)*
L1= {0, 000, 00000,..} (odd length strings without Null)
L2={ε, 00, 0000, 000000,.......} (even length strings with Null)
L1∪ L2 = {ε, 0, 00, 000, 0000, 00000, 000000,.......} (all possible lengths strings with Null)
RE(L1∪L2)= 0* (this is also a regular expression or a regular set)

Property 2) The intersection of two regular set is also a regular set.


Let there be two regular expressions RE1 = 0(0*) and RE2 = (00)*
L1= {0,00, 000, 0000, ....} (odd length strings without Null)
L2= {ε, 00, 0000, 000000,.......} (even length strings with Null)
L1∩L2= {00, 0000, 000000,.......} (even length strings without Null)
RE(L1∩L2)=00(00)*(this is a regular set or regular expression also).

Property 3) The complement of a regular set is also regular set.


Let there be a regular expression − RE = (00)*
L = {ε, 00, 0000, 000000, .......} (even length strings with Null)
Complement of L is set of all the strings that is not included in L.
L' = {0, 000, 00000, .....} (odd length strings without Null)
RE (L') = 0(00)* this denotes a regular set or regular expression in itself.

Property 4) The difference of two regular set is also a regular set.


Let there be two regular expressions − RE1 = 0 (0*) and RE2 = (00)*
L1 = {0, 00, 000, 0000, ....} (all possible lengths strings without Null)
L2 = { ε, 00, 0000, 000000,.......} (even length strings with Null)
L1 – L2 = {0, 000, 00000, 0000000, ....} (all odd lengths strings without Null)
RE (L1 – L2) = 0 (00)*(this is also a regular set or a regular expression in itself)

Property 5) The reversal of a regular set is also a regular set.


If L is a regular set: Let, L = {ab, ba, bb, ba}
RE (L) = b1 + 1b +bb + ba
LR = {ba, ab, bb, ab}
RE(LR)= ab + ba + bb + bathis is the reverse and also a regular set or a regular expression in itself
Property 6) The closure of a regular set or an expression is also a regular set.
If L = {0, 000, 00000, .......} (all odd length strings without Null)
i.e., RE (L) = 0 (00)*
L* = {0, 00, 000, 0000 ,00000,……………} ( all lengths strings without Null)
RE (L*) = 0 (0)*(this is also a regular set or a regular expression)

Property 7) The concatenation of two regular sets is also a regular set.


Let RE1 = (a+1)*a and RE2 = a1(a+1)*
Here, L1 = {a, aa, 1a, aaa, a1a, ......} (Set of strings ending in a)
and L2 = {a1, a1a,a11,.....} (Set of strings beginning with a1)
Then, L1 L2 = {aa1, aa1a, aa11, aaa1, aa1a, aaa11, 1aa1, 1aa1a,.............}
Set of strings containing aa1 as a substring which can be denoted by an RE − (a + 1)*aa1(a + 1)*

Regular Expressions
Regular Expressions (RE) are used for representing certain sets of strings in an algebraic fashion.
Definitions:
1. ε (epsilon) is a RE indicates the language containing an empty string. L(ε) = {ε}
2. φ (phi) is a RE denoting an empty language. L(φ) = { }
3. a is a RE where L = {a}
4. If R is a RE denoting the language LR and S is a RE denoting the language LS, then
a) R+S is a RE corresponding to the language LR∪ LS
b) R•S is a RE corresponding to the language LR• LS
c) R* is a RE corresponding to the language LR*
Identity rules
Let r, r1, r2, and r3 be any regular expressions then the identity rules are as follows:

Regular expression (RE) can be converted into DFA by the following methods:
a) Thompson’s subset construction
• Given regular expression is converted into NFA
• Resultant NFA is converted into DFA
b) Direct Method
• In direct method, given regular expression is converted directly into DFA.
------------------------------------------------------------------------------------------------------------

RE to NFA RE to DFA
Conversion of Regular Expression to Finite Automata
Designing Finite Automata from Regular Expression

1.

2.

3.

4.

5.

6. The regular expression for even number of a’s is (b|ab*ab*)*. We can construct a finite
automata as

7. The regular expression for strings with ‘ab’ as substring is (a|b)*ab(a|b)*. We can construct
finite automata as
8. The regular expression for strings with count of a divisible by 3 is {a 3n | n >= 0}. We can
construct automata as

9. The regular expression for binary numbers which are divisible by three is (0|1(01*0)*1)*. We
can construct automata as

Pumping Lemma
Purpose:
1. It is used to prove that a Language is Not Regular
2. It cannot be used to prove that a language is Regular

All strings in the language can be repeated --- PUMPED--- if they are at least as long as a certain
length. The pumping Length (P)
Assignment
UNIT-4
Context Free Grammars: Context free grammars and languages, Derivation trees, Left most and
Right most derivation of strings and sentential forms Ambiguity, left recursion and left factoring in
context free grammars, Minimization of context free grammars, Normal forms for context free
grammars, Chomsky normal form, Grei-bach normal form, Pumping Lemma for Context Free
Languages (Proofs Not Required).
Pushdown Automata: Definition, Graphical notation, Acceptance of context free language,
Acceptance by final state and empty state and its equivalence, Equivalence of context free grammars
and pushdown automata. Definition of Context Sensitive Grammar (CFG) (Proofs Not Required)
Context Free Grammars (CFG)
Grammar: It contains set of rules to construct the sentence in a language.
Context Free means there is a single variable on the left side of each grammar rule.
In formal languages theory, a Context Free Language (CFL) is a language generated by some Context Free
Grammar (CFG).
The set of all CFL is identical to the set of languages accepted by Pushdown Automata (PDA).

Example CFG:
<sentence> –> <noun-phrase> <verb-phrase> (1)
<noun-phrase> –> <proper-noun> (2)
<noun-phrase> –> <determiner> <common-noun> (3)
<proper-noun> –> John (4)
<proper-noun> –> Jill (5)
<common-noun> –> car (6)
<common-noun> –> hamburger (7)
<determiner> –> a (8)
<determiner> –> the (9)
<verb-phrase> –> <verb> <adverb> (10)
<verb-phrase> –> <verb> (11)
<verb> –> drives (12)
<verb> –> eats (13)
<adverb> –> slowly (14)
<adverb> –> frequently (15)

Example Derivation:
<sentence> => <noun-phrase> <verb-phrase> by (1)
=> <proper-noun> <verb-phrase> by (2)
=> Jill <verb-phrase> by (5)
=> Jill <verb> <adverb> by (10)
=> Jill drives <adverb> by (12)
=> Jill drives frequently by (15)

Informally, a CFG is a set of rules for deriving (or generating) strings (or sentences) in a language.

Informally, a CFG consists of:


– A set of replacement rules, each having a Left-Hand Side (LHS) and a Right-Hand Side (RHS).
– Two types of symbols; variables and terminals.
– LHS of each rule is a single variable (no terminals).
– RHS of each rule is a string of zero or more variables and terminals.
– A string consists of only terminals.
Formally, a Context-Free Grammar (CFG) is a 4-tuple: G = (V, T, P, S)
V - finite set of variables or non-terminals (A, B, C,… are variables)
T - finite set of terminals (a, b, c,… are terminals)
P - finite set of productions, each of the form A –> α, where A V and α (V  T)* [Note: α may be ε]
S - starting non-terminal (S is in V)

Example-1: For generating a language that generates equal number of a’s and b’s in the form anbn | n≥0,
the CFG will be defined as G = ({S, A}, {a,b}, S –> aAb, A –>aAb| ε, S) //Remember: G = (V, T, P, S)
Solution:

Note that G “generates” the language {anbn | n≥0}

Derivation Tree
A Derivation Tree or Parse Tree is an ordered rooted tree that graphically represents the semantic information
of strings derived from a CFG.
Example 3.2: Let G be a context free grammar for which the production rules are given as below:

Derive the string aaabbabbba using the grammar


Solution:
Solution:

QUESTION: Given a grammar, is it possible that every (left/right) derivation for a string is
unique?
Ans: ---------

Ambiguous Grammar
If a context free grammar G has more than one derivation tree for some string w ∈ L(G), it is called
an ambiguous grammar. There exist multiple right-most or left-most derivations for some string generated
from that grammar.

QUESTION: Why do we care about this? Isn’t our goal to determine whether or not w ∈ L(G)?
Parse trees give meaning to the string with respect to the grammar.
For example: a − b ∗ c means that a − (b ∗ c) (or) (a − b) ∗ c.
• How can we fix this?
Ans: Precedence Rule and Associativity Rule.
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner

You might also like