0% found this document useful (0 votes)
40 views96 pages

Unit 3

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)
40 views96 pages

Unit 3

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/ 96

Unit – III (Push Down

Automata)
Topics to be discussed
• Deterministic Push Down Automata
• Non-Deterministic Push Down Automata
• Equivalence of CFG and PDA
• Pumping Lemma for CFL
• Closure Properties of CFL and Decision Algorithms
• Overview of Top-down and Bottom-up Parsing
Deterministic Push Down Automata
PDA - the automata for Context Free
Languages
• What is?
• FA to Reg Lang, PDA is to CFL
• PDA == [ FA + “a stack” ]
• Why a stack?

Input -NFA Accept/reject


string

A stack filled with “stack symbols”


4
Pushdown Automata - Definition
• A PDA P := ( Q,∑,, δ,q0,Z0,F ):
• Q: states of the -NFA
• ∑: input alphabet
•: stack symbols
• δ: transition function
• q0: start state
• Z0: Initial stack top symbol
• F: Final/accepting states

5
PDA as a state diagram

δ(qi,a, X)={(qj,Y)}

Next Current Stack


input stack Top
Current symbol top Replacement
state (w/ string Y)

a, X / Y Next
qi qj state

6
Acceptance by 2 Methods
• PDAs that accept by final state:
• For a PDA P, the language accepted by P, denoted by L(P) by final state, is:
• {w | (q0,w,Z0) |---* (q,, A) }, s.t., q  F
Checklist:
- input exhausted?
- in a final state?

• PDAs that accept by empty stack:


• For a PDA P, the language accepted by P, denoted by N(P) by empty stack, is:
• {w | (q0,w,Z0) |---* (q, , ) }, for any q  Q.

Q) Does a PDA that accepts by empty stack Checklist:


need any final state specified in the design? - input exhausted?
- is the stack empty?
7
Design a PDA for accepting a language n
{0 1n |
n>=1}
• According to the given language, consider an Input string with an
equal number of 0s and 1s as “0011∈”. Whenever the epsilon (∈)
occurs as input, then that transition is the last.
Transition Explanation for first input
• To perform the PUSH Operation for the first input “0”, the transition
function and detail diagram are given below
• δ(q0, 0, Z0) = (q0, 0Z0)
Transition Explanation for second input
• To perform the PUSH Operation for the second input “0”, the transition function and detail diagram
are given below.
• δ (q0, 0, 0) = (q0, 00Z0)
Transition Explanation for third input
• To perform the POP Operation for the third input “1”, the transition function and detail diagram are
given below.
• δ(q0, 1, 0) = (q0, ∈) // Now stack hold value “0Z0”
Transition Explanation for fourth input
• To perform the POP Operation for the fourth input “1”, the transition function and detail diagram
are given below
• δ(q0, 1, 0) = (q0, ∈) // Now stack hold value “Z0”
Transition Explanation for Empty Input
• At this stage, the stack is empty (Z0), and the last Input string having the last symbol is epsilon (∈).
So, there will be no change in the stack. The transition function with a detailed diagram is given
below.
• δ (q0, ∈, Z0) = (q1, ∈) // Z0 represents epsilon
Visualization of Push Down Automata

Have a look at the link to explore how PDA


works

• Visualization of Pushdown Automata (asethome.org)


Deterministic PDA: Definition
• A PDA is deterministic if and only if:
1. δ(q,a,X) has at most one member for any a  ∑ U {}

➔ If δ(q,a,X) is non-empty for some a∑, then δ(q, ,X) must be


empty.

➔ A language L is a deterministic context-free language (DCFL)if there


is a DPA that accepts L

15
Checking conditions in DPDA
Problems: Deterministic PDA
Problems: Deterministic PDA
D-PDA for Lwcwr = {wcwR | c is some special symbol not in
w}
Note:
Grow stack
Pop stack for • all transitions have
0, Z0/0Z0
1, Z0/1Z0 matching symbols become deterministic
0, 0/00
0, 1/01 0, 0/ 
1, 0/10 1, 1/ 
1, 1/11

