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

Module 4

Uploaded by

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

Module 4

Uploaded by

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

Pushdown Automata

Just Recognizing
We need a device similar to an FSM except that it
needs more power.
The insight: Precisely what it needs is a stack, which
gives it an unlimited amount of memory with a
restricted structure.
Example: The balanced parentheses language

(((()))
Definition of a Pushdown Automaton
M = (K, , , , s, A), where:
K is a finite set of states
 is the input alphabet
 is the stack alphabet
s  K is the initial state
A  K is the set of accepting states, and
 is the transition relation. It is a finite subset of

(K  (  {})  *)  (K  *)

state input or  string of state string of


(current) symbols (next) symbols
to pop to push
from top on top
of stack(top of stack) of stack
A PDA for AnBn = {anbn: n  0}
Manipulating the Stack
c will be written as “cab” if the input is “bac”

If input baccn…c2c1 is pushed onto the stack:

c1
c2

cn
c
a
b

c1c2…cncab
Configuration & Computations\Instantaneous
description(ID)
A configuration of M is an element of K  *  *.
The initial configuration of M is (s, w, ).
A computation by M is a finite sequence of configurations
C0, C1, …, Cn for some n  0 such that:
● C0 is an initial configuration,
● Cn is of the form (q, , ), for some state q  KM and
some string  in *, and
● C0 |-M C1 |-M C2 |-M … |-M Cn.
Deterministic PDA(DPDA):
Nondeterminism
If M is in some configuration (q1, s, ) it is possible that:

● contains exactly one transition that matches.

● contains more than one transition that matches.

● contains no transition that matches.


Accepting
A computation C of M is an accepting computation iff:

● C = (s, w, ) |-M* (q, , ), and


● q  A.

M accepts a string w iff at least one of its computations accepts.

Other paths may:


● Read all the input and halt in a nonaccepting state,
● Read all the input and halt in an accepting state with the stack not
empty,
● Loop forever and never finish reading the input, or
● Reach a dead end where no more input can be read.

The language accepted by M, denoted L(M), is the set of all strings


accepted by M.
Rejecting
A computation C of M is a rejecting computation iff:

●C = (s, w, ) |-M* (q, w, ),


● C is not an accepting computation, and
● M has no moves that it can make from (q, , ).

M rejects a string w iff all of its computations reject.

** So note that it is possible that, on input w, M neither


accepts nor rejects.
A PDA for AnBn = {anbn: n  0}
A PDA for Balanced Parentheses

M = (K, , , , s, A), where:


K = {s} the states
 = {(, )} the input alphabet
 = {(} the stack alphabet
A = {s}
 contains: (s,(,ε)=(s,( )
(s,),()=(s, ε)
A PDA for {wcwR: w  {a, b}*}

M = (K, , , , s, A), where:


K = {s, f} the states
 = {a, b, c} the input alphabet
 = {a, b} the stack alphabet
A = {f} the accepting states
 contains: ((s, a, )= (s, a))
((s, b, )= (s, b))
((s, c, )= (f, ))
((f, a, a)= (f, ))
((f, b, b)= (f, ))
A PDA for {anb2n: n  0}
Exploiting Nondeterminism
A PDA M is deterministic iff:
● M contains no pairs of transitions that compete with each other, and
● Whenever M is in an accepting configuration it has no available moves.

But many useful PDAs are not deterministic.


A PDA for PalEven ={wwR: w  {a, b}*}
S→
S → aSa
S → bSb

PDA:
A PDA for PalEven ={wwR: w  {a, b}*}
S→
S → aSa
S → bSb

PDA:
A PDA for {w  {a, b}* : #a(w) = #b(w)}
A PDA for {w  {a, b}* : #a(w) = #b(w)}
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}

Start with the case where n = m:


b/a/
a//a

b/a/
1 2
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}

Start with the case where n = m:


b/a/
a//a

b/a/
1 2

● If stack and input are empty, halt and reject.

● If input is empty but stack is not (m > n) (accept):

● If stack is empty but input is not (m < n) (accept):


More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}

b/a/
a//a

b/a/
1 2

● If input is empty but stack is not (m > n) (accept):

a//a b/a/ /a/

b/a/ /a/
1 2 3
More on Nondeterminism
Accepting Mismatches
L = {ambn : m  n; m, n > 0}

b/a/
a//a

b/a/
1 2

● If stack is empty but input is not (m < n) (accept):

b/a/ b//
a//a

b/a/ b//
1 2 4
Putting It Together
L = {ambn : m  n; m, n > 0}
Module 4-
Syntax Analysis

1
BOTTOM UP PARSING
• It corresponds to the construction of parse
tree for an input at the leaves and working
up towards the root.
• Bottom-up parsers in general use explicit
stack.
• They are also known as shift reduce
parsers.
• They give right most derivation in reverse.
• Derivation v/s reduction
2
Example (BUP=RMD in reverse )
E → T, T→ T*F | F, F→ id
E → T → T*F → T*id → F *id →id*id (RMD)

3
Example problem
Write bottom up parses for bbb*b++ using
E→ EE+|EE*|b

4
Handle and Handle Pruning

• Handle is a substring that matches the body of a


production, and whose reduction represents one
step along the reverse of a right most derivation.
• Formal definition of handle: If S→ αAw → αβw,
then production A→ β in the position following α is
a handle of αβw.

5
Handle Pruning
• RMD in reverse can be obtained by handle pruning.
• If w is a sentence of grammar at hand, then let w= γn ,
where γn is the nth right sentential form of some unknown
RMD.
• S => γ0 => γ1 => γ2……….. γn-1 => γn = w
• To reconstruct this derivation in reverse order, we locate
the handle βn in γn and replace βn by the head of the
production An => βn to obtain the previous right sentential
form γn-1. Repeat this process. That is we locate the handle
βn-1 in γn-1 and obtain γn-2. By continuing this process we
produce a right sentential form consisting only of the start
symbol S, then halt and announce successful completion
of parsing.

6
Problems on handles
• Locate the handles for the given right
sentential forms.
• 1) 000111 using S→ 0S1 | 01

• 2) a) SSS+a*+ using S→SS+|SS*|a


