Compiler Construction Lecture 12 Predictive Parsing-Step1
Compiler Construction Lecture 12 Predictive Parsing-Step1
Lecture-12
1
Today’s Agenda
• Parsing
2
Source Language
Lexical
Analyzer
Syntax Analyzer Front
Structure Semantic
End
of a Analyzer
Int. Code Generator
Compiler Intermediate Code
Now! Lexical
Analyzer
Syntax Analyzer Front
End
Semantic
Analyzer
Int. Code Generator
Intermediate Code
5
Parsing
• Two types of parsing
– Top-down parsing
1. Recursive-descent parsing
2. Predictive parsing
– Bottom-up parsing (To be studied later)
6
Top-Down Parsing
• Constructing a parse tree starting from the root and
creating the nodes of the parse tree in pre-order.
• Recursive-descent parsing (Recursive means
repetition-descent means moving downwards)
– general form of top-down parsing,
– requires backtracking to find the correct production to be
applied.
• Predictive parsing,
– a special case of recursive-descent parsing,
– no backtracking required.
– chooses the correct production by looking ahead at the
input till next symbol
7
Recursive-descent parsing
• Recursive-descent parsers are also called top-down parsers,
since they construct the parse tree top down (rather than
bottom up).
• The basic idea of recursive-descent parsing is to associate
each non-terminal with a procedure.
• The goal of each such procedure is to read a sequence of
input characters that can be generated by the corresponding
non-terminal, and return a pointer to the root of the parse
tree for the non-terminal. The structure of the procedure is
dictated by the productions for the corresponding non-
terminal.
8
Problems with Recursive-descent
parsing
• There are two problems :
– Back Tracking
– Non-termination
9
Back-Tracking
• There are many production rules for a
single non-terminal.
• Parser can chose any production rule
to replace.
• If it finds that the decision is incorrect, it goes
back and tries again.
• This is time consuming, specially for
complex grammars with thousands of
production rules.
10
Non-termination
• The parser chooses the first production again
and again. It traps into infinite loop.
• This problem occurs due to left recursive
grammar.
11
Let’s try: Back-Tracking
E E op E | id
op + | - | * | /
Id x | y | z
• Input string: x
+y*z
12
Let’s try: Non-termination
• EE+T Input string: x – y * z
• EE–T
• ET E E – T
• TT*F E–T–T
• E–T–T–T
TT
E – T – T – T – T
• TF and so on
• Fx|y|z
13
Solution: Predictive parsing
• Parser has to look ahead one more
token, then it can predict the correct
production.
• Also called LL1 parsing
– Here the 1st L represents that the scanning of the
Input will be done from Left to Right manner.
– The second L shows that in this parsing technique
we are going to use Left most Derivation Tree.
– And finally, the 1 represents the number of look-
ahead, which means how many symbols are you
going to see when you want to make a decision.
14
Predictive parsing Algorithm
• Steps:
1. Convert left-recursive grammar to right-
recursive grammar
2. Convert non-deterministic grammar to
deterministic grammar
3. Find out First and Follow sets of the
production
4. Construct LL1 parsing table
15
Predictive Parsing
• Predictive parsing
– Parser has to look ahead one more token, then it can
predict the correct production.
– Also called LL1 parsing
• Steps:
1. Convert left-recursive grammar to right-recursive
grammar
2. Convert non-deterministic grammar to deterministic
grammar
3. Find out First and Follow sets of the production
4. Construct LL1 parsing table
16
Two types of grammar
• Left – recursive grammar
– The non-terminal on the left side of the
production appears to the left of the right side of
the production :-P
• Right – recursive grammar
– The non-terminal on the left side of the
production appears to the right of the right side of
the production
17
Examples
Left – recursive grammar Right – recursive grammar
• A Aa • A aA
• S SS* • S abS
• EE–T • ET–E
• EE+T
• ET
• TT*F
• TT/F
18
Elimination of Left Recursion
• Top-down parsing methods cannot handle
left-recursive grammars,
• Rule for Transformation to right recursion:
– If we have A A | we follow this
formula:
A A’
A’ A’ |
19
Example-1
• EE+T|T
For
• TT*F|F A A |
write:
• F | id A A’
A’ A’ |
20
Example-1 Solution
• EE+T|T
For
• TT*F|F A A |
write:
• F | id A A’
A’ A’ |
22
Example-2 Solution
• S Sab
For
• Input string: ababab A A
write:
• A= S, = ab A A’
A’
• S S’ A’ A’
•
S’ abS’
• S’
23
Reference/Self Study
• Chapter 2 of the DragonBook 2.4
24