Tree Generation: Programming Language Principles
Tree Generation: Programming Language Principles
Prepared by
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.
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
}
if Next_Token = T_* then Read (T_*); T; Write (X *T); else Write (X ); 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