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

ATCD PPT Module-4

- LL parsing does a top-down, leftmost derivation while LR parsing does a bottom-up, rightmost derivation in reverse. - LL parsing starts with the root nonterminal on the stack and ends when the stack is empty. LR parsing starts with an empty stack and ends with the root nonterminal on the stack. - LL parsing uses the stack to keep track of what is still expected, while LR parsing uses the stack to keep track of what has already been seen.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

ATCD PPT Module-4

- LL parsing does a top-down, leftmost derivation while LR parsing does a bottom-up, rightmost derivation in reverse. - LL parsing starts with the root nonterminal on the stack and ends when the stack is empty. LR parsing starts with an empty stack and ends with the root nonterminal on the stack. - LL parsing uses the stack to keep track of what is still expected, while LR parsing uses the stack to keep track of what has already been seen.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

21CS51 – AUTOMATA THEORY AND COMPILER DESIGN

MODULE - 4
PUSH DOWN AUTOMATA(PDA)
Syntax analysis Phases of compilers part 2

1
Pushdown Automata(PDA)
Pushdown Automata(PDA)
A Pushdown Automaton (PDA) is a 7-tuple (Q, Σ, Γ, δ, q0, Z0, F) where:

Q: A finite set of states.


Σ: A finite alphabet, which represents the input symbols.
Γ: A finite stack alphabet, which includes the symbols that can be pushed onto
and popped from the stack.
δ: The transition function, which is a partial function from (Q × (Σ ∪ {ε}) × Γ) to
2^(Q × Γ*), where ε represents an empty string. In other words, δ(q, a, X) = { (p, Y)
| (q, a, X) → (p, Y) } for some transition (q, a, X) → (p, Y).
q0: The initial state, where q0 ∈ Q.
Z0: The initial stack symbol, which is the symbol initially on the stack when it is
empty, where Z0 ∈ Γ.
F: A set of accepting states, where F ⊆ Q.
Pushdown Automata(PDA)
The actions performed by PDA depends on
1. The current state
2. The next input symbol
3. The symbol on top of the stack

The action performed by the machine consist of


1. Changing the states from one state to another
2. Replacing the symbol on the stack

δ (state, i/p_symbol, stack_symbol)=(next_state, stack_symbol)


Pushdown Automata(PDA)
Instantaneous Description of PDA:
The Instantaneous description is called as an informal notation, and
explains how a Push down automata (PDA) computes the given input
string and makes a decision that the given string is accepted or
rejected.
• The PDA involves both state and content of the stack.
• Stack is often one of the important parts of PDA at any time.
• So, we make a convenient notation for describing the successive
configurations of PDA for string processing.
Pushdown Automata(PDA)
Instantaneous Description of PDA(cont..)
• The factors of PDA notation by triple (q, w, γ) were
• q is the current state.
• w is the remaining input alphabet.
• γ is the current contents of the PDA stack.

• Generally, the leftmost symbol indicates the top of the stack γ and the
bottom at the right end.
• This type of triple notation is called an instantaneous description or
ID of the pushdown automata
Pushdown Automata(PDA)
Languages by PDA:
There are two cases wherein a string w is accepted by PDA.
Get the final state from the start state.
Get an empty stack from the start State.
We say that the language is accepted by the final state and the second
case we say that the language is accepted by an empty stack.
Pushdown Automata(PDA)
Example:
For input string w = “aabb” of PDA where,
M = ({q0, q1, q2}, {a, b}, {a, b, Z0}, δ, q0, Z0, {q2}),
Where δ is defined as follows
δ(q0, a, Z0) = {(q0, aZ0)} Rule (1)
δ(q0, a, a) = {(q0, aa)} Rule (2)
δ(q0, b, a) = {(q1, Ꜫ)} Rule (3)
δ(q1, b, a) = {(q1, Ꜫ)} Rule (4)
δ(q1, Ꜫ, Z0) = {(q2, Ꜫ)} Rule (5)
Pushdown Automata(PDA)

(q0, aabb, Z0)


|- (q0, abb, aZ0) based on Rule (1)
|- (q0, bb, aaZ0) based on Rule (2)
|- (q1, b, aZ0) based on Rule (3)
|- (q1, Ꜫ, Z0) based on Rule (3)
|- (q2, Ꜫ, Ꜫ) based on Rule (5)
Pushdown Automata(PDA)

Deterministic and Non-deterministic PDA:


Deterministic PDA:
Let M = (Q, Σ, Γ, δ, q0, Z0,F) be a PDA.
The PDA is deterministic if
i) δ(q,a,Z) has only one element.
ii) If δ(q, Ꜫ ,Z) is not empty, then (q,a,Z) should be empty.

If both conditions are satisfied, then the PDA is deterministic.


A Graphical notations for PDA

• The nodes correspond to the states of the PDA.


• An arrow labeled Start indicates the unique start state.
• Doubly circled states are accepting states.
• Edges correspond to transitions in the PDA as follows:
• An edge labeled (ai, tm)/tn from state q to state p means that d(q, ai, tm)
contains the pair (p, tn), perhaps among other pairs.
A Graphical notations for PDA
Graphical notation for the PDA for the language L = wwr

All possibilities that do


not have explicit edges,
have implicit edges that
go to an implicit reject
state.
The Languages of a PDA.

Acceptance by Final State:


