Statement 27 February 2019 OSU CSE 1 BL Compiler Structure Code - - PowerPoint PPT Presentation

statement
SMART_READER_LITE
LIVE PREVIEW

Statement 27 February 2019 OSU CSE 1 BL Compiler Structure Code - - PowerPoint PPT Presentation

Statement 27 February 2019 OSU CSE 1 BL Compiler Structure Code Tokenizer Parser Generator string of string of abstract string of characters tokens program integers (source code) (words) (object code) A BL program consists


slide-1
SLIDE 1

Statement

27 February 2019 OSU CSE 1

slide-2
SLIDE 2

BL Compiler Structure

27 February 2019 OSU CSE 2

Code Generator Parser Tokenizer string of characters (source code) string of tokens (“words”) abstract program string of integers (object code)

A BL program consists of some statements …

slide-3
SLIDE 3

Statement

  • The Statement component family allows

you to manipulate values that are ASTs for BL statements

  • The mathematical model of a Statement

value is a tree of STATEMENT_LABEL with some constraints

27 February 2019 OSU CSE 3

slide-4
SLIDE 4

27 February 2019 OSU CSE 4

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

slide-5
SLIDE 5

27 February 2019 OSU CSE 5

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

A Statement variable’s value is a tree of STATEMENT_LABEL with some constraints, so we use rather than to illustrate its recursive structure.

slide-6
SLIDE 6

27 February 2019 OSU CSE 6

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

The kind of statement (based on the root) determines how many and which kinds of children it may have.

slide-7
SLIDE 7

27 February 2019 OSU CSE 7

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

The children of a BLOCK statement may not be BLOCK statements.

slide-8
SLIDE 8

27 February 2019 OSU CSE 8

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

The child of an IF or WHILE statement must be a BLOCK statement.

slide-9
SLIDE 9

27 February 2019 OSU CSE 9

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

The two children of an IF_ELSE statement must be BLOCK statements.

slide-10
SLIDE 10

27 February 2019 OSU CSE 10

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

A CALL statement has no children.

slide-11
SLIDE 11

Interfaces and Classes

Statement- Kernel extends Standard extends

27 February 2019 OSU CSE 11

Statement implements Statement1

slide-12
SLIDE 12

Interfaces and Classes

Statement- Kernel extends Standard extends

27 February 2019 OSU CSE 12

Statement implements Statement1 StatementKernel has contracts for 12 methods that involve “assembling” and “disassembling” the various BL statements (similar to Tree methods).

slide-13
SLIDE 13

Interfaces and Classes

Statement- Kernel extends Standard extends

27 February 2019 OSU CSE 13

Statement implements Statement1 Statement has these additional methods (not discussed here): parse parseBlock prettyPrint

slide-14
SLIDE 14

Enumerations

  • Java has a special construct, enum (short

for “enumeration”), that easily allows you to use meaningful symbolic names where you might otherwise be inclined to declare int variables and give them arbitrary values to participate just in equality tests

27 February 2019 OSU CSE 14

slide-15
SLIDE 15

Example: The Kind Enum

  • The interface StatementKernel

contains this code:

/** * The kinds of statements. */ enum Kind { BLOCK, IF, IF_ELSE, WHILE, CALL }

27 February 2019 OSU CSE 15

slide-16
SLIDE 16

Example: The Kind Enum

  • The interface StatementKernel

contains this code:

/** * The kinds of statements. */ enum Kind { BLOCK, IF, IF_ELSE, WHILE, CALL }

27 February 2019 OSU CSE 16

This is quite different from: final int BLOCK = 1; final int IF = 2; ... You may not do arithmetic with the enum constants, where you could with the int constants above.

slide-17
SLIDE 17

Example: The Condition Enum

/** * The possible conditions for IF, IF_ELSE, * and WHILE statements. */ enum Condition { NEXT_IS_EMPTY, NEXT_IS_NOT_EMPTY, NEXT_IS_WALL, NEXT_IS_NOT_WALL, NEXT_IS_FRIEND, NEXT_IS_NOT_FRIEND, NEXT_IS_ENEMY, NEXT_IS_NOT_ENEMY, RANDOM, TRUE }

27 February 2019 OSU CSE 17

slide-18
SLIDE 18

Mathematical Model (1)

IDENTIFIER is string of character exemplar id constraint [id starts with a letter 'a'-'z', 'A'-'Z'] and [id contains only letters, digits '0'-'9', and '-'] and [id is not one of the keywords or conditions in the BL language]

27 February 2019 OSU CSE 18

