ast generation prepared by manuel e. bermúdez, ph.d. associate professor university of florida...

21
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Upload: aaron-bannister

Post on 14-Dec-2015

219 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

AST Generation

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 9

Page 2: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Replacing Recursion with Iteration

• Not all the nonterminals are needed.

• The recursion in SL, X, Y and Z can be replaced with iteration.

Page 3: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Replacing Recursion with Iteration (cont’d)

proc S; {S → begin SL end→ id := E;

case Next_Token ofT_begin : Read(T_begin);

repeat S;until Next_Token {T_begin,T_id};Read(T_end);

T_id : Read(T_id);Read (T_:=);E;Read (T_;);

otherwise Error;end

end;Replaces recursion on Z.

Replaces call

to SL.

SLSL → S Z Z → S Z → }

Page 4: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Replacing Recursion with Iteration (cont’d)

proc E; {E → TYY → +TY → }

T;while Next_Token = T_+ do Read (T_+); T;

odend;

Replaces recursion on Y.

Page 5: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Replacing Recursion with Iteration (cont’d)

proc T; {T → PXX → *T → }

P;if Next_Token = T_*

then Read (T_*);T;

end;

Replaces call to X.

Page 6: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Replacing Recursion with Iteration (cont’d)

proc P;{P → (E)→ id }

case Next_Token ofT_(: Read (T_();

E; Read (T_));

T_id: Read (T_id);otherwise Error;end

end;

Page 7: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Construction of Derivation Tree for the Original Grammar (Bottom Up)

proc S; { (1)S → begin SL end (2)S → begin SL end → id := E; → id := E;

SL → SZ SL → SL S Z → SZ → S

→ }case Next_Token of

T_begin : Read(T_begin); S; Write (SL → S); while Next_Token in {T_begin,T_id} do

S;Write (SL → SL S);

od Read(T_end); Write (S → begin SL

end);

Page 8: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d)

T_id : Read(T_id);Read (T_:=);E;Read (T_;);Write (S → id :=E ;);

otherwise Error;end

end;

Page 9: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d)

proc E; {(1)E → TY (2) E → E+T Y → +TY → T → }

T;Write (E → T);while Next_Token = T_+ do

Read (T_+);T;Write (E → E+T);

odend

Page 10: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d)proc T; {(1)T → PX (2) T → P*T

X → *T → P →

}P; if Next_Token = T_*

then Read (T_*);T;Write (T → P*T)

else Write (T → P);end;

Page 11: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Construction of Derivation Tree for the Original Grammar (Bottom Up) (cont’d)

proc P;{(1)P → (E) (2)P → (E) → id → id

}

// SAME AS BEFOREend;

Page 12: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Example

• Input String: begin id := (id + id) * id; end• Output:

P → idT → PE → TP → idT → PE → E+TP → (E)P → idT → P

T → P*TE → TS → id:=E;SL→ SS → begin SL end

Page 13: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9
Page 14: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar proc S; { S → begin S+ end 'block'

→ id := E; 'assign'var N:integer;

case Next_Token ofT_begin : Read(T_begin);

S;N:=1;while Next_Token in {T_begin,T_id} do

S;N:=N+1;

odRead(T_end);Build Tree ('block',N);

T_id : Read(T_id);Read (T_:=);E;Read (T_;);Build Tree ('assign',2);

otherwise Errorend

end;

Assume this builds a node.

Build Tree (‘x’,n) pops n trees from the stack, builds an ‘x’ node as their parent, and pushes the resulting tree.

Page 15: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar (cont’d)proc E; {E → E+T '+'

→ T }T;while Next_Token = T_+ do

Read (T_+)T;Build Tree ('+',2);

odend;

Left branching in tree!

Page 16: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar (cont’d)

proc T; {T → P*T '*' → P }

P;if Next_Token = T_*

then Read (T_*)T;Build Tree ('*',2);

end;

Right branching in tree!

Page 17: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Generating the Abstract Syntax Tree, Bottom Up, for the Original Grammar (cont’d)

proc P;{P → (E)→ id }

// SAME AS BEFORE, // i.e.,no trees builtend;

Page 18: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Example

• Input String: begin id1 := (id2 + id3) * id4; end

• Sequence of events:

id1

id2

id3

id4

BT('+',2)

BT('*',2)

BT('assign',2)

BT('block',1)

Page 19: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9
Page 20: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

Summary

• Bottom-up or top-down tree construction.

• Original or modified grammar.• Derivation Tree or Abstract Syntax

Tree.

• Technique of choice (Hint: project)• Top-down, recursive descent parser.• Bottom-up tree construction for the

original grammar.

Page 21: AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9

AST Generation

Prepared by

Manuel E. Bermúdez, Ph.D.Associate ProfessorUniversity of Florida

Programming Language ConceptsLecture 9