q0 q1 q2
c, Z0/Z0 , Z0/Z0
c, 0/0
c, 1/1 Accepts by
Switch to final state
popping mode

19
NPDA vs DPDA vs Regular languages
Lwcwr
Lwwr

D-PDA
Regular languages

Non-deterministic PDA

20
Non Deterministic Finite Automata
Non Deterministic Pushdown Automata
• A Non-Deterministic Pushdown Automata (NPDA) is an automaton comprising a finite state machine combined with a stack.
• It can have multiple transitions for a given input symbol and stack content, making it non-deterministic.
• It is mainly used to recognize context-free languages (CFLs).
• The formal definition of an NPDA is a 7-tuple:

Q: Finite set of states.

Σ: Input alphabet.

Γ: Stack alphabet.

δ: Transition function (maps Q × Σ × Γ to the power set of Q × Γ*).

q₀: Initial state.

Z₀: Initial stack symbol.

F: Set of accepting states.


NPDA vs DPDA vs Regular languages
Lwcwr
Lwwr

D-PDA
Regular languages

Non-deterministic PDA

23
Deterministic Vs Non Deterministic PDA

Deterministic PDA’s
• Every state/stack combination has exactly one transition
• No epsilon transitions(epsilon for both input and stack symbol)
Non Deterministic PDAs
• More powerful than deterministic PDAs
• -example: Strings from a*b * that have either twice as many a’s as b’s or
thrice as many a’s as b’s , etc.,
N-PDA
An NPDA can accept a language in two ways:

• By final state: The NPDA accepts if it enters an accepting (final) state


after reading the input string.

• By empty stack: The NPDA accepts if the input is consumed, and the
stack is empty.
NDPDA-Problems related to DPDA
NDPDA-Problems related to DPDA
N-PDA as a state diagram

δ(qi,a, X)={(qj,Y)}

Next Current Stack


input stack Top
Current symbol top Replacement
state (w/ string Y)

a, X / Y Next
qi qj state

a, X / Y
qk Multiple
Next states 28
Initial state of the PDA:

PDA for LWWR Stack


top
q0

Z0

1. δ(q0,0, Z0)={(q0,0Z0)}
2. δ(q0,1, Z0)={(q0,1Z0)} First symbol push on stack

3. δ(q0,0, 0)={(q0,00)}
4. δ(q0,0, 1)={(q0,01)} Grow the stack by pushing
5. δ(q0,1, 0)={(q0,10)} new symbols on top of old
6. δ(q0,1, 1)={(q0,11)} (w-part)
7. δ(q0, , 0)={(q1, 0)}
8. δ(q0, , 1)={(q1, 1)}
Switch to popping mode, nondeterministically
9. δ(q0, , Z0)={(q1, Z0)}
(boundary between w and wR)
10. δ(q1,0, 0)={(q1, )}
Shrink the stack by popping matching
11. δ(q1,1, 1)={(q1, )}
symbols (wR-part)
12. δ(q1, , Z0)={(q2, Z0)}

Enter acceptance state


29
Example shows that: Nondeterministic PDAs ≠ D-PDAs

D-PDA for Lwcwr = {wcwR | c is some special symbol not in w}


Note:
• all transitions have
Grow stack become deterministic
0, Z0/0Z0
Pop stack for
1, Z0/1Z0 matching symbols
0, 0/00
0, 1/01 0, 0/ 
1, 0/10 1, 1/ 
1, 1/11

q0 q1 q2
c, Z0/Z0 , Z0/Z0
c, 0/0
c, 1/1 Accepts by
Switch to final state
popping mode

30
This PDA for L=wwR is non-deterministic

Grow stack
0, Z0/0Z0
Why does it have
1, Z0/1Z0 Pop stack for to be non-
0, 0/00 matching symbols deterministic?
0, 1/01
1, 0/10 0, 0/ 
1, 1/11 1, 1/ 