slide-19
SLIDE 19

Mathematical Model (2)

STATEMENT_LABEL is ( kind: Kind, test: Condition, call: IDENTIFIER) exemplar sl constraint [if sl.kind = BLOCK then sl.test and sl.call are irrelevant] and [if sl.kind = IF or sl.kind = IF_ELSE or sl.kind = WHILE then sl.call is irrelevant] and [if sl.kind = CALL then sl.test is irrelevant]

27 February 2019 OSU CSE 19

slide-20
SLIDE 20

Mathematical Model (2)

STATEMENT_LABEL is ( kind: Kind, test: Condition, call: IDENTIFIER) exemplar sl constraint [if sl.kind = BLOCK then sl.test and sl.call are irrelevant] and [if sl.kind = IF or sl.kind = IF_ELSE or sl.kind = WHILE then sl.call is irrelevant] and [if sl.kind = CALL then sl.test is irrelevant]

27 February 2019 OSU CSE 20

Notice the use of the enums in this model, as well as the previously defined IDENTIFIER.

slide-21
SLIDE 21

Mathematical Model (3)

STATEMENT_MODEL is tree of STATEMENT_LABEL exemplar s constraint |s| > 0 and [BLOCK can have 0 or more children, but not another BLOCK as a child] and [IF must have exactly one BLOCK child] and [IF_ELSE must have exactly two BLOCK children] and [WHILE must have exactly one BLOCK child] and [CALL must have no children (must be a leaf)]

27 February 2019 OSU CSE 21

slide-22
SLIDE 22

Mathematical Model (4)

type StatementKernel is modeled by STATEMENT_MODEL

27 February 2019 OSU CSE 22

slide-23
SLIDE 23

No-argument Constructor

  • Ensures:

this = compose((BLOCK, ?, ?), <>)

27 February 2019 OSU CSE 23

slide-24
SLIDE 24

No-argument Constructor

  • Ensures:

this = compose((BLOCK, ?, ?), <>)

27 February 2019 OSU CSE 24

The use of ? here means we do not know—and, frankly, do not care about—the values of the 2nd and 3rd tuple components (test and call); the model says they are irrelevant if the 1st tuple component (kind) is BLOCK.

slide-25
SLIDE 25

Example

27 February 2019 OSU CSE 25

Code State

Statement s = new Statement1();

slide-26
SLIDE 26

Example

27 February 2019 OSU CSE 26

Code State

Statement s = new Statement1();

s =

BLOCK

slide-27
SLIDE 27

kind

Statement.Kind kind()

  • Reports the kind of statement this is.
  • Ensures:

kind = [the statement kind of this]

27 February 2019 OSU CSE 27

slide-28
SLIDE 28

Example

27 February 2019 OSU CSE 28

Code State

s =

Kind k = s.kind();

BLOCK

slide-29
SLIDE 29

Example

27 February 2019 OSU CSE 29

Code State

s =

Kind k = s.kind();

s = k = BLOCK

BLOCK BLOCK

slide-30
SLIDE 30

addToBlock

void addToBlock(int pos, Statement s)

  • Adds the statement s at position pos in this BLOCK

statement.

  • Updates: this
  • Clears: s
  • Requires:

[this is a BLOCK statement] and [s is not a BLOCK statement] and 0 <= pos <= [length of this BLOCK]

  • Ensures:

