0% found this document useful (0 votes)
51 views30 pages

Theory of Computation

The document provides an overview of the Theory of Computation, focusing on Context-Free Grammars (CFGs), Normal Forms (Chomsky and Greibach), and their applications in parsing and language recognition. It also discusses Push Down Automata (PDA) and Turing Machines (TM), highlighting their roles in recognizing context-free languages and more complex languages, respectively. The conclusion emphasizes the differences in computational power between PDAs and TMs.

Uploaded by

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

Theory of Computation

The document provides an overview of the Theory of Computation, focusing on Context-Free Grammars (CFGs), Normal Forms (Chomsky and Greibach), and their applications in parsing and language recognition. It also discusses Push Down Automata (PDA) and Turing Machines (TM), highlighting their roles in recognizing context-free languages and more complex languages, respectively. The conclusion emphasizes the differences in computational power between PDAs and TMs.

Uploaded by

pp2898866
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Theory of

Computation

SUBMITTED TO : [Link]
PRASAD
TEAM
INTRODUCTION

BATCH NO :12
SECTION :2C
COURSE NAME :Theory of Computation
COURSE CODE :22CS208
YEAR :2 BATCH MATES
231FA04E88 - [Link] Haritha
231FA04837 - [Link]
231FA04990 - [Link]
231FA04A24 - [Link] Poojitha
Context Free Grammar
A formal grammar called context free grammar (CFG) is used to produce every conceivable string in a given formal
language . Four tuples are used to define the context free grammar G:
G = (V, T, P, S)
G refers to a grammar that consists of sets of various production rules. We use it to generate a language’s strings.
T refers to the terminal symbol’s final set. Lower case letters are used to denote it.
V refers to the nonterminal symbol’s final set. Capital letters are used to denote it.
P refers to a set of production rules that can be used to replace the nonterminal symbols in a string
S refers to the start symbol that is used to derive the string .
NORMAL FORMS
Normal Forms are standardized grammar structures for context-free grammars (CFGs),
simplifying them and making them easier to work with, particularly for parsing
algorithms. The two main normal forms are Chomsky Normal Form (CNF) and Greibach
Normal Form (GNF).

Why Do We Use Normal Forms?


[Link] Parsing: Many parsing algorithms (CYK algorithm, Earley’s parser) work efficiently
when the grammar is in a normal form.
[Link] Redundant Productions: Eliminates useless, nullable, and unit productions, making
the grammar concise.
[Link] Theoretical Properties: Some theorems (like the Pumping Lemma for CFLs) are easier to
prove when the grammar is normalized.
TYPES OF NORMAL FORMS
Chomsky Normal Form (CNF)
•Each production must be of the form:
1.A → BC (where A, B, and C are non-terminals, B and C are not the start symbol)
2.A → a (where 'a' is a terminal)
3.S → ε (only if the language contains ε)
•Important Points:
•Every CFG without ε-productions (except S → ε) and unit productions can be converted to CNF.

Greibach Normal Form (GNF)


•Each production must be of the form:
1.A → aα (where 'a' is a terminal, and α is a sequence of non-terminals)
2. No ε-productions or unit productions.
•Important Points:
•Every CFG without ε-productions can be converted into GNF.
PROBLEM STATEMENT
SOLUTIONS:

A)
GIVEN; L={an bn/n>=1}
ε= {a ,b}
L={ab, aabb,_,_,___}
G=CFG=(V,T,P,S)
V={S}
T={a,b}
P={S->aS/bS/SS/ ε}

CHECK “ abab”
S->SS [S->aS]
S->aSaS [S->aS]
S->abSabS [S->bS]
S->abεabε [S->ε]
S->abab

G=({S}, {a,b}, {S->aS/bS/SS/ ε},{S})


B)
GIVEN; L={ai bj ck}

L={abbccc,_,_,___}

Z=|abbccc|>=3

|6|>=3

Z=abbccc(divide them using u,v,w,x,y)

U=a ,v=b, w=bb, x= ε , y=ccc

Conditions:

|vwx|<=n |bbb|<=n |3|<=3

|vw|>=1 |bb|>=1 |2|>=1

For i=1 (condition : uviwxiy)

= abbbεccc (language is not context free Language)


C)
A grammar is ambiguous if there exists at least one string that
can have more than one leftmost derivation or more than one
parse tree (i.e., more than one way to derive the string using the
grammar rules).
Let the grammar be: E → E + E | E - E | E * E | E / E | [E] | n
Check if this grammar is ambiguous or not.
Let’s consider a string derived from this grammar: n + n * n
→ Leftmost derivation:
Try to derive it in two different ways using the rules. If possible, the
grammar is ambiguous.
E
E→E+E
/|\
→n+E E+E
→n+E*E / \
→n+n*E n E*E
→n+n*n | |
n n
→ Leftmost derivation:

E→E*E
E
→E+E*E /|\
E*E
→n+E*E
| |
→n+n*E E+E n
| |
→n+n*n n n

Since the string n+n*n can be derived using two different parse
trees or left most derivation

So,the grammar is Ambiguous


D) Convert the following CFG to CNF

S ->ASB | ε

A ->aAS |a
B -> SbS|A| * bb

Convert this CFG to CNF, where each production is in one of the two
forms:
• A → BC, where A, B, and C are non-terminal symbols (not start
symbol on right).
• A → a, where a is a terminal.
• (Optionally, S → ε is allowed only if ε is in the language.)
STEP 1: Start Symbol present on the right side of the production rule
S1->S
S ->ASB |ε

A ->aAS |a
B -> SbS |A| bb
STEP :2 Remove useless, ε ,unit productions
No useless productions
Remove ε productions(S->ε )
S1->S
S->ASB/AB
A->aAS/aA/a
B->SbS/bS/Sb/b/A/bb
Remove unit productions(B->A,S1->S)
1)B->A A->aAS/aA/a
B-> aAS/aA/a
2) S1->S S-> ASB/AB

S1 -> ASB/AB

UPDATED GRAMMAR: S1 -> ASB/AB


S->ASB/AB
A->aAS/aA/a
STEP 3: Convert Terminal by Non -Terminal

S1-> A S B/A B
S-> A S B/A B
A-> Ca A S/Ca A/a
B-> S Cb S/Cb S/S Cb /b/Cb Cb/Ca A S/Ca A/a
Ca->a
Cb->b

STEP 4: Removal of sequence of Non – Terminals

S1->AX/AB
S->AX/AB
A->Ca X /Ca A /a
B-> Cb Y /Cb S /S Cb/ b/ Cb Cb/ Ca Z/Ca A /a
Ca->a
Cb->b
Y->SS
X->SB
Z->AS

IT WAS IN THE CNF


E)
Remove All use less , ε production rules
S → aA | aB
A → aaA | B | ε
B → b | bB
D→B

Remove use less symbols


S → aA
S
/\
a A
|
ε
Usefull

S → aB
S
/\
a B
|
b
usefull
A → aaA

A
/|\
a a A
|
ε
Usefull

A->B(usefull)

B->b(usefull)

B->bB

B
/ \
b B
|
b

D-> B(Usefull) No USELESS SYMBOLS


Remove ε production rules
S → aA | aB
A → aaA | B | ε
B → b | bB
D→B
Remove (A-> ε)
S->aA/aB/a
A->aaA/aa/B
B->b/bB
D->B
the final after removing useless, ε production rules
S->aA/aB/a
A->aaA/aa/B
B->b/bB
D->B
EXTENSION
FOR T1
 PDA (PUSH DOWN AUTOMATA)

 Turing Machine
Push Down Automata
A Push Down Automata is formally defined as a 7-tuple:
PDA= (Q,Σ,Γ,δ,q0,Z0,F)
where:
 Q = Finite set of states
 Σ = Input alphabet (finite set of input symbols)
 Γ = Stack alphabet (set of stack symbols)
 δ = Transition function:
δ:Q×(Σ∪ε)×Γ→P(Q×Γ∗)δ: Q × (Σ ∪ {ε}) × Γ → P(Q ×
Γ^*)δ:Q×(Σ∪ε)×Γ→P(Q×Γ∗)
(Defines how the PDA transitions between states based on input and stack
contents.)
 q₀ = Initial state (q₀ ∈ Q)
 Z₀ = Initial stack symbol (Z₀ ∈ Γ)
 F = Set of final states (F ⊆ Q)
Why Do We Need PDA?
1. Finite Automata are insufficient for Context-Free Languages
(CFLs).
o Example: The language L = {aⁿbⁿ | n ≥ 1} (equal number of
‘a’s followed by ‘b’s) cannot be recognized by a finite
automaton because it requires memory to store the count of
‘a’s.
2. Stack memory enables PDA to recognize CFLs.
o A stack allows PDAs to push elements when processing
one type of input and pop them when encountering
another type (e.g., matching ‘a’s with ‘b’s).
3. Used in compilers for syntax checking and parsing.
o Example: Parsing arithmetic expressions, handling
recursion, function calls, and nested loops in programming
languages.
Architecture of Push Down Automata

