Lec 9 - Parsing Algo
Lec 9 - Parsing Algo
Lecture 9
Topics Covered in Lecture 8
• Concept about top down parsing
3
1. Recursive Descent Parsing
• A general form of top-down parsing where
backtracking may be involved.
• Involves repeated attempts to get the
correct output
4
How RDP Works ? Learn by
Example
• Consider the grammar:
– S -> cAd | bd
– A -> ab | a
• Input string is “cad”.
5
How RDP Works ? Learn by
Example
• Consider the S
grammar:
– S -> cAd | bd
– A -> ab | a c A d
c Matched ! !
6
How RDP Works ? Learn by
Example
• Consider the S
grammar:
– S -> cAd | bd
– A -> ab | a c A d
a Matched ! !
7
How RDP Works ? Learn by
Example
• Consider the S
grammar:
– S -> cAd | bd
– A -> ab | a c A d
8
How RDP Works ? Learn by
Example
S
• Consider the
grammar:
– S -> cAd | bd
c A d
– A -> ab | a
9
How RDP Works ? Learn by
Example
• Consider the S
grammar:
– S -> cAd | bd
c
– A -> ab | a A d
a Matched ! !
10
How RDP Works ? Learn by
Example
S
• Consider the
grammar:
– S -> cAd | bd c A d
– A -> ab | a
d Matched ! !
11
Problem with RDP
• EE+T
E
|E–T
|T
E + T
• Input string
2+5-9
E + T
E + T
Infinite loop
E + T
12
Hence, we can conclude
• A left recursive grammar shall cause a
recursive descent parser to go in an
infinite loop
• Therefore, we need to improve the
algorithm
13
Implementation: RD Parser
• View the production rule for a non terminal
symbol “A” as a definition for a procedure that
will recognize the symbol “A”.
• The RHS of the production rule for “A” specifies
the structure of the code for this procedure.
• The sequence of terminals and non terminals in
a choice correspond to matches of the input
string and calls to other procedures.
• While choices correspond to alternative (case or
if-stat) within the code
14
Implementation: RD Parser
• Let the grammar be:
S AB
AC|D
B eD | CD
Operation of RDP:
• In attempt to accomplish its goal for recognizing S (start symbol), it
will restate that goal as first recognizing “A” in the input sentence
and then recognizing “B” in the remainder part of the input string.
• In attempting to recognize “A”, it will use the second production.
• If it recognizes “C” or “D”, it will have achieved its goal of
recognizing “A”.
• In recognizing “B”, it will use third production rule.
• The goal will be satisfied, if the parser matches “eD” or “CD” with
the remaining part of input string.
• And so on…
15
2. Predictive Parsing
• To construct a predictive parser, we must know, given the current
input symbol “s” and the non-terminal “A” to be expanded, which
one of the alternatives of production is the unique alternative and
derives a string beginning with “s”.
– A α1 | α2 | …. | αn
• Proper alternative must be detectable by looking at only the first
symbol it derives.
• A special case of recursive descent parsing that needs no back
tracking
• Obtained as a result of eliminating left recursion from a grammar
and left factoring it.
• Implementation is very easy, uses the method discussed earlier.
16
Left Factoring
Let the grammar be:
Exp:
Aαβ|αγ
Let the grammar be:
where α is not null
A cD | cF
Can be redesigned:
Can be redesigned:
A α A1
A c A1
A1 β | γ
A1 D | F
17
Left Recursive
Let the grammar be: Exp:
AAα|β E→ E + T | T
Can be redesigned: Can be redesigned:
A β A’ A=E
α = +T
A’ α A’ | ε
β=T
So,
E T E’
E’ +T E’ | ε
18
Transition Diagram
• Purpose:
– Transition Diagram is used as a plan for
predictive parser
• One diagram for each non – terminal
• Edges are labeled by tokens or non
terminals
• Transition on a terminal
– Take transition if the token is the next input
symbol
• Transition on non – terminal
– Call of procedure for the non - terminal 19
How to construct a TD for
predictive parser
• Step 1 :
• Eliminate left – recursion from the grammar
• Step 2 :
• Left factor the grammar
• Step 3:
• For each non – terminal create an initial and final
state
• For each production create a path from an initial
state to the final state.
20
Learn by Example
• Original Grammar • Transformed Grammar
– E→ E + T | T – E TE ’
– T→ T * F | F – E’ + TE ’
– F→ ( E ) | id |Є
– T FT ’
– T’ * FT ’
|Є
– F(E)
| id
21
Learn by Example
• Grammar T E’
E: 0 1 2
– E TE’
– E’ + TE’ + T E’
E’: 3 4 5 6
|Є
– T FT’ Є
– T’ * FT’
|Є
– F (E)
| id
22
Learn by Example
• Grammar
– E TE’ F
T’
– E’ + TE’ T: 7 8 9
|Є
– T FT’ * F T’
T’: 10 11 12 13
– T’ * FT’
Є
|Є
– F (E)
| id
23
Learn by Example
• Grammar
( E )
– E TE’ 14 15 16 17
F:
– E’ + TE’
id
|Є
– T FT’
– T’ * FT’
|Є
– F (E)
| id
24
T E’
Finally E: 0 1 2
+ T E’
E’: 3 4 5 6
• Grammar
Є
F T’
– E TE’ T: 7 8 9
– E’ + TE’
|Є F T’
*
– T FT’ T’: 10 11 12 13
– T’ * FT’ Є
|Є ( E )
– F (E) F: 14 15 16 17
| id id
25
Lets Simplify and Optimize our
Transition Diagrams
Optimizing Transition Diagram –
Learn by Example
• Grammar
– E TE’ T E’
E: 0 1 2
– E’ +TE’
| + T E’
E’: 3 4 5 6
Є
– T FT’ Є
– T’ * FT’
|Є
Lets Optimize E !
– F (E)
| id
27
Optimizing Transition Diagram –
Learn by Example
+ T E’
• Grammar E’: 3 4 5 6
--E TE’ Є
– E’ + TE’ Є
|Є + T
E’: 3 4 5
– T FT’
– T’ * FT’ Є
6
|Є T
– F (E)
| id 3
+
4
E’:
Є
6 28
Optimizing Transition Diagram –
Learn by Example
• Grammar 0
T
1
E’
2
E:
T
– E TE’
– E’ + TE’ +
E’: 3 4
|Є
Є
– T FT’ 6
– T’ * FT’ T
|Є
– F (E) T +
0 3 4
| id E:
Є
6 29
Optimizing Transition Diagram –
Learn by Example
T
• Grammar
+
-- E TE’ E: 0
T
3 4
– E’ + TE’ Є
6
|Є
– T FT’
– T’ * FT’ +
|Є
T
– F (E) E: 0 3
| id Є
6
30
Optimizing Transition Diagram –
Learn by Doing
• Grammar
– E TE’
– E’ + TE’ T: 7 8 9
|Є
– T FT’ * F T’
T’: 10 11 12 13
– T’ * FT’
|Є Є
– F (E)
| id See if you can optimize T !
31
Optimizing Transition Diagram –
Learn by Doing
* F T’
T’: 10 11 12 13
• Grammar
Є
– E TE’
– E’ + TE’ Є
|Є * F
T’: 10 11 12
– T FT’
Є
– T’ * FT’ 13
|Є F
– F (E)
| id *
T’: 10 11
Є
13 32
Optimizing Transition Diagram –
Learn by Doing
F T’
• Grammar T: 7 8 9
– E TE’ F
– E’ + TE’
|Є *
T’: 10 11
– T FT’ Є
13
– T’ * FT’
|Є F
– F (E)
| id F *
T: 7 10 11
Є
13 33
Optimizing Transition Diagram –
Learn by Doing
F
• Grammar
F *
– E TE’ T: 7 10 11
– E’ + TE’ Є
13
|Є
– T FT’
*
– T’ * FT’
|Є F
T: 7 10
– F (E)
Є
| id 13
34
Optimizing Transition Diagram –
Learn by Doing
• Grammar
– E TE’
– E’ + TE’ ( E )
|Є F: 14 15 16 17
– T FT’ id
– T’ * FT’
|Є
See if you can optimize F !
– F (E)
| id Can not be further optimized!!!
35
Optimized Transition
+ Diagrams
• Grammar E: 0
T
3
Є
* 6
– E TE’
– E’ + TE’ F
7 10
|Є T:
Є
– T FT’ 13
– T’ * FT’
( E )
|Є F: 14 15 16 17
– F (E) id
| id
36
Why should we do optimizing at
all ?
• Because !!!
• An implementation of un - optimized
transition diagram for a predictive parser is
20 % slower than that of a optimized
transition diagram !!!
37
Exercise
• Grammar
• Set of terminals
Expr Expr + Term – { +, -, *, /, Int }
| Expr - Term
• Set of Non terminals
| Term
– { Start, Expr, Term }
38
Let’s Revise !!!
Recursive Descent Parsing
• A general form of top-down parsing where
backtracking may be involved.
• Involves repeated attempts to get the
correct output
• brute-force type of parsing
• outdated, just because there are much
better methods of parsing which we will be
discussing later
40
• Why does Recursive Descent Parser fail
at particular conditions ?
41
Predictive Parsing
• A special case of recursive descent
parsing that needs no back tracking
44
• Need for optimizing Transition Diagrams
45
Homework :
• The language for which you have built your
lexical analyzer must have some rules for
construction of valid statements.
• Explain the rules in terms of productions (e.g.
the example done for TD was a rule for
arithmetic expressions)
• Make Transition Diagrams and optimize them
• This will become the first step towards
building your parser !!
46