decision structures - the syntax tree - lecture 22...

42
Decision Structures - The Syntax Tree Robb T. Koether if Statements The Back- patchNode Class Backpatching Backpatch Functions Jumps in the Grammar Assignment Decision Structures - The Syntax Tree Lecture 22 Sections 8.4, 8.6 Robb T. Koether Hampden-Sydney College Fri, Apr 10, 2009

Upload: others

Post on 26-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Decision Structures - The Syntax TreeLecture 22

Sections 8.4, 8.6

Robb T. Koether

Hampden-Sydney College

Fri, Apr 10, 2009

Page 2: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Outline

1 if Statements

2 The BackpatchNode Class

3 Backpatching

4 Backpatch Functions

5 Jumps in the Grammar

6 Assignment

Page 3: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

if Statements

We will consider two forms of the if statement.

stmt → if ( cexpr ) stmt ;

stmt → if ( cexpr ) stmt else stmt ;

where cexpr is a conditional expression.Recall that we have the productions

stmt → { stmts }

cexpr → expr

Page 4: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

if Statements

For the time being, we will assume that cexpr is anumerical expression.Zero is interpreted as false.Any nonzero value is interpreted as true.Thus, we do not need to be concerned with relationaloperators (==, !=, <, >, <=, >=) or boolean operators(&&, ||, !) yet.

Page 5: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Flow of Control

Consider the flow of control for the production

stmt → if ( cexpr ) stmt1 ;

If cexpr is non-zero, then execution must jump to stmt1.If cexpr is zero, then execution must jump to whateverfollows stmt1.Problem: We do not yet know where that it.

Page 6: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Flow of Control

We need an object associated with cexpr that willcontain two destinations.

The “true” destination.The “false” destination.

Such an object is called a backpatch node.

Page 7: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

The BackpatchNode Class

The BackpatchNode Classpublic class BackpatchNode{

LinkedList trueList;LinkedList falseList;

}

Page 8: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Backpatch Nodes

Each backpatch node containsA “true” reference to a linked list of labels.A “false” reference to a linked list of labels.

The nonterminal cexpr will represent a backpatch node.From cexpr we set up a “true” destination label and a“false” destination label.

Page 9: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Backpatching

The “true” and “false” destination labels are labels to beresolved once we know where the destinations are.An unresolved label is called a backpatch label.When we figure out the destination, we will “backpatch”the backpatch label to an actual label.

Page 10: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Backpatching

An Equate StatementB6=L8

An assembly language equate statement willaccomplish this.

Page 11: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Backpatching

The reason we keep a list of such labels is that theremay be several branches in the program that all havethe same destination.

Page 12: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

Page 13: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

Branchon true

Page 14: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

Branchon false

Page 15: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

Unconditionalbranch

Page 16: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

Unconditionalbranch

Page 17: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

Notice that the unconditional branch from the true partand the unconditional branch from the false part havethe same destination.Thus, we build a list of two labels that will be resolvedto the same destination.

Page 18: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

Page 19: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

TJump on true

to Label B1

Page 20: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

T FJump on true

to Label B1Jump on falseto Label B2

Page 21: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

T FJump on true

to Label B1Jump on falseto Label B2

Label L3 L3:

Page 22: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

T FJump on true

to Label B1Jump on falseto Label B2

Label L3 L3:

∴ B1 = L3

Page 23: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

FJump on falseto Label B2

L3:

Page 24: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

FJump on falseto Label B2

L3: Jump toLabel B4

Page 25: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

FJump on falseto Label B2

L3: Jump toLabel B4

L5:Label L5

Page 26: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

FJump on falseto Label B2

L3: Jump toLabel B4

L5:Label L5

∴ B2 = L5

Page 27: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3: Jump toLabel B4

L5:

Page 28: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3: Jump toLabel B4

L5: Jump toLabel B6

Page 29: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3: Jump toLabel B4

L5: Jump toLabel B6

∴ Merge B4 and B6

Page 30: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3:Jump toLabel {B4, B6}L5:

Page 31: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3:Jump toLabel {B4, B6}L5:

L7:Label L7

Page 32: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3:Jump toLabel {B4, B6}L5:

L7:Label L7

∴ B4 = B6 = L7

Page 33: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

L3:

L5:L7:

Page 34: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

if (a) b = 5;else b = 10;c = 2;

T F

L7:L5:

L3:

Page 35: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Example

Within the if statement, there are two “known”destinations and one unknown destination.We can see that labels B4 and B6 will be resolved tothe same destination eventually, but within the ifstatement that destination is unknown.Therefore, in the meantime, we merge them together ina list of backpatch nodes to be resolved later to thesame destination.Once we learn the destination, all labels in the list areresolved to that location.

Page 36: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Backpatch-Related Functions

Backpatching FunctionsmakeList(label);

Creates a LinkedList containing the single Integerlabel. Returns a reference to the list.

merge(list1, list2);Merges the elements of list1 and list2. Returns areference to the merged list.

backpatch(list, label);Equates label as the target label for each backpatchlabel in list.

Page 37: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Labels and Jumps in the Grammar

We saw in the example that actual labels are neededwithin the if statement to serve as destinations.We will incorporate the generation of these labels intothe grammar.The nonterminal m will create a label node which willserve as a known destination.The nonterminal n will create a jump to an as-yetunknown destination.

Page 38: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Labels and Jumps in the Grammar

m represents a destination.n represents an unconditional branch.The productions that involve these nonterminals are

stmts → stmts m stmt

stmt → if ( cexpr ) m stmt

stmt → if ( cexpr ) m stmt n else m stmt

func → fbeg stmts m }

Page 39: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Label Trees

LABEL6 LABEL label=6 L6:

An AssemblyCode Label

A Label Tree A Printed Label Tree

Page 40: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Jump Trees

JUMP JUMP INT BLABEL blabel=6 jmp B6

An AssemblyCode Jump

A Jump Tree

A Printed Jump Tree

BLABEL6

Page 41: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Equate Trees

JUMP EQU BLABEL blabel=6 LABEL label=8

B6=L8

An AssemblyCode Equate

An Equate Tree

A Printed Equate TreeBLABEL6

LABEL8

Page 42: Decision Structures - The Syntax Tree - Lecture 22 ...people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · Decision Structures - The Syntax Tree Robb T. Koether if Statements

DecisionStructures -The Syntax

Tree

Robb T.Koether

ifStatements

The Back-patchNodeClass

Backpatching

BackpatchFunctions

Jumps in theGrammar

Assignment

Assignment

HomeworkRead Section 8.4, pages 491 - 493.Read Section 8.6, pages 504 - 506.