The PDA is said to accept its input by the final state if it enters any final state
in zero or more moves after reading the entire input.
• Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language acceptable by the final
state can be defined as:
• L(PDA) = {w | (q0, w, Z) ⊢* (p, ε, ε), q ∈ F}
The Languages of a PDA.

Acceptance by Empty Stack:


• There is another method for accepting words with a pushdown automaton.
• It is called "acceptance by empty stack".
• In this case, the automaton does not have any final states, and the word is
accepted by the pushdown automaton if and only if it can read the whole
word and the stack is empty when the end of the input word is reached.
More formally, the language accepted by automaton
Bottom up parsing
Introduction to LR Parsing :SLR , More Powerful LR parsers
Parsing
• Syntax analyzers follow production rules defined by means of context-
free grammar. The way the production rules are implemented
(derivation) divides parsing into two types : top-down parsing and
bottom-up parsing.
Bottom-up Parsing
• As the name suggests, bottom-up parsing starts with the input symbols and tries to
construct the parse tree up to the start symbol.
• Example:
• Input string : a + b * c
• Production rules:
• S→E
• E→E+T
• E→E*T
• E→T
• T → id
• Let us start bottom-up parsing
• a+b*c
• Read the input and check if any production matches with the input:
• a+b*c
• T+b*c
• E+b*c
• E+T*c
• E*c
• E*T
• E
• S
• The key decision during bottom up parsing are “when to reduce and about what
production to apply “ as the parse proceeds.

• EX: The above strings shows sequences of reduction, then reduction can be
expressed in terms of sequence of strings like

• id*id, F*id, T*id, T*F, T,E.

• If the substring is chosen correctly at each step, the reduction steps could be an
exact reverse of RMD.
Handle Pruning
1. What is mean by handle pruning? How it helps in shift reduce
parsing? List the action of a shift reduce parser.
2.What is handle and handle pruning? Show the working of a shift
reduce parser for
• accepting id1*id2 considering the grammar
• E→E + T |T , T→T*F |F,
F→ ( E ) |id Ans:
• During BUP , a left to right scan of the input construct a right most
derivation in reverse.
• Informally, “A Handle is a substring that matches the body of
production and whose reduction represents one step along the reverse
of a RMD.”
• “A RMD in reverse called canonical reduction sequence is obtained
by the process of handle pruning”
1. Give BUP parse tree for the following input=abbcde for grammar S→aAcBe ,A→AB|b ,B→d

.Find the handle for the above string each with right sentential form and also write shift-reduce
configuration for the same input.

Solution: 1. Handle prunning


2.Shift-Reduce Configuration
Stack Input Production
$ abbcde$ Shift ‘a’
$a bbcde$ Shift ‘b’
$ab bcde$ Reduce A→b
$aA bcde$ Shift ‘b’
$aAb cde$ Reduce A→Ab
$aA cde$ Shift ‘c’
$aAc de$ Shift ‘d’
$aAcd e$ Reduce B→d
$aAcB e$ Shift ‘e’
$aAcBe $ Reduce S→aAcBe
$S $ Accept
3. Construct bottom up tree for input
• There are 2 rules as follows,
1. When an input symbol ‘a’ shift on to the stack a new node is created and it is
labeled as ‘a’.
2. When X1,X2, …..Xn is reduced to ‘A’ , a new node labled ‘A’ is created whose
children's are X1,X2….Xn.
S

a A B e
A b d
b c
Conflicts during shift reduce parsing

1. Shift- Reduce Conflict


2. Reduce –Reduce Conflict
LR Parser
❖The LR parser is a non-recursive, shift-reduce, bottom-up parser.

❖ It uses a wide class of context-free grammar which makes it the


most efficient syntax analysis technique.

❖ LR parsers are also known as LR(k) parsers, where L stands for


left-to-right scanning of the input stream, R stands for the construction
of right-most derivation in reverse, and k denotes the number of
lookahead symbols to make decisions.
• There are three widely used algorithms available for constructing an LR parser:

• SLR(1) – Simple LR Parser:


• Works on smallest class of grammar
• Few number of states, hence very small table
• Simple and fast construction

• LR(1) – LR Parser:
• Works on complete set of LR(1) Grammar
• Generates large table and large number of states
• Slow construction

• LALR(1) – Look-Ahead LR Parser:


• Works on intermediate size of grammar
• Number of states are same as in SLR(1)
Difference between LL and LR
LL LR

Does a leftmost derivation. Does a rightmost derivation in reverse.

Starts with the root nonterminal on the stack. Ends with the root nonterminal on the stack.

Ends when the stack is empty. Starts with an empty stack.


Uses the stack for designating what is still to be Uses the stack for designating what is already seen.
expected.
Builds the parse tree top-down. Builds the parse tree bottom-up.

Continuously pops a nonterminal off the stack, and Tries to recognize a right hand side on the stack,
pushes the corresponding right hand side. pops it, and pushes the corresponding nonterminal.

Expands the non-terminals. Reduces the non-terminals.


Reads the terminals when it pops one off the stack. Reads the terminals while it pushes them on the
stack.
Pre-order traversal of the parse tree. Post-order traversal of the parse tree.
LL V/S LR Parsing
1.LR(0) Automata for grammar with closure and GOTO ( ).
E→E+T |T, T→T*F|F ,F→(E) |id
Construction of SLR parsing table using Action and GOTO ()
2.LR(0) Automata for grammar with closure and GOTO ( ),
S→L=R | R, L→*R|id, R→L
Construction of SLR parsing table

You might also like