0% found this document useful (0 votes)
147 views24 pages

Mod11 - Context Sensitive Grammar (PDA)

This document provides information about context sensitive grammar and pushdown automata (PDA). It discusses the basic structure of a PDA, including its components and formal definition. It also covers PDA acceptance methods, the relationship between PDAs and context-free grammars, and how PDAs can be used for parsing by implementing top-down parsing.
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)
147 views24 pages

Mod11 - Context Sensitive Grammar (PDA)

This document provides information about context sensitive grammar and pushdown automata (PDA). It discusses the basic structure of a PDA, including its components and formal definition. It also covers PDA acceptance methods, the relationship between PDAs and context-free grammars, and how PDAs can be used for parsing by implementing top-down parsing.
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
You are on page 1/ 24

WEEK 13

Context Sensitive
Grammar

CS 6205 – Automata and Formal Language


Theory
Week 13: Context Sensitive Grammar
Course Learning Outcomes: Basic Structure of PDA

By the end of this module, a student is able to:


1. Pushdown Automata Acceptance.
2. PDA & Context-Free Grammar
3. Pushdown Automata and Parsing
Topics Covered

• Pushdown Automata Context Sensitive


Acceptance.
• PDA & Context-Free
Grammar
Grammar
• Pushdown Automata and
Parsing
BASIC STRUCTURE OF PDA
● A pushdown automaton is a way to implement a context-
free grammar in a similar way we design DFA for a
regular grammar

• Basically a pushdown automaton is:


-"Finite state machine" + "a stack"
BASIC STRUCTURE OF PDA
A pushdown automaton has three components:
● an input tape
● a control unit
● a stack with infinite size
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
Alan Turing (1912-1954)
A PDA may or may not read an input symbol, but it has to
read the top of the stack in every transition
BASIC STRUCTURE OF PDA
A PDA can be formally described as a 7-tuple (Q, Σ, S, δ, q0, I, F) :

• Q is the finite number of states.

• Σ is input alphabet

• S is stack symbols

• δ is the transition function: Q × (Σ∪{ε}) × S × Q × S*

• q0 is the initial state (q0 ∈ Q)

• I is the initial stack top symbol (I ∈ S)

• F is a set of accepting states (F ∈ Q)


BASIC STRUCTURE OF PDA
The following diagram shows a transition in a PDA from a state q1 to state
q2, labeled as a,b → c :

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.
Terminologies Related to PDA
Instantaneous Description
The instantaneous description (ID) of a PDA is represented by
a triplet (q, w, s) where
• q is the state
• w is unconsumed input
• s is the stack contents
Turnstile Notation
•The "turnstile" notation is used for connecting pairs of ID's
that represent one or many moves of a PDA.
•The process of transition is denoted by the turnstile symbol
"⊢".

-Consider a PDA (Q, Σ, S, δ, q0, I, F). A transition can be


mathematically represented by the following turnstile
notation:

Note: If we want zero or more moves of a PDA, we have to use the


symbol (⊢*) for it.
Pushdown Automata Acceptance
● Final State Acceptability
- a PDA accepts a string when, after reading the entire
string, the PDA is in a final state

For a PDA (Q, Σ, S, δ, q0, I, F), the language

accepted by the set of final states F is: L(PDA) = {w |

(q0, w, I) ⊢* (q, ε, x), q ∈ F}


for any input stack string x.
Pushdown Automata Acceptance
Empty Stack Acceptability
•Here a PDA accepts a string when, after reading the entire
string, the PDA has emptied its stack.

For a PDA (Q, Σ, S, δ, q0, I, F), the language

accepted by the empty stack is: L(PDA) = {w |

(q0, w, I) ⊢* (q, ε, ε), q ∈ Q}