A PDA consists of three main components:


1. Finite Control (State Machine)
o Contains a set of finite states.
o Decides transitions based on input symbol and stack contents.
2. Input Tape (Read-Only Memory)
o Reads one symbol at a time from left to right.
o Cannot modify the input.
3. Stack Memory (LIFO – Last In, First Out)
o Used for storing and retrieving symbols.
o Supports three operations:
 Push(X): Add symbol X to the stack.
 Pop(): Remove the top symbol from the stack.
 No operation (ε): Leave the stack unchanged.
Real-Life Example On PDA:
Employee Entry & Exit Management
Consider an employee entry-exit system in a secure
office where:
 Employees enter in groups (0s).
 Each employee logs in (1s) to start work.
 Each employee logs out (2s) before leaving.
 Employees exit in groups (3s).
Approach Used in This PDA
1. Entry (0) is pushed onto the stack.
A valid entry-exit sequence follows L = {0ⁿ1ᵐ2ᵐ3ⁿ | n ≥ 1,
m ≥ 1}, meaning: 2. Login (1) is pushed onto the stack.
 The number of entry (0) and exit (3) events must 3. For each logout (2), one 1 is popped from the stack.
match. 4. If all 1s have been popped and 0 is on top of the stack, then for
 Each login (1) must have a corresponding logout (2). every exit (3), a 0 is popped.
 Employees must log in before logging out. 5. If the input finishes and the stack is empty, the sequence is
Employees must exit only after logging out valid; otherwise, it is rejected.
Step-by-Step Working of the PDA
 Step 1: On receiving 0 (Employee enters), push it onto
the stack. On receiving 1 (Employee logs in), push it onto
the stack and go to the next state.
 Step 2: On receiving 1 (Employee logs in), push it onto
the stack. On receiving 2 (Employee logs out), pop 1
from the stack and go to the next state.
 Step 3: On receiving 2 (Employee logs out), pop 1 from
the stack. If all 1s have been popped and 0 is on top of
the stack, then upon receiving 3 (Employee exits), pop 0
Transition Table
from the stack and go to the next state.
 Step 4: On receiving 3 (Employee exits), pop 0 from the State Input Stack Top Stack Action Next State
stack. If input is finished and stack is empty, go to the q₀ 0 Z₀ Push 0 q₀
q₀ 1 0 Push 1 q₁
final state and accept the sequence. q₁ 1 1 Push 1 q₁
q₁ 2 1 Pop 1 q₁
q₁ 2 1 Pop 1 q₂
q₂ 3 0 Pop 0 q₂

q₂ 3 0 Pop 0 q₄ (Accept)
Example Valid Sequences (Accepted by PDA)
1. 011223✅ (One group of employees entering, one person logging in and
out, then exiting.)
2. 00111223 ✅ (Two groups enter, each with one employee logging in and
out.)
3. 0011112223 ✅ (Two groups enter, each with two employees logging in and
out.)

Example Invalid Sequences (Rejected by PDA)


1. 0113 ❌ (Employee logged in but never logged out.)
2. 01223 ❌ (Employee logged out without logging in.)
3. 0111223 ❌ (Mismatched logins and logouts.)

Conclusion
 This PDA ensures employees follow proper login-logout procedures before leaving the office.
 It can be used in access control systems, attendance tracking, and secure office environments.
 Any mismatch in entry-exit or login-logout leads to immediate rejection.
Turing
Machine
A Turing Machine (TM) is a mathematical model of computation that
defines an abstract machine capable of simulating any algorithm. It
consists of an infinite tape, a read/write head, and a finite set of states
that determine the machine’s behavior based on a transition function.
Formally, a Turing Machine is defined as a 7-tuple:
M=(Q,Σ,Γ,δ,q0​
,B,F)
 A Turing Machine operates by reading a
symbol on the tape, changing it, moving left or
right, and transitioning between states
according to δ until it reaches an accept or
reject state.
PROBLEM ON TURING MACHINE
CONCLUSION
A Context-Free Grammar (CFG) defines a language where each
production rule replaces a single non-terminal with a string of
terminals and/or non-terminals, used to generate context-free
languages (CFLs).
A Pushdown Automaton (PDA) is a machine that uses a stack to
recognize CFLs, handling languages with nested structures, like
balanced parentheses.
A Turing Machine (TM) is a more powerful computational model
that can recognize recursively enumerable languages (RE), with
an infinite tape and the ability to read, write, and move in both
directions. While PDAs are limited to context-free languages, TMs
can recognize much more complex languages, including those that
are not context-free.
THANK YOU

You might also like