




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Source
We have a grammar
Product
We want a parsing algorithm
Idea
Synthesize the algorithm from the grammar
Problem
How are we going to use the grammar ???
Hint...
We can look at the beginning of the input...
Technique
Use a window of lookahead Guide a LEFTMOST derivation with the window content
So...
How to guide?
Lookahead
Predictive tool! Helps to select the right production
Question
How?
Answered with an example...
Use the production body
To know what can start a production
Selecting a production
Pick the rule that can start with the symbol in the lookahead window!
Using the production
What should we do with chosen production?
Production^ while expr do stmt
Input (^) while x do { if x then ....
Matching
When input matches
Eat the matched symbols In the production In the input Move the window further down Deal with the rest of the production How?
Production^ while expr do stmt
Input (^) while x do { if x then ....
Simple...
The non-terminal is defined somewhere The non-terminal is defined by a set of productions
Corollary
Pop the non-terminal Use the lookahead to choose a production for NT. Push and Recur
Recall the grammar and the current state
Production^ while expr do stmt
Input (^) while x do { if x then ....
stmt → if expr then stmt else stmt .... lvalue → id expr → id | integer
What can happen in the end?
We “eat” (match) the entire input Meaning? We get “stuck” at some point What does it mean (to be stuck) How can we get stuck?
We can get stuck
Lookahead window and topmost non-terminal yield... An empty prediction! This is the “classic” syntax error
Expecting xyz
Syntax error at file:line. Expecting xyz got abc
Almost....
A Few Remaining issues...
How do we get the predictions from the grammar? What should we do if the same symbol predict
1 rule? How can we implement the algorithm above? How large should the lookahead window be? Why is this called “top-down”? What can be automated about all this?
How large?
Very good question.
The art of counting
1,2, a lot.
Conclusion
Usually, 1 token is plenty Occasionally, 2 tokens may be needed Beyond 2 is wild
In theory
Any finite constant will do 1,2,3....,k
Identify a leftmost derivation for an input string
Why?
By always replacing the leftmost non-terminal symbol via a production rule, we are guaranteed of developing a parse tree in a left-to-right fashion consistent with scanning the input. A ⇒ aBc ⇒ adDc ⇒ adec (scan a, scan d, scan e, scan c - accept!)
Recursive-descent parsing concepts
Predictive parsing
Recursive / Brute force technique non-recursive / table driven
Error recovery
Implementation Docsity.com
Identify a leftmost derivation for an input string
Why?
By always replacing the leftmost non-terminal symbol via a production rule, we are guaranteed of developing a parse tree in a left-to-right fashion consistent with scanning the input. A ⇒ aBc ⇒ adDc ⇒ adec (scan a, scan d, scan e, scan c - accept!)
Recursive-descent parsing concepts
Predictive parsing
Recursive / Brute force technique non-recursive / table driven
Error recovery
Implementation Docsity.com