Docsity

Top-Down Parsing - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Top-Down Parsing - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 4: Syntax Analysis

Part 2: Top-Down Parsing

Motivation

 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...

Lookahead

 Technique

 Use a window of lookahead  Guide a LEFTMOST derivation with the window content

 So...

 How to guide?

Predictive Parsing

 Lookahead

 Predictive tool!  Helps to select the right production

 Question

 How?

 Answered with an example...

Key Idea

 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

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 ....

Dealing with non-terminals

 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

On the Example

 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

  1. Choose production for expr result: expr → id
  2. Push & Recur... result:^ Production while id do stmt Input (^) while x do { if x then ....
  3. Pop result: expr

Final outcome

 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?

Negative outcome

 We can get stuck

 Lookahead window and topmost non-terminal yield...  An empty prediction!  This is the “classic” syntax error

 Expecting xyz

  • A list of tokens predicting the productions of the current non-terminal  Got abc
  • “abc” is the actual content of the lookahead window

Syntax error at file:line. Expecting xyz got abc

Are We Done?

 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?

The Lookahead Window

 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

Top-Down Parsing

 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

Top-Down Parsing

 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