0% found this document useful (0 votes)
8 views

chapter 3 Slides

Uploaded by

cherkos welday
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

chapter 3 Slides

Uploaded by

cherkos welday
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Pushdown Automata

Chapter Three
Formal Languages and Automata

Yoseph K, Instructor
[email protected]
Office Hours: Monday - Friday
Introduction

CLASSIFICATION OF GRAMMARS
 A grammar is a set of rules for putting strings together and
so corresponds to a language.
 A grammar G can be formally written as a 4-tuple (N, T, S,
P) where
 N or V is a set of Non-terminal symbols
 T or ∑ is a set of Terminal symbols
 S is the Start symbol, S ∈ N
 P is Production rules for Terminals and Non-terminals
Summary
 Type-3 grammars generate regular languages.
 Type-2 grammars generate context-free languages.
 Type-1 grammars generate context-sensitive languages.
 Type-0 grammars generate recursively enumerable languages.
Context-Free Grammar
 Definition: A context-free grammar (CFG) consisting of a finite
set of grammar rules is a quadruple (N, T, P, S) where
 N or V is a set of non-terminal symbols.
 T is a set of terminals
 P is a set of rules,
 S is the start symbol.
Example
 The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
 The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
Cont’d
 Example:-
for generating a language that generates equal number of a’s
& b’s in the form an bn , the CFG will be defined as
G={(S,A), (a,b), (S → aAb, A → aAb| ε)}
Solution:
S → aAb
→ aaAbb [by production rule of A →aAb]
→ aaaAbbb [by production rule of A →aAb]
→ aaabbb [by production rule of A → ε]
→ a3 b3 = an bn
Cont’d
 Example2. Consider the grammar
S → AB (1)
A → C (2)
CB → Cb (3)
C → a (4)
where {a, b} are terminals, and {S, A, B, C} are non-terminals.
 We can derive the phrase “ab” from this grammar in the following
way: S → AB, from (1)
→ CB, from (2)
→ Cb, from (3)
→ ab, from (4)
Generation of Derivation Tree
 A derivation tree or parse tree is an ordered rooted tree that
graphically represents the semantic information a string derived from a
context-free grammar.
Representation Technique
 Root vertex − Must be labeled by the start symbol.
 Vertex − Labeled by a non-terminal symbol.
 Leaves − Labeled by a terminal symbol or ε.
If S → x1x2 …… xn is a production rule in a CFG, then the parse tree /
derivation tree will be as follows −
Cont’d

 There are two different approaches to draw a derivation tree −


 Top-down Approach −
 Starts with the starting symbol S
 Goes down to tree leaves using productions
 Bottom-up Approach −
 Starts from tree leaves
 Proceeds upward to the root which is the starting symbol S
Example
 Let a CFG {N,T,P,S} be
N = {S}, T = {a, b}, Starting symbol = S,
P = S → SS | aSb | ε
 One derivation from the above CFG is “abaabb”
 S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
Example derivation in a Grammar
 Grammar: start symbol is A
A → aAa
A→B
B → bB
B→ε
 The from the above, sample derivation gives us:
A ⇒ aAa ⇒ aaAaa ⇒ aaaAaaa ⇒ aaaBaaa
⇒ aaabBaaa ⇒ aaabbBaaa ⇒ aaabbaaa

Q1. Language?
Q2 Derivations in Tree Form ?
Derivations in Tree Form
CFL Closure Property
 Context-free languages are closed under −
 Union
 Concatenation
 Kleene Star operation
 [Union]
Let L1 and L2 be two context free languages. Then L1 ∪ L2 is also
context free
 [Concatenation]
If L1 and L2 are context free languages, then L1L2 is also context
free.
 [Kleene Star]
If L is a context free language, then L* is also context free.
Arithmetic expressions in a programming
language

Q1 - Derive: a + a*a ???


Cont’d

 Notice how the grammar gives the meaning a + (a×a)


Grammars in real computing
 CFGʼs are universally used to describe the syntax of
programming languages
 Perfectly suited to describing recursive syntax of
expressions and statements
 Tools like compilers must parse programs; parsers
can be generated automatically from CFGʼs
 Real languages usually have a few non-CF bits
 CFGʼs are also used in XML DTDʼs
All regular languages have context free
grammars
 Any regular grammar is also a CFG. (Immediate from the
definition of the grammars).
Example:
• S→aQ1 S→bR1
• Q1 →aQ1 Q1 →bQ2
• Q2 →aQ1 Q2 →bQ2
• R1 →aR2 R1 →bR1
• R2 →aR2 R2 →bR1
• Q1 →ε R1 →ε
Ambiguity
 A grammar in which the same string can be given more than one
parse tree is ambiguous.
 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.
 Example: another grammar for arithmetic expressions
