Bottom-up derivation tree generation Module 08.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez
Tree generation Red: now Possibilities: Derivation tree, or Abstract Syntax Tree. Top-down, or bottom-up. For original, or modified grammar ! Leading up to: AST, bottom-up, for the original grammar (“the one”).
BOTTOM-UP derivation tree generation In each procedure, For each alternative, Write out the selected production rule A->ω IMMEDIATELY AFTER is parsed.
BOTTOM-UP derivation tree generation proc S; 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 end;
BOTTOM-UP derivation tree generation proc SL; S(); Z(); Write(SL → SZ); end; proc Z; case Next Token of T_begin, T_id: S();Z(); Write(Z → SZ); T_end: Write(Z → ); otherwise Error;
BOTTOM-UP derivation tree generation proc E; T(); Y(); Write(E → TY); end; proc Y; if Next Token = T_+ then Read(T_+); T(); Y(); Write (Y → +TY); else Write (Y → ) ;
BOTTOM-UP derivation tree generation proc T; P(); X(); Write (T → PX); end; proc X; if Next Token = T_* then Read(T_*); T(); Write (X → *T); else Write (X → );
BOTTOM-UP derivation tree generation proc P; case Next Token of T_(: Read(T_(); E(); Read(T_)); Write (P → (E)); T_id: Read(T_id); Write (P → id); otherwise Error; end;
Parser output P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → Input String: begin id := (id + id) * id; end Output: P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → T → PX X → *T Y → E → TY S → id:=E; Z → SL → SZ S → begin SL end
Bottom-up derivation tree generation Location of Write() statements: Still obvious: grammar is LL(1). Productions emitted as procedures quit, not as they start. Sequence of productions must be reversed to obtain a right- most derivation. First (of two) ways to build tree: Build tree after parsing concludes. Reverse the list of productions. Top-down, Right-most derivation. Still yucky, need to add lots of code (“current”?).
Top-down right-most derivation (post-parser) Input String: begin id := (id + id) * id; end Output: P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → T → PX X → *T Y → E → TY S → id:=E; Z → SL → SZ S → begin SL end
Bottom-up derivation tree generation Second (of two) ways to build tree: Build tree during parsing (using stack of trees). Read(T_t) means Push(Stack,Node(T_t)). “Write(A->ω)” means “reduce” ω to A. (opposite of “derive” ω from A): Pop n=|ω| trees from Stack, Create parent node A for them, Push(S,Node(A)). Sound familiar ?
Bottom-up, stack-based, DT construction Input String: begin id := (id + id) * id; end Output: P → id X → T → PX Y → Y → +TY E → TY P → (E) P → id X → T → PX X → *T Y → E → TY S → id:=E; Z → SL → SZ S → begin SL end
Bottom-up derivation tree generation Only need to modify Read() and Write(). proc Read(T_t); if T_t ≠ Next_Token then Error(); Push(S,Node(T_t)); Next_token = scan(); end; proc Write(A->ω); Node t(A); for i=1 to |ω| do add_child(Pop(S),t,1); end; Push(S,t); Neat.
Tree generation Red: Done Possibilities: Derivation tree, or Abstract Syntax Tree. Top-down, or bottom-up. For original, or modified grammar ! Leading up to: AST, bottom-up, for the original grammar (“the one”).