q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 Accepts by final state
Switch to To remove guessing,
popping mode impose the user to
insert c in the
middle
31
This PDA for L=wwR is non-deterministic
This PDA for L=wwR is non-deterministic
Limitations of DPDA
• DPDAs cannot accommodate the full range of Context-Free Languages
(CFLs) due to their deterministic behaviour.
Applications
• DPDAs find applications in designing deterministic context-
free programming languages and parsers.
• Its simplistic and straightforward nature allows for
easier debugging and quicker running time.
• DCFL can be parsed in linear time. This is so important for compilers
that all practical programming languages are based on DCFLs
• NPDAs are utilized in the design of compilers and syntax checkers
where multiple transitions for the same condition might occur,
offering flexibility and increased computing power.
Think off?
• DFA and NFA are Equivalent

• Think Whether PDA’s and NPDA’s are Equivalent?


Equivalence of CFG and PDA
Pushdown automata to CFL
Pushdown automata to CFL
Pushdown automata to CFL Equivalence-Problems
of PDA to CFG
Pushdown automata to CFL
Equivalence-Problems of PDA to CFG
CFL to Pushdown automata Equivalence
CFL to Pushdown automata Equivalence-Problems
CFL to PDA - Problems
CFL to PDA - Problems
Assignment Exercises:
Pumping Lemma
Pumping Lemma
• Pumping lemma for context-free language (CFL) is used to prove that
a language is not a Context-free language
• Assume L is context-free language
• Then there is a pumping length n such that any string w εL of
length>=n can be written as follows −
• |w|>=n
Pumping Lemma
We can break w into 5 strings, w=uvxyz, such as the ones given below
• |vxy| >=n
• |vy| # ε
• For all k>=0, the string uvkxyyz∈L
Pumping Lemma
The steps to prove that the language is not context-free by using the
pumping lemma are explained below
• Assume that L is context-free.
• The pumping length is n.
• All strings longer than n can be pumped |w|>=n.
• Now find a string 'w' in L such that |w|>=n.
• Divide w into uvxyz
• Show that uvkxykz ∉L for some k
• Then, consider the ways that w can be divided into uvxyz.
• Show that none of these can satisfy all the 3 pumping conditions at same
time.
• w cannot be pumped (contradiction).
Pumping Lemma
Example
Find out whether L={xnynzn |n>=1} is context free or not