‹EXPR› → ‹EXPR› + ‹EXPR› |
‹EXPR› × ‹EXPR› |
( ‹EXPR› )| a
Derive: a + a × a
 This grammar is ambiguous: there are two different parse trees for a + a × a
Cont’d
 Problem
Check whether the grammar G with production rules:
X → X+X | X*X |X| a
is ambiguous or not.
Pushdown Automata Introduction
Basic Structure of PDA
• A pushdown automaton is a way to implement a context-free
grammar in a similar way we design FA for a regular grammar.
• A DFA can remember a finite amount of information, but a PDA can
remember an infinite amount of information.
• Basically a pushdown automaton is −
"Finite state machine" + "a stack"
• A pushdown automaton has three components
 an input tape,
 a control unit, and
 a stack with infinite size.
Cont’d
• The stack head scans the top symbol of the stack.
• A stack does two operations −
 Push − a new symbol is added at the top.
 Pop − the top symbol is read and removed.
• A PDA may or may not read an input symbol, but it has to
read the top of the stack in every transition.
Cont’d
• A PDA can be formally described as a 7-tuple (Q, ∑, Γ, δ, q0, I,
F) −
 Q is the finite number of states
 ∑ is input alphabet
 Γ is stack symbols
 δ is the transition function: Q × (∑ ∪ {ε}) × Γ × Q × Γ *
 q0 is the initial state (q0 ∈ Q)
 I is the initial stack top symbol (I ∈ Γ)
 F is a set of accepting states (F ∈ Q)
Cont’d
 The following diagram shows a transition in a PDA from a
state q1 to state q2, labeled as a, b → c −

 From the above diagram, this means at state q1, if we encounter an


input string ‘a’ and top symbol of the stack is ‘b’, then we pop ‘b’,
push ‘c’ on top of the stack and move to state q2.
Cont’d
 Suppose the set of transition rules of an PDA contains
δ (q1 , a , b) = {(q2 , cd) , (q3 , ε )}
If at any time the control unit is in state q1, the input symbol
read is a, and the symbol on top of the stack is b, then one of
two things can happen:
 the control unit goes into state q2 and the string cd
replaces b on top of the stack, or
 the control unit goes into state q3 with the symbol b
removed from the top of the stack.
Executing a PDA
 The behavior of a PDA at any point depends on
⟨state, stack, unread input⟩
 PDA begins in start state with stated symbol on the stack
 On each step it optionally reads an input character, pops a
stack symbol, and non-deterministically chooses a new state
and optionally pushes one or more stack symbols
 PDA accepts if it is in a final state and there is no more input.
Examples on PDA | class Activity
 Example1. consider a PDA with
Q = {q0 , q1 , q2 , q3},
∑ = {a, b},
Γ = {0, 1},
I = 0,
F = {q3}, with initial state q0 and
δ(q0 , a , 0) = {(q1 , 10) , (q3 , ε)}
δ(q0 , ε , 0) = {(q3 , ε)}
δ(q1 , a , 1) = {(q1 , 11)}
δ(q1 , b , 1) = {(q2 , ε)}
δ(q2 , b , 1) = {(q2 , ε)}
δ(q2 , ε , 0) = {(q3 , ε)}
Design the given machine ??
In this representation we label the edges of the graph
with three things: the current input symbol, the symbol at the
top of the stack, and the string that replaces the top of the
stack.
Example PDA
A PDA for {an bn :n>0}
Example Execution: 0011
Example Execution: 0011
Example Execution: 0011
Example Execution: 0011
Example Execution: 0011
Example Execution: 0011
Example Execution: 0011
Transitions
We defined δ : (Q × ∑ε × Γε) → P{Q × Γ*}
 The transitions δ(q, a, γ) are applicable iff
q is the current state,
a= ε, or the next character on the input tape is a, and
γ = ε, or the top of the stack is
Summary
 PDA explains “FA + Stack” and it enhanced FA containing
infinite stack.
 Transition in PDA are of the form
a, X Y
Meaning that if you see an a in the input string & the
stack contains the symbol X on top then you remove the X &
add Y.
 The stack gives us extra power to recognize non regular
languages.
Exercise
 Problem 1. Which language generates the grammar G given
by the productions S → aSa | aBa , B → bB | b
 Problem 2. Construct a context-free grammar for the
following DFA:

•Problem 3. Show that the grammar ({S}; {a, b}; R, S) with


rules R = S → aS | aSbS |ε is ambiguous.
Cont’d
 Problem 4. consider the grammar with productions
S → aAB,
A →bBb,
B → A| ε , then check the leftmost and rightmost
derivation?
 Problem 5. Show for the string aabbbb with the grammar and
give a verbal description of the language generated by this
grammar?
S → AB| ε
A → aB,
B → Sb
cont’d
 Problem 6. Consider the grammar with productions
S → aaB,
A →bBb | ε
B → Aa,
Show that the string aabbabba is not in the language generated
by this grammar.
THE END

You might also like