Module 4
Module 4
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
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:
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}
b/a/
1 2
More on Nondeterminism
Accepting Mismatches
L = {ambn : m n; m, n > 0}
b/a/
1 2
b/a/
a//a
b/a/
1 2
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
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
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
7
Shift Reduce Parsing
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
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
14
Items and 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