Tree Generation - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Tree Generation

Description:

Y; else Write (Y ); end; Top-Down Generation of Derivation Tree ... Y; Write(E TY); end; Bottom-up Generation of the Derivation Tree (cont'd) proc Z; {Z SZ ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 27
Provided by: manuelb9
Learn more at: https://www.cise.ufl.edu
Category:
Tags: generation | tree

less

Transcript and Presenter's Notes

Title: Tree Generation


1
Tree Generation
Programming Language Principles Lecture 5
  • Prepared by
  • Manuel E. Bermúdez, Ph.D.
  • Associate Professor
  • University of Florida

2
String-To-Tree Transduction
  • Can obtain derivation or abstract syntax tree.
  • Tree can be generated top-down, or bottom-up.
  • We will show how to obtain
  • Derivation tree top-down
  • AST for the original grammar, bottom-up.

3
Top-Down Generation of Derivation Tree
  • In each procedure, and for each alternative,
    write out the appropriate production AS SOON AS
    IT IS KNOWN

4
Top-Down Generation of Derivation Tree (contd)
  • proc S S ? begin SL end
  • ? id E
  • case Next_Token of
  • T_begin Write(S ? begin SL end)
  • Read(T_begin)
  • SL
  • Read(T_end)

5
Top-Down Generation of Derivation Tree (contd)
  • T_id Write(S ? id E)
  • Read(T_id)
  • Read (T_)
  • E
  • Read (T_)
  • otherwise Error
  • end
  • end

6
Top-Down Generation of Derivation Tree (contd)
  • proc SL SL ? SZ
  • Write(SL ? SZ)
  • S
  • Z
  • end
  • proc E E ? TY
  • Write(E ? TY)
  • T
  • Y
  • end

7
Top-Down Generation of Derivation Tree (contd)
  • proc Z Z ? SZ
  • ?
  • case Next_Token of
  • T_begin, T_id Write(Z ? SZ)
  • S
  • Z
  • T_end Write(Z ? )
  • otherwise Error
  • end
  • end

8
Top-Down Generation of Derivation Tree (contd)
  • proc Y Y ? TY
  • ?
  • if Next_Token T_ then
  • Write (Y ? TY)
  • Read (T_)
  • T
  • Y
  • else Write (Y ? )
  • end

9
Top-Down Generation of Derivation Tree (contd)
  • proc T T ? PX
  • Write (T ? PX)
  • P
  • X
  • end
  • proc XX ? T
  • ?

10
Top-Down Generation of Derivation Tree (contd)
  • if Next_Token T_ then
  • Write (X ? T)
  • Read (T_)
  • T
  • else Write (X ? )
  • end

11
Top-Down Generation of Derivation Tree (contd)
  • proc PP ? (E)
  • ? id
  • case Next_Token of
  • T_( Write (P ? (E))
  • Read (T_()
  • E
  • Read (T_))
  • T_id Write (P ? id)
  • Read (T_id)
  • otherwise Error
  • end

12
Notes
  • The placement of the Write statements is obvious
    precisely because the grammar is LL(1).
  • Can build the tree as we go, or have it built
    by a post-processor.

13
Example
  • Input String
  • begin id (id id) id end
  • Output

S ? begin SL end SL ? SZ S ? id E E ? TY T ?
PX P ? (E) E ? TY T ? PX P ? id X ?
Y ? TY T ? PX P ? id X ? Y ? X ? T T ? PX P ?
id X ? Y ? Z ?
14
(No Transcript)
15
Bottom-up Generation of the Derivation Tree
  • We could have placed the write statements at the
    END of each phrase, instead of the beginning. If
    we do, the tree will be generated bottom-up.
  • In each procedure, and for each alternative,
    write out the production A ? ? AFTER ? is parsed.

16
Bottom-up Generation of the Derivation Tree
(contd)
  • proc SS ? begin SL end
  • ? id E
  • case Next_Token of
  • T_begin Read (T_begin)
  • SL
  • Read (T_end)
  • Write (S ? begin SL end)
  • T_id Read (T_id)
  • Read (T_)
  • E
  • Read (T_)
  • Write (S ? idE)
  • otherwise Error
  • end

17
Bottom-up Generation of the Derivation Tree
(contd)
  • proc SL SL ? SZ
  • S
  • Z
  • Write(SL ? SZ)
  • end
  • proc E E ? TY
  • T
  • Y
  • Write(E ? TY)
  • end

18
Bottom-up Generation of the Derivation Tree
(contd)
  • proc Z Z ? SZ
  • ?
  • case Next_Token of
  • T_begin, T_id S
  • Z
  • Write(Z ? SZ)
  • T_end Write(Z ? )
  • otherwise Error
  • end
  • end

19
Bottom-up Generation of the Derivation Tree
(contd)
  • proc Y Y ? TY
  • ?
  • if Next_Token T_ then
  • Read (T_)
  • T
  • Y
  • Write (Y ? TY)
  • else Write (Y ? )
  • end

20
Bottom-up Generation of the Derivation Tree
(contd)
  • proc T T ? PX
  • P
  • X
  • Write (T ? PX)
  • end
  • proc XX ? T
  • ?
  • if Next_Token T_ then
  • Read (T_)
  • T
  • Write (X ? T)
  • else Write (X ? )
  • end

21
Bottom-up Generation of the Derivation Tree
(contd)
  • proc PP ? (E)
  • ? id
  • case Next_Token of
  • T_( Read (T_()
  • E
  • Read (T_))
  • Write (P ? (E))
  • T_id Read (T_id)
  • Write (P ? id)
  • otherwise Error
  • end

22
Notes
  • The placement of the Write statements is still
    obvious.
  • The productions are emitted as procedures quit,
    not as they start.

23
Notes (contd)
  • Productions emitted in reverse order, i.e., the
    sequence of productions must be used in reverse
    order to obtain a right-most derivation.
  • Again, can built tree as we go (need stack of
    trees), or later.

24
Example
  • Input String
  • begin id (id id) id end
  • Output

P ? id X ? T ? PX P ? id X ? T ? PX Y ? Y ?
TY E ? TY P ? (E)
P ? id X ? T ? PX X ? T T ? PX Y ? E ? TY S ?
idE Z ? SL ? SZ S ? begin SL end
25
(No Transcript)
26
Tree Generation
Programming Language Principles Lecture 5
  • Prepared by
  • Manuel E. Bermúdez, Ph.D.
  • Associate Professor
  • University of Florida
Write a Comment
User Comments (0)
About PowerShow.com