Docsity

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

Toggle sidebar
bg1
Chapter 4: Syntax Analysis
Part 2: Top-Down Parsing
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

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