Unit 3
Unit 3
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?
5
PDA as a state diagram
δ(qi,a, X)={(qj,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?
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:
Σ: Input alphabet.
Γ: Stack alphabet.
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 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)}
a, X / Y Next
qi qj state
a, X / Y
qk Multiple
Next states 28
Initial state of the PDA:
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)}
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
• 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.
Case-02:
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-
• 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.
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 have string like aAbcde and your input string is
abbcde.
• Expand A->b.
• 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.