• Let L be context-free.
• L must satisfy pumping length, say n.
• Now we can take a string such that s=xnynzn
• We divide s into 5 strings uvxyz.
Pumping Lemma
Let n=4 so, s=x4y4z4
Case 1:
v and y each contain only one type of symbol.
{we are considering only v and y because v and y have power uv2xy2z}
x xx xyyyyz z zz
=uvkxykz when k=2
=uv2xy2z
=xxxxxxyyyyzzzzz
=x6y4z5
(Number of x # number of y #number of z)
Therefore, The resultant string is not satisfying the condition
x6y4z5 ∉ L
If one case fails then no need to check another condition.
Applications of Pumping Lemma
• XML Parsing
• Pumping lemma is used while playing games.
Pumping lemma is a negative test. It can be used in applications
like Showing an invalid move in game of chess. As the move may
not obey rules of game, the pumping lemma can be applied to
prove that the inputted move is invalid.
• At power stations this pumping lemma is used.
Moreover, some Power stations also use this lemma for determining
the cut off temperatures to be kept in furnaces. For an example, say
pumping lemma can answer to why the temperature shouldn’t go
beyond 250 Degrees, etc.
Closure Properties of Context Free
Lanuage (CFL)
Closure Properties of CFL
• Context Free Languages (CFLs) are accepted by pushdown automata.
• Context free languages can be generated by context free grammars
• CFL are closed under:
• Union
• Concatenation
• Closure – Kleene and positive
• Reversal
• Homomorphisms
• CFL is not closed under concatenation, difference, complement
CFL - Union
• If L1 and L2 are two context free languages, then L1 ꓴ L2 will also be
context free. For example,
• L1 = { anbncm | m >= 0 and n >= 0 } and L2 = { anbmcm | n >= 0 and m
>= 0 }
• L3 = L1 ꓴ L2 = { anbncm ꓴ anbmcm | n >= 0, m >= 0 } is also context free.
• L1 says number of a’s should be equal to number of b’s and L2 says
number of b’s should be equal to number of c’s.
• Union of L1 and L2 says either of the two conditions are true.
• Resultant string is belongs to a context free language.
• CFL are closed under Union.
CFL – Concatenation
• If L1 and If L2 are two context free languages, their concatenation L1.L2 will also
be context free. For example,
• L1 = { anbn | n >= 0 } and L2 = { cmdm | m >= 0 }
• L3 = L1.L2 = { anbncmdm | m >= 0 and n >= 0} is also context free.
• L1 says number of a’s should be equal to number of b’s and L2 says number of c’s
should be equal to number of d’s.
• Concatenation of L1 and L2 says first number of a’s should be equal to number of
b’s, then number of c’s should be equal to number of d’s.
• We can create a PDA which first pushes a’s and pops b’s, then pushes c’s and pops
d’s.
• Resultant string is accepted by pushdown automata; Resultant String is context
free.
• CFL are closed under Concatenation.
Kleene Closure
• If L1 is context free, its Kleene closure L1* will also be context free.
For example,
• L1 = { anbn | n >= 0 }
• L1* = { anbn | n >= 0 }* is also context free.
• CFL are closed under Kleen Closure.
CFL – Intersection and Complementation
• If L1 and If L2 are two context free languages, their intersection L1 Ո L2 need not be
context free
• L1 = { anbncm | n >= 0 and m >= 0 } and L2 = (ambncn | n >= 0 and m >= 0 }
• L3 = L1 Ո L2 = { anbncn | n >= 0 } need not be context free.
• L1 says number of a’s should be equal to number of b’s
• L2 says number of b’s should be equal to number of c’s.
• Intersection says both conditions need to be true, but push down automata can compare
only two.
• Cannot be accepted by pushdown automata, hence not context free.
• Similarly, complementation of context free language L1 which is ?* – L1, need not be
context free.

• Note : So CFL are not closed under Intersection and Complementation.


Closure under Reversal
• If L is a CFL with grammar G, form a grammar for LR by reversing the
right side of every production.
• Example: Let G have S -> 0S1 | 01.
• The reversal of L(G) has grammar S -> 1S0 | 10.
Substitution - CFL
• CFL’s are closed under Substitution.
• String in a language is substituted by an entire language
For each a  ∑, let s(a) be a language
If w=a1a2…an  L, then:
• s(w) = { x1x2 … }  s(L), s.t., xi  s(ai)
Example:
• Let ∑={0,1}
• Let: s(0) = {anbn | n ≥1}, s(1) = {aa,bb}
• If w=01, s(w)=s(0).s(1)
• E.g., s(w) contains a1 b1 aa, a1 b1bb,
a2 b2 aa, a2 b2bb,
… and so on.
Homomorphism - CFL
• CFL is closed under homomorphism
• It is similar to substitution
• Let L be a CFL with grammar G.
• Let h be a homomorphism on the terminal symbols of G.
• Construct a grammar for h(L) by replacing each terminal symbol
• ‘a’ by ‘h(a).’
• G has productions S -> 0S1 | 01.
• h is defined by h(0) = ab, h(1) = ε.
• h(L(G)) has the grammar with productions S -> abS | ab
Applications of Closure Properties of CFL
• Closure properties are used to show that a language is not context-
free
Decision Algorithms for CFL
Algorithm To Decide Whether CFL Is Finite Or Not-
For a given CFG, an algorithm exists to decide whether its language is
finite.
Step-01:
Reduce the given grammar completely by
• Eliminating ∈ productions
• Eliminating unit productions
• Eliminating useless productions
Decision Algorithms for CFL
Step-02:

• Draw a directed graph whose nodes are variables of the given


grammar.
• There exists an edge from node A to node B if there exists a
production of the form A → αBβ.
Decision Algorithms for CFL
Case-01:

• Directed graph contains a cycle.


• In this case, the language of the given grammar is infinite.

Case-02:

• Directed graph does not contain any cycle.


• In this case, the language of the given grammar is finite.
Decision Algorithms for CFL
Problem

Check whether language of the following grammar is finite or not-


S → AB / a
A → BC / b
B → CC / c
Solution-
Step-01:
The given grammar is already completely reduced.
Step-02:
We will draw a directed graph whose nodes will be S , A , B , C.
Decision Algorithms for CFL
• Due to the production S → AB, directed graph will have edges S → A
and S → B
• Due to the production A → BC, directed graph will have edges A → B
and A → C.
• Due to the production B → CC, directed graph will have edge B → C.
Decision Algorithms for CFL
The required directed graph is-

Clearly,
•The directed graph does not contain any cycle.
•Therefore, language of the given grammar is finite.
Decision Algorithms for CFL
Problem:
Check whether language of the following grammar is finite or not-
S → XS / b
X → YZ
Y → ab
Z → XY
Solution
Step-01:
The given grammar is already completely reduced.
Step-02:
We will draw a directed graph whose nodes will be S , X , Y , Z.
Decision Algorithms for CFL
• Due to the production S → XS / b, directed graph will have edges S →
X and S → S.
• Due to the production X → YZ, directed graph will have edges X → Y
and X → Z.
• Due to the production Z → XY, directed graph will have edges Z → X
and Z → Y.
Decision Algorithms for CFL
The required directed graph is-

Clearly,
•The directed graph contain cycles.
•Therefore, language of the given grammar is infinite.
Decision Algorithms for CFL
Algorithm To Decide Whether CFL Is Empty Or Not
• If we can not derive any string of terminals from the given
grammar,then its language is called as an Empty Language.
L(G) = ϕ
• For a given CFG, an algorithm exists to decide whether its language is
empty L(G) = ϕ or not.
Decision Algorithms for CFL
Algorithm-

• Remove all the useless symbols from the grammar.


• A useless symbol does not derive any string of terminals.
• If the start symbol is found to be useless, then language is empty
otherwise not

• The language generated from a CFG is non-empty iff the start symbol
is generating.
Decision Algorithms for CFL
Example-
Consider the following grammar-
S → XY
X → AX
X → AA
A→a
Y → BY
Y → BB
B→b
Decision Algorithms for CFL
Now, let us check whether language generated by this grammar is
empty or not.
The given grammar can be written as-
S → aabb
X → aX
X → aa
A→a
Y → bY
Y → bb
B→b
Decision Algorithms for CFL
Clearly,
• The start symbol generates at least one string (many more are
possible).
• Therefore, start symbol is useful.
• Thus, language generated by the given grammar is non-empty.
Overview of Top Down Parsing and
Bottom Up Parsing
Parser
A parser takes the sequence of tokens produced by the lexical analyzer
(lexer) and constructs a parse tree or abstract syntax tree (AST). The
primary goals of the parser are:
1.Syntax Analysis: To verify that the token sequence follows the
language’s grammar rules.
2.Error Detection: To identify and report syntax errors in the source
code.
3.AST Generation: To build a hierarchical structure (AST) representing
the syntactical structure of the source code.
Syntax Analysis (Application of Parser)
• Every programming language has rules that prescribe the syntactic
structure of well-formed programs.
• In Pascal, for example, a program is made out of blocks, a block out of
statements, a statement out of expressions, an expression out of
tokens, and so on.
• The syntax of programming language constructs can be described by
context-free grammars or BNF (Backus-Naur Form) notation.
Syntax Analysis (Application of Parser)
• Grammars offer significant advantages to both language designers and compiler
writers.
• Grammar gives a precise, yet easy-to-understand. Syntactic specification of
a programming language.
• From certain classes of grammars we can automatically construct an
efficient parser that determines if a source program is syntactically well
formed. As an additional benefit, the parser construction process can reveal
syntactic ambiguities and other difficult-to-parse constructs that might
otherwise go undetected in the initial design phase of a language and its
compiler.
• A properly designed grammar imparts a structure to a programming
language that is useful for translating source programs into correct object
code and detecting errors. Tools are available for converting grammar-based
descriptions of translations into working pro-grams.
Role of the Parser
• Parser for any grammar is a program that takes as input string w (obtain a
set of strings tokens from the lexical analyzer) and produces as output either
a parse tree for w if w is a valid sentence of grammar or error message
indicating that w is not a valid sentence of given grammar.
• The goal of the parser is to determine the syntactic validity of a source
string is valid; a tree is built for use by the subsequent phases of the
computer.
• The tree reflects the sequence of derivations or reductions used during the
parser. Hence, it is called a parse tree.
• If the string is invalid, the parse has to issue a diagnostic message
identifying the nature and cause of the errors in the string.
• Every elementary subtree in the parse tree corresponds to a production of
the grammar.
Role of the Parser
There are two ways of identifying an elementary subtree:
• 1. By deriving a string from a non-terminal or
• 2. By reducing a string of symbol to a non-terminal.
Parse Tree
• A parse tree is a graphical representation of a derivation sequence of a sentential
form.
In a parse tree:
• Inner nodes of a parse tree are non-terminal symbols.
• The leaves of a parse tree are terminal symbols.
• A parse tree can be seen as a graphical representation of a derivation.
• A parse tree depicts associativity and precedence of operators.
• The deepest sub-tree is traversed first, therefore the operator in that sub-tree gets
precedence over the operator in the parent nodes.
Parse Tree
• Construction of Parse Tree
Types of Parser
Parsers are broadly categorized into two types based on their parsing
strategies:
1.Top-Down Parsers: These parsers build the parse tree from the top
(start symbol) and proceed down to the leaves (terminal symbols).
1. Recursive Descent Parser: A straightforward top-down parser implemented
using a set of recursive procedures.
2. Predictive Parser (LL Parser): A more efficient top-down parser that uses
lookahead tokens to make parsing decisions without backtracking.
Types of Parser
Bottom-Up Parsers: These parsers build the parse tree from the leaves
(terminal symbols) and proceed upwards to the root (start symbol).
• Shift-Reduce Parser: A type of bottom-up parser that shifts input
tokens onto a stack and reduces them to grammar rules.
• LR Parser: A more powerful bottom-up parser that can handle a
broader range of grammars, including LR(0), SLR(1), LALR(1), and
LR(1) parsers.
Types of Parser
Top Down Parser
• In the top-down technique parse tree constructs from the top and input
will read from left to right. In top-down, In the top-down parser, It will
start the symbol from proceed to the string.

• It follows left most derivation.

• In a top-down parser, difficulty with a top-down parser is if a variable


contains more than one possibility selecting 1 is difficult.
Working of Top Down Parser
• Let’s consider an example where grammar is given and you need to construct a
parse tree by using top-down parser technique.
Grammar:
S -> aABe
A -> Abc | b
B -> d

Input:
abbcde$
Working of Top Down Parser
• First, you can start with S -> a A B e and then you will see
input string a in the beginning and e in the end.

• Now, you need to generate abbcde .

• Expand A-> Abc and Expand B-> d.

• Now, You have string like aAbcde and your input string is
abbcde.

• Expand A->b.

• Final string, you will get abbcde.


Bottom Up Parser
• It will start from string and proceed to start.
• In a Bottom-Up parser, Identifying the correct handle (substring) is
always difficult.
• It will follow the rightmost derivation in reverse order.
Working of Bottom-Up Parser
• Grammar
S -> aABe
A -> Abc | b
B -> d

• Input
abbcde$
Working of Bottom-Up Parser
• First, you can start with A -> b.
• Now, expand A -> Abc.
• After that Expand B-> d.
• In the last, just expand the S -> aABe
• Final string, you will get abbcde.
Exercise Problem
• construct a Top Down Parser for the below string using the given
grammar
Exercise Problem
• construct a Bottom-Up Parser for the given input string using the
given grammar.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy