tree generation
DESCRIPTION
Tree Generation. Programming Language Principles Lecture 5. Prepared by Manuel E. Bermúdez, 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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/1.jpg)
Tree Generation
Prepared byManuel E. Bermúdez, Ph.D.
Associate ProfessorUniversity of Florida
Programming Language PrinciplesLecture 5
![Page 2: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/2.jpg)
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 obtain1. Derivation tree top-down2. AST for the original grammar,
bottom-up.
![Page 3: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/3.jpg)
Top-Down Generation of Derivation Tree• In each procedure, and for each
alternative, write out the appropriate production AS SOON AS IT IS KNOWN
![Page 4: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/4.jpg)
Top-Down Generation of Derivation Tree (cont’d)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);
![Page 5: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/5.jpg)
Top-Down Generation of Derivation Tree (cont’d)
T_id : Write(S → id :=E;);
Read(T_id);Read (T_:=);E;Read (T_;);
otherwise Errorend
end;
![Page 6: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/6.jpg)
Top-Down Generation of Derivation Tree (cont’d)proc SL; {SL → SZ}
Write(SL → SZ);S;Z;
end;
proc E; {E → TY}Write(E → TY);T;Y;
end;
![Page 7: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/7.jpg)
Top-Down Generation of Derivation Tree (cont’d)proc Z; {Z → SZ
→ }case Next_Token of
T_begin, T_id: Write(Z → SZ); S; Z;
T_end: Write(Z → );otherwise Error;
endend;
![Page 8: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/8.jpg)
Top-Down Generation of Derivation Tree (cont’d)proc Y; {Y → +TY
→ }if Next_Token = T_+ then
Write (Y → +TY);Read (T_+);T;Y;
else Write (Y → );end;
![Page 9: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/9.jpg)
Top-Down Generation of Derivation Tree (cont’d)proc T; {T → PX}
Write (T → PX);P;X
end;
proc X;{X → *T → }
![Page 10: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/10.jpg)
Top-Down Generation of Derivation Tree (cont’d)
if Next_Token = T_* thenWrite (X → *T);Read (T_*);T;
else Write (X → );end;
![Page 11: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/11.jpg)
Top-Down Generation of Derivation Tree (cont’d)
proc P;{P → (E)→ id }
case Next_Token ofT_(: Write (P → (E));
Read (T_(); E; Read (T_)); T_id: Write (P → id); Read (T_id);otherwise Error;
end;
![Page 12: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/12.jpg)
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.
![Page 13: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/13.jpg)
Example• Input String:
begin id := (id + id) * id; end• Output:
S → begin SL endSL → SZS → id :=E;E → TYT → PXP → (E)E → TYT → PXP → idX →
Y → +TYT → PXP → idX → Y → X → *TT → PXP → idX → Y → Z →
![Page 14: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/14.jpg)
![Page 15: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/15.jpg)
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.
![Page 16: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/16.jpg)
Bottom-up Generation of the Derivation Tree (cont’d)proc S;{S → begin SL end
→ id := E; }case Next_Token ofT_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;
![Page 17: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/17.jpg)
Bottom-up Generation of the Derivation Tree (cont’d)proc SL; {SL → SZ}
S;Z;Write(SL → SZ);
end;
proc E; {E → TY}T;Y;Write(E → TY);
end;
![Page 18: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/18.jpg)
Bottom-up Generation of the Derivation Tree (cont’d)proc Z; {Z → SZ
→ }case Next_Token of
T_begin, T_id: S; Z; Write(Z → SZ);
T_end: Write(Z → );otherwise Error;
endend;
![Page 19: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/19.jpg)
Bottom-up Generation of the Derivation Tree (cont’d)
proc Y; {Y → +TY → }
if Next_Token = T_+ thenRead (T_+);T;Y;Write (Y → +TY);
else Write (Y → );end;
![Page 20: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/20.jpg)
Bottom-up Generation of the Derivation Tree (cont’d)proc T; {T → PX }
P;X;Write (T → PX)
end;
proc X;{X → *T → }
if Next_Token = T_* thenRead (T_*);T;Write (X → *T);
else Write (X → );end
![Page 21: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/21.jpg)
Bottom-up Generation of the Derivation Tree (cont’d)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;
![Page 22: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/22.jpg)
Notes
• The placement of the Write statements is still obvious.
• The productions are emitted as procedures quit, not as they start.
![Page 23: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/23.jpg)
Notes (cont’d)
• 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.
![Page 24: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/24.jpg)
Example• Input String: begin id := (id + id) * id; end• Output:
P → idX →T → PXP → idX →T → PXY → Y → +TYE → TYP → (E)
P → idX → T → PXX → *TT → PXY → E → TYS → id:=E; Z → SL → SZS → begin SL end
![Page 25: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/25.jpg)
![Page 26: Tree Generation](https://reader036.vdocuments.mx/reader036/viewer/2022062305/56815a86550346895dc7f545/html5/thumbnails/26.jpg)
Tree Generation
Prepared byManuel E. Bermúdez, Ph.D.
Associate ProfessorUniversity of Florida
Programming Language PrinciplesLecture 5