b) aaa*a++ using S→SS+|SS*|a

7
Shift Reduce Parsing

• 4 possible actions a shift reduce parser can make:


• Shift: Shift the next input symbol onto the stack.
• Reduce: The right end of the string to be reduced
must be at the top of the stack. Locate the left end
of the string within the stack and decide with what
nonterminal to replace the string.
• Accept: announce successful completion of
parsing.
• Error: discover a syntax error and call an error
recovery routine.

8
Example

• E → T, T→ T*F | F, F→ id

9
Example

E → T, T→ T*F | F, F→ id
Stack Input Action
Input id1+*id2 $ id1+*id2 $

10
E → E+T | T, T → T*F | F, F → (E) | id
input id+id*id$ Stack Input Action
$ id+id*id$

11
Conflicts during shift reduce parsing

• Shift-reduce: cannot decide whether to shift or reduce.


• Ex: consider the grammar S → i E t S | i E t S e S | other
• If we have a shift reduce parser in configuration
Stack input
. . . .i E t S e...$
• We cannot tell whether i E t S is the handle, no matter what
appears below it on the stack. Here there is a shift reduce
conflict. Depending on what follows the ‘e’ on the input, it
might be correct to reduce i E t S to S, or it might be
correct to shift ‘e’ and then look for another S to complete
the alternative i E t S e S.

12
• Reduce-reduce: cannot decide which of
several reductions to make.
• Ex: If there are two productions like A→ B
and C → B, and stack contains B on the top,
then parser might be confused to reduce B
by A or B by C.

13
Introduction to LR parsing: Simple LR

• LR parsers can be constructed to recognize all


programming language constructs for which CFGs
can be written.
• LR parsing method is the most general non
backtracking shift reduce parsing method.
• An LR parser can detect a syntactic error as soon
as it is possible to do so on a left to right scan of
input.

14
Items and LR(0) automaton:

• How does a shift reduce parser know when to shift


and when to reduce?
• An LR parser makes shift reduce decisions by
maintaining states to keep track of where we are
in a parse. States represent sets of “items”.
• An LR(0) item of a grammar G is a production of G
with a dot at some position of the body.
• For example a production A → XYZ yields four
items
• A → . XYZ, A → X .YZ, A → XY. Z,
A → XYZ .
15
Canonical LR(0) collection
• Provides the basis for constructing a
deterministic finite automaton that is used to
make parsing decisions.
• CLOSURE and GOTO:
• 2 functions which are essential to construct
canonical LR(0) automaton.

16
CLOSURE of item sets
• If I is set of items for a grammar G, then
CLOSURE(I) is the set of items constructed from I
by the 2 rules:
• Initially, add every item(with “dot”) in I to
CLOSURE(I).
• If A → α . Bβ is in CLOSURE(I) and B → γ is a
production, then add the item B → . γ to
CLOSURE(I), if it is already not there. Apply this
rule until no more new items can be added to
CLOSURE(I).
• Ex: E→ E+T|T, T→ T*F|F, F→(E)|id
find closure(E).
17
• Kernel items: the initial item, S’ → . S, and all items whose
dots are not at the left end.
• Non kernel items: all items with their dots at the left end,
except for S’ → . S

18
The function GOTO
• GOTO(I,X)- where I is a set of items and X
is a grammar symbol. GOTO(I,X) is defined
to be the closure of the set of all items [A→
α . Xβ] is in I.
• GOTO function is used to define the
transitions in the LR(0) automaton for a
grammar.
• The states of automaton correspond to sets
of items, and GOTO(I,X) specifies the
transition from the state for I under input X.
19
Algorithm for sets of LR(0) items

20
Problems
• Construct LR(0)/ SLR automaton for the
following grammars.
1) S→ SA | A
A→ a
2) S→ S;T | T
T→a

21
LR parsing algorithm

22
Constructing SLR Parsing Table

23
24
Example
Consider the grammar below:
E → E+T | T
T → T*F | F
F → (E) | id
i) Construct LR(0) automaton by computing all
LR(0) item sets and GOTO.
ii) Construct SLR parsing table.
iii) Show the moves made by the string
“id+id*id”
25
SLR Parsing Table

26
Moves made by “id*id+id”

27
For the following problems,
i)Construct LR(0) automaton by computing all
LR(0) item sets and GOTO.
ii) Construct SLR parsing table.
iii) Show the moves made by a string.
1) S→ CC 2) S→S;T | T
C→ cC |d T→ a
String: ccdcd string: a;a
4) A→ Ac 3) A→(A) | a
A→AB|ε string: ((a))
B→ aB | b (hint: remove ε production) 28
5) S→L=R | R
L→*R | id
R→ L
String: *id=*id

6) S→ AS | b
A→SA | a
String: abab
Note: shift-reduce/reduce-reduce conflicts
occur in problem 5 and 6
29
LALR PARSING

30

You might also like