0% found this document useful (0 votes)
43 views24 pages

Compiler Construction Lecture 12 Predictive Parsing-Step1

The document discusses parsing in compilers, including recursive descent parsing and predictive parsing. It describes the process of parsing, problems with recursive descent parsing like backtracking and non-termination, and how predictive parsing solves these problems by looking ahead one token. Examples are provided of converting left recursive grammars to right recursive grammars.

Uploaded by

MalAika Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views24 pages

Compiler Construction Lecture 12 Predictive Parsing-Step1

The document discusses parsing in compilers, including recursive descent parsing and predictive parsing. It describes the process of parsing, problems with recursive descent parsing like backtracking and non-termination, and how predictive parsing solves these problems by looking ahead one token. Examples are provided of converting left recursive grammars to right recursive grammars.

Uploaded by

MalAika Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Compiler Construction

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

Code Optimizer Back


Target Code Generator
End
Target Language
Source Language

Now! Lexical
Analyzer
Syntax Analyzer Front
End
Semantic
Analyzer
Int. Code Generator

Intermediate Code

Code Optimizer Back


Target Code Generator
End
Target Language
Parsing
• Parsing is the process of determining how a string of terminals can be
generate by a grammar.
• For any context-free grammar there is a parser that takes at most O(n3)
time to parse a string of n terminals.
• Real programming languages, we can generally design a grammar that
can be parsed quickly.
• Linear-time algorithms
• Programming-language parsers almost always make a single left-to-right
scan over the input, looking ahead one terminal at a time, and
constructing pieces of the parse tree as they go.

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
• EE+T Input string: x – y * z
• EE–T
• ET E E – T
• TT*F E–T–T
• E–T–T–T
TT
E – T – T – T – T
• TF and so on
• Fx|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
• EE–T • ET–E
• EE+T
• ET
• TT*F
• TT/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
• EE+T|T
For
• TT*F|F A  A | 
write:
• F   | id A   A’
A’  A’ | 

20
Example-1 Solution
• EE+T|T
For
• TT*F|F A  A | 
write:
• F   | id A   A’
A’  A’ | 

• A = E,  = +T,  = T ……… A = T,  = *F, 


=F
• E  TE’ ……………………….. T  FT’
• E’  +T E’ |   T’  *FT’ | 
• F id|  21
Example-2
• S  Sab
For
• Input string: ababab A  A

write:
A   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

• Chapter 4 of the DragonBook  4.3,4.4

24

You might also like