Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bottom-up derivation tree generation

Similar presentations


Presentation on theme: "Bottom-up derivation tree generation"— Presentation transcript:

1 Bottom-up derivation tree generation
Module COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

2 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”).

3 BOTTOM-UP derivation tree generation
In each procedure, For each alternative, Write out the selected production rule A->ω IMMEDIATELY AFTER  is parsed.

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

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

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

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

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

9 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

10 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”?).

11 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

12 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 ?

13 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

14 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.

15 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”).


Download ppt "Bottom-up derivation tree generation"

Similar presentations


Ads by Google