Pushdown Automata Acceptance
EXAMPLE:
Construct a PDA that accepts L= {0n 1n | n ≥ 0}
Solution:
Pushdown Automata Acceptance
This language accepts L = {ε, 01, 0011, 000111, ............................. }
Here, in this example, the number of ‘a’ and ‘b’ have to be same.
· Initially we put a special symbol ‘$’ into the empty stack.
· Then at state q2, if we encounter input 0 and top is Null, we push 0 into stack. This may
iterate. And if we encounter input 1 and top is 0, we pop this 0.
· Then at state q3, if we encounter input 1 and top is 0, we pop this 0. This may also iterate.
And if we encounter input 1 and top is 0, we pop the top element.
· If the special symbol ‘$’ is encountered at top of the stack, it is popped out and it finally goes
to the accepting state q4.
Pushdown Automata Acceptance
Example:
Construct a PDA that accepts L= { wwR | w = (a+b)* }
Pushdown Automata Acceptance
Initially we put a special symbol ‘$’ into the empty stack. At state q2, the w
is being read. In state q3, each 0 or 1 is popped when it matches the
input. If any other input is given, the PDA will go to a dead state. When we
reach that special symbol ‘$’, we go to the accepting state q4.
PDA & Context-Free Grammar
•If a grammar G is context-free, we can build an equivalent
nondeterministic PDA which accepts the language that is produced by
the context-free grammar G
•if P is a pushdown automaton, an equivalent context-free grammar G can be
constructed where
L(G) = L(P)
In the next two topics, we will discuss how to convert from PDA to
CFG and vice versa
PDA & Context-Free Grammar
Algorithm to find PDA corresponding to a given CFG
Input: A CFG, G= (V, T, P, S)

Output: Equivalent PDA, P= (Q, Σ, S, δ, q0, I, F)

•Step 1 Convert the productions of the CFG into GNF.

•Step 2 The PDA will have only one state {q}.


•Step 3 The start symbol of CFG will be the start symbol in the PDA.
•Step 4 All non-terminals of the CFG will be the stack symbols of the PDA and all the
terminals of the CFG will be the input symbols of the PDA.
•Step 5 For each production in the form A→ aX where a is terminal and A, X are combination
of terminal and non-terminals, make a transition δ (q, a, A).
PDA & Context-Free Grammar
Algorithm to find CFG corresponding to a given PDA
Input: A CFG, G= (V, T, P, S)
Output: Equivalent PDA, P = (Q, Σ, S, δ, q0, I, F) such that the non-
terminals of the grammar G will be {Xwx | w,x ∈ Q} and the start state will
be Aq0,F.
•Step 1 For every w, x, y, z ∈ Q, m ∈ S and a, b ∈ Σ, if δ (w, a, e)
contains (y, m) and (z, b, m) contains (x, e), add the production rule Xwx
→ a Xyzb in grammar G.
•Step 2 For every w, x, y, z ∈ Q, add the production rule Xwx →
XwyXyx in grammar G.
•Step 3 For w ∈ Q, add the production rule Xww→ e in grammar G.
Pushdown Automata and Parsing
Parsing is used to derive a string using the production rules of a
grammar.

● Top-Down Parser: Top-down parsing starts from the top with the
start-symbol and derives a string using a parse tree.

● Bottom-Up Parser: Bottom-up parsing starts from the bottom


with the string and comes to the start symbol using a parse tree
Pushdown Automata and Parsing
Design of Top-Down Parser
For top-down parsing, a PDA has the following four types of transitions:
· Pop the non-terminal on the left hand side of the production at the
top of the stack and push its right-hand side string.
· If the top symbol of the stack matches with the input symbol being read,
pop it.
· Push the start symbol ‘S’ into the stack.
· If the input string is fully read and the stack is empty, go to the final state
‘F’.
Pushdown Automata and Parsing
Example:
Design a top-down parser for the
expression "x+y*z" for the grammar G with
the following production rules:
P: S → S+X | X, X → X*Y | Y, Y → (S) | id
Pushdown Automata and Parsing
Solution

If the PDA is (Q, Σ, S, δ, q0, I, F), then the top-

down parsing is: (x+y*z, I) ⊢(x +y*z, SI) ⊢ (x+y*z,

S+XI) ⊢(x+y*z, X+XI)


⊢(x+y*z, Y+X I) ⊢(x+y*z, x+XI) ⊢(+y*z, +XI) ⊢ (y*z, XI)
⊢(y*z, X*YI) ⊢(y*z, y*YI) ⊢(*z,*YI) ⊢(z, YI) ⊢(z, zI) ⊢(ε, I)
END

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