Docsity

Top-Down and Bottom-Up Parsing Techniques, Slides of Compiler Construction

Partial preview of the text

Download Top-Down and Bottom-Up Parsing Techniques and more Slides Compiler Construction in PDF only on Docsity!

Parsing

Techniques

2

Parsing Techniques

Top-down parsers  Start at the root of the parse tree and grow towards leaves.  Pick a production and try to match the input

4

Parsing Techniques

Top-down parsers  Start at the root of the parse tree and grow towards leaves.  Pick a production and try to match the input

5

Parsing Techniques

Top-down parsers  Bad “pick” ⇒ may need to backtrack  Some grammars are backtrack-free.

7

Parsing Techniques

Bottom-up parsers  Start at the leaves and grow toward root  As input is consumed, encode possibilities in an internal state.

8

Parsing Techniques

Bottom-up parsers  Start at the leaves and grow toward root  As input is consumed, encode possibilities in an internal state.

11

Parsing Techniques

Bottom-up parsers  Start in a state valid for legal first tokens  Bottom-up parsers handle a large class of grammars

12

Parsing Techniques

Bottom-up parsers  Start in a state valid for legal first tokens  Bottom-up parsers handle a large class of grammars

14

Top-Down Parser

 A top-down parser starts with the root of the parse tree.  The root node is labeled with the goal symbol of the grammar

15

Top-Down Parsing Algorithm

 Construct the root node of the parse tree  Repeat until the fringe of the parse tree matches input string

17

Top-Down Parsing

 At a node labeled A , select a production with A on its lhs  for each symbol on its rhs, construct the appropriate child

18

Top-Down Parsing

 At a node labeled A , select a production with A on its lhs  for each symbol on its rhs, construct the appropriate child

20

Top-Down Parsing

 When a terminal symbol is added to the fringe and it does not match the fringe, backtrack  Find the next node to be expanded

21

Top-Down Parsing

 The key is picking right production in step 1.

 That choice should be guided by the input string