this = [#this with child #s inserted at position pos]

27 February 2019 OSU CSE 30

slide-31
SLIDE 31

Example

27 February 2019 OSU CSE 31

Code State

s = ns =

s.addToBlock(1, ns);

BLOCK

slide-32
SLIDE 32

Example

27 February 2019 OSU CSE 32

Code State

s = ns =

s.addToBlock(1, ns);

s = ns =

BLOCK BLOCK BLOCK

slide-33
SLIDE 33

removeFromBlock

Statement removeFromBlock(int pos)

  • Removes and returns the statement at position

pos of this BLOCK statement.

  • Updates: this
  • Requires:

[this is a BLOCK statement] and 0 <= pos < [length of this BLOCK]

  • Ensures:

this = [#this with child at position pos removed and returned as result]

27 February 2019 OSU CSE 33

slide-34
SLIDE 34

Example

27 February 2019 OSU CSE 34

Code State

s = Statement ns = s.removeFromBlock(1);

BLOCK

slide-35
SLIDE 35

Example

27 February 2019 OSU CSE 35

Code State

s = Statement ns = s.removeFromBlock(1); s = ns =

BLOCK BLOCK

slide-36
SLIDE 36

lengthOfBlock

int lengthOfBlock()

  • Reports the number of statements in this

BLOCK statement.

  • Requires:

[this is a BLOCK statement]

  • Ensures:

lengthOfBlock = [the number of children of this]

27 February 2019 OSU CSE 36

slide-37
SLIDE 37

assembleIf

void assembleIf(Statement.Condition c, Statement s)

  • Assembles in this a statement with root label (IF, c , ?)

and only subtree the BLOCK s; the declaration notwithstanding, the dynamic type of s must be the same as the dynamic type of this.

  • Replaces: this
  • Clears: s
  • Requires:

[s is a BLOCK statement]

  • Ensures:

this = compose((IF, c, ?), <#s>)

27 February 2019 OSU CSE 37

slide-38
SLIDE 38

Example

27 February 2019 OSU CSE 38

Code State

s = ? ns =

s.assembleIf( RANDOM, ns);

slide-39
SLIDE 39

Example

27 February 2019 OSU CSE 39

Code State

s = ? ns =

s.assembleIf( RANDOM, ns);

s = ns =

IF RANDOM

BLOCK

slide-40
SLIDE 40

disassembleIf

Statement.Condition disassembleIf( Statement s)

  • Disassembles IF statement this into its test Condition, which is

returned as the value of the function, and its only subtree, the BLOCK statement s; the declaration notwithstanding, the dynamic type of s must be the same as the dynamic type of this.

  • Replaces: s
  • Clears: this
  • Requires:

[this is an IF statement]

  • Ensures:

#this = compose((IF, disassembleIf, ?), <s>)

27 February 2019 OSU CSE 40

slide-41
SLIDE 41

Example

27 February 2019 OSU CSE 41

Code State

s = ns = ? Condition c = s.disassembleIf(ns);

IF TRUE

slide-42
SLIDE 42

Example

27 February 2019 OSU CSE 42

Code State

s = ns = ? Condition c = s.disassembleIf(ns); ns = s = c = TRUE

IF TRUE

BLOCK

slide-43
SLIDE 43

Other Methods

  • See the Javadoc for Statement for

details of the other methods:

– assembleIfElse – disassembleIfElse – assembleWhile – disassembleWhile – assembleCall – disassembleCall

27 February 2019 OSU CSE 43

slide-44
SLIDE 44

27 February 2019 OSU CSE 44

BLOCK IF condition WHILE condition CALL instruction

s =

IF_ELSE condition

slide-45
SLIDE 45

“Processing” a Statement

if (s.kind() == BLOCK) { ... } else if (s.kind() == IF) { ... } else if (s.kind() == IF_ELSE) { ... } else if (s.kind() == WHILE) { ... } else if (s.kind() == CALL) { ... }

27 February 2019 OSU CSE 45

slide-46
SLIDE 46

“Processing” a Statement

if (s.kind() == BLOCK) { ... } else if (s.kind() == IF) { ... } else if (s.kind() == IF_ELSE) { ... } else if (s.kind() == WHILE) { ... } else if (s.kind() == CALL) { ... }

27 February 2019 OSU CSE 46

Technically, there is no reason you need to test this last condition; because what else could it be?

slide-47
SLIDE 47

Java’s switch Construct

switch (s.kind()) { case BLOCK: { ... ; break;} case IF: { ... ; break;} case IF_ELSE: { ... ; break;} case WHILE: { ... ; break;} case CALL: { ... ; break;} default: { ... ; break;} }

27 February 2019 OSU CSE 47

slide-48
SLIDE 48

Java’s switch Construct

switch (s.kind()) { case BLOCK: { ... ; break;} case IF: { ... ; break;} case IF_ELSE: { ... ; break;} case WHILE: { ... ; break;} case CALL: { ... ; break;} default: { ... ;} }

27 February 2019 OSU CSE 48

The switch is recommended

  • ver a long string of

if-else-if-else-if-...

slide-49
SLIDE 49

Java’s switch Construct

switch (s.kind()) { case BLOCK: { ... ; break;} case IF: { ... ; break;} case IF_ELSE: { ... ; break;} case WHILE: { ... ; break;} case CALL: { ... ; break;} default: { ... ;} }

27 February 2019 OSU CSE 49

The default case is recommended even when technically it is not needed (as here).

slide-50
SLIDE 50

Resources

  • OSU CSE Components API: Statement

– http://cse.osu.edu/software/common/doc/

  • Big Java, Section 5.3

– http://osu.worldcat.org/title/big-java/oclc/754642794

27 February 2019 OSU CSE 50