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

BCSE304L

Theory of Computation
Lecture 5
Dr. Saritha Murali
(SCOPE, VIT Vellore)
11-01-2024
Recap
• Grammars
• Derivations
• Sentential Forms
• Language generated from a grammar
Types of grammars
Chomsky Hierarchy
Grammars based on the type of production rules:
Language Grammar Machine Example

Regular grammars
Finite-state
Type 3 Regular languages • Right-linear grammars a*
automata
• Left-linear grammars

Push-down
Type 2 Context-free languages Context-free grammars anbn
automata
Linear-bound
Type 1 Context-sensitive languages Context-sensitive grammars an bn cn
automata

Recursive languages any


Turing
Type 0 Recursively enumerable Unrestricted grammars computable
machines
languages function

Read as: unrestricted grammars can generate the languages that can be accepted by a Turing machine.
Chomsky Hierarchy
Non-recursively enumerable

Recursively-enumerable

Recursive

Context-sensitive

Context-free

Regular
Type – 0 Unrestricted or Phrase structured
grammar
Productions of the form 𝜶 → 𝜷
𝜶 ∈ (𝑽 + 𝑻)* 𝑽 (𝑽 + 𝑻)*
𝜷 ∈ (𝑽 + 𝑻)∗
• The productions have no restrictions.
• 𝜶 is a string of terminals and non-terminals with at least one non-terminal
and 𝜶 cannot be null.
• 𝜷 is a string of terminals and non-terminals.
Type – 0 Unrestricted or Phrase structured
grammar
• Type-0 grammars include all formal grammars.
• Type-0 grammar languages are recognized by Turing machine.
• The languages generated are recursive or recursively enumerable
languages.
e.g.,
S → Aa
SBb→ abc
a → Acb (not type-0 as there is no variable/Non-Terminal on LHS)
aB → BabcS
Type – 1 Context-sensitive grammar
Productions of the form 𝜶 → 𝜷
|𝜶| ≤ |𝜷|
𝜶 ∈ (𝑽 + 𝑻)* 𝑽 (𝑽 + 𝑻)*
𝜷 ∈ (𝑽 + 𝑻)∗

e.g.,
AB → CDB
AB → CdEB
ABcd → abCDBcd
A → bcA
B→b
Type – 1 Context-sensitive grammar
• Type 1 grammar should be Type 0
• Type 1 grammars generate context-sensitive languages
• The language generated by the grammar are recognized by the Linear
Bound Automata.
Type – 2 Context-free grammar
Productions of the form 𝜶 → 𝜷
|𝜶| ≤ |𝜷|
𝜶 ∈ 𝑽 and |𝜶| = 𝟏
𝜷 ∈ (𝑽 + 𝑻)∗

• left-hand side of each production rule consists of only a single nonterminal


• RHS is a string of terminals and non-terminals
e.g.,
A → aBc
S → Aa
Sb → Aa (not type-2 as left hand side has a variable and a terminal)
B → bAa
Type – 2 Context-free grammar
• Type 2 grammar should be Type 1
• Type-2 grammars generate the context-free languages.
• The language generated by the grammar is recognized by a
Push down Automata.
Type – 3 Regular grammar
Right linear form:
Productions of the form A → 𝜶B / 𝜷
A, B ∈ 𝑽
𝜶, 𝜷 ∈ T *

Left linear form:


Productions of the form A → B 𝜶 / 𝜷
A, B ∈ 𝑽
𝜶, 𝜷 ∈ T *

• They have a single non-terminal on the left-hand side


