0% found this document useful (0 votes)
42 views

Tree Generation: Programming Language Principles

This document discusses string-to-tree transduction and generating derivation trees from grammars in both a top-down and bottom-up manner. It presents algorithms for obtaining derivation trees top-down by writing productions as soon as they are known, and bottom-up by writing productions after parsing is complete. Examples are provided to illustrate the process.

Uploaded by

Mrunal Ruikar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Tree Generation: Programming Language Principles

This document discusses string-to-tree transduction and generating derivation trees from grammars in both a top-down and bottom-up manner. It presents algorithms for obtaining derivation trees top-down by writing productions as soon as they are known, and bottom-up by writing productions after parsing is complete. Examples are provided to illustrate the process.

Uploaded by

Mrunal Ruikar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Tree Generation

Programming Language Principles Lecture 5

Prepared by

Manuel E. Bermdez, Ph.D.


Associate Professor University of Florida

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 1. Derivation tree top-down 2. AST for the original grammar, bottom-up.

Top-Down Generation of Derivation Tree


In each procedure, and for each alternative, write out the appropriate production AS SOON AS IT IS KNOWN

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);

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;

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;

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;

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;

Top-Down Generation of Derivation Tree (contd)


proc T; {T PX}
Write (T PX); P; X end; proc X;{X *T

Top-Down Generation of Derivation Tree (contd)


if Next_Token = T_* then
Write (X *T); Read (T_*); T; else Write (X ); end;

Top-Down Generation of Derivation Tree (contd)


proc P;{P (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;

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.

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 T P X Y X T P X Y Z +TY PX id

*T PX id

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.

Bottom-up Generation of the Derivation Tree (contd)


proc S;{S 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 id:=E;); otherwise Error; end;

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;

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;

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;

Bottom-up Generation of the Derivation Tree (contd)


proc T; {T PX } P; X; Write (T PX) end; proc X;{X *T

}
if Next_Token = T_* then Read (T_*); T; Write (X *T); else Write (X ); end

Bottom-up Generation of the Derivation Tree (contd)


proc P;{P (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;

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

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.

Example
Input String: begin id := (id + id) * id; end Output: P X T P X T Y Y E P id P id X T PX X *T T PX Y E TY S id:=E; Z SL SZ S begin SL end

PX id
PX +TY TY (E)

Tree Generation
Programming Language Principles Lecture 5

Prepared by

Manuel E. Bermdez, Ph.D.


Associate Professor University of Florida

You might also like