Type – 3 Regular grammar
• A regular grammar is right-linear or left-linear (but not both)
• Right-Linear productions have the form X → aY or X → a
• Left-Linear productions have the form X → Yb or X → b
Type – 3 Regular grammar
• Regular grammar generates regular language.
• These languages can be decided by a finite state automaton.
• Type 3 is most restricted form of grammar.
e.g.,
A → BAa (not type-3 as right hand side has more than one
i.e 2 variables which is not allowed in regular grammar )
Ab → aB (not type-3 as LHS has symbols other than a variable)
Task 3
1. Which of the following strings is not generated by the following grammar?
S -> SaSbS|e
(A) aabb
(B) abab
(C) aababb
(D) aaabb
2. If G is a grammar with productions S → SaS | aSb | bSa | SS | ∈
where S is the start variable. Which one of the following strings in not generated by
G?
1. abab
2. aaab
3. abbaa
4. babba
3. Consider the CFG with {S,A,B) as the non-terminal alphabet, {a,b) as the
terminal alphabet, S as the start symbol and the following set of production
rules
• S --> aB S --> bA
• B --> b A --> a
• B --> bS A --> aS
• B --> aBB A --> bAA
Which of the following strings is generated by the grammar?
(A) aaaabb
(B) aabbbb
(C) aabbab
(D) abbbba
What is the language generated by this grammar?
4. Construct a grammar that accepts all strings from the language L={w|w=wR and
Σ={a,b}} . i.e., Construct grammar for palindromes over {a, b}
Ans: Language L = {ε, ab, ba, aab, abb, baa, bba, . . .}
S → aSa | bSb | aAb | bAa
A → Aa | Ab | ε

5. Construct a grammar that accepts all strings from the language L={w|w ¹ wR and
Σ={a,b}} . i.e., Construct grammar for non-palindromes over {a, b}

6. Construct a grammar that accepts all strings from the following languages
• L = {w | na(w) = nb(w)} i.e., no. of a is equal to no. of b
• L = {w | na(w)>nb(w)}
• L = {w | na(w) = 2nb(w)}
Finite State Automata (FA)
Design the logic behind an electric bulb.

• States = {nolight, light}, Input = {off, on}


• Finite Automaton.
Design the logic behind a multispeed fan.

• States = {0, 1, 2, 3}
• Input = {↷, ↶}
• Finite Automaton.
Design the logic behind automatic doors

• States = {close, open}, Input = {left, right, neither}


Finite State Automata (FA)
0 1 1 0 0 …

Finite
Control

• One-way, infinite tape, broken into cells


• Read-only tape head.
• Finite control, i.e., finite number of states, and
• A string is placed on the tape, read head is positioned at the left end.
FA will read the string one symbol at a time until all symbols have been read.
The FA will then either accept or reject the string.
Deterministic Finite State Automata (DFA)
A deterministic finite automaton is a 5-tuple M = (Q, Σ, d, q0, F), where
•Q - is a finite set of states
•Σ - is a finite set of input symbols
• q0 Î Q - is the start state (initial state)
•FÍQ - is the set of accept states (final states)
• d:Q´Σ→Q - is the TRANSITION FUNCTION
Q = {q0 , q1 } , Σ = { a, b}

n(Q) = 2 , n(Σ) = 2

A x B = { (x, y) | x Î A , y Î B } - Cartesian Product of A and B

n(A x B) = n(A) . n(B)

Q x Σ = { (q0, a), (q0, b), (q1, a), (q1, b) }

n(Q x Σ) = n(Q). n(Σ) = 2 . 2 = 4

d:Q´Σ→Q
Examples
M = ( {q0 , q1 , q2 , D } , {a, b} , q0 , d , {q2} ) a b
q1 D
where d(q0 , a) = q1 d(q0 , b) = D q0
q1 q2
d(q1 , a) = q1 d(q1 , b) = q2 q1
D q2
d(q2 , a) = D d(q2 , b) = q2 q2
d(D , a) = D d(D , b) = D D D D
a
Transition Table or State Table

q0 a b b
q1 q2
Transition diagram or State diagram
b a
D
a,b
DFA
• In a DFA, for a particular input symbol from an input state, the
machine goes to ONE state only.
• A transition function is defined on every state for every input symbol.
• Also in DFA, null move is not allowed, i.e., DFA cannot change state
without any input character.
• A DFA has exactly one transition from every state on every symbol in the
alphabet.

• Does not have exactly one transition from every state on every symbol:
• Two transitions from q0 on a
• No transition from q1 (on either a or b)
• Hence not a DFA

You might also like