Program 9 January 2019 OSU CSE 1 BL Compiler Structure Code - - PowerPoint PPT Presentation

program
SMART_READER_LITE
LIVE PREVIEW

Program 9 January 2019 OSU CSE 1 BL Compiler Structure Code - - PowerPoint PPT Presentation

Program 9 January 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 of


slide-1
SLIDE 1

Program

9 January 2019 OSU CSE 1

slide-2
SLIDE 2

BL Compiler Structure

9 January 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, and more …

slide-3
SLIDE 3

Program

  • The Program component family allows

you to manipulate values that are models

  • f complete BL programs
  • The mathematical model of a Program

includes that of a Statement (specifically, a BLOCK) for its body, plus more:

– the program name – the new user-defined instructions, each of which also has a body

9 January 2019 OSU CSE 3

slide-4
SLIDE 4

Structure of a BL Program

9 January 2019 OSU CSE 4

PROGRAM MyProg IS BEGIN END MyProg The program’s name (must be an IDENTIFIER). The context: a set of new instructions (names and bodies). The body Statement (the “main” program; kind must be BLOCK).

slide-5
SLIDE 5

Structure of a New Instruction

9 January 2019 OSU CSE 5

INSTRUCTION Instr IS END Instr The instruction’s name (must be an IDENTIFIER). The body Statement (kind must be BLOCK).

slide-6
SLIDE 6

9 January 2019 OSU CSE 6

PROGRAM MyProg IS BEGIN END MyProg INSTRUCTION Instr1 IS END Instr1

BLOCK

BLOCK

The program’s context comprises zero or more new instructions.

INSTRUCTION Instr2 IS END Instr2

BLOCK

slide-7
SLIDE 7

Example: “Draw” the Value

PROGRAM SteerClear IS INSTRUCTION Avoid IS IF random THEN turnright ELSE turnleft END IF END Avoid INSTRUCTION Flee IS turnright turnright END Flee BEGIN WHILE true DO IF next-is-empty THEN move ELSE IF next-is-enemy THEN Flee ELSE Avoid END IF END IF END WHILE END SteerClear

9 January 2019 OSU CSE 7

slide-8
SLIDE 8

Example: “Draw” the Value

PROGRAM SteerClear IS INSTRUCTION Avoid IS IF random THEN turnright ELSE turnleft END IF END Avoid INSTRUCTION Flee IS turnright turnright END Flee BEGIN WHILE true DO IF next-is-empty THEN move ELSE IF next-is-enemy THEN Flee ELSE Avoid END IF END IF END WHILE END SteerClear

9 January 2019 OSU CSE 8

slide-9
SLIDE 9

Example

p = ("SteerClear", {("Avoid", ), ("Flee", )}, )

9 January 2019 OSU CSE 9

BLOCK BLOCK BLOCK

slide-10
SLIDE 10

Example

p = ("SteerClear", {("Avoid", ), ("Flee", )}, )

9 January 2019 OSU CSE 10

BLOCK BLOCK BLOCK

IF random THEN turnright ELSE turnleft END IF

slide-11
SLIDE 11

Example

p = ("SteerClear", {("Avoid", ), ("Flee", )}, )

9 January 2019 OSU CSE 11

BLOCK BLOCK BLOCK

turnright turnright

slide-12
SLIDE 12

Example

p = ("SteerClear", {("Avoid", ), ("Flee", )}, )

9 January 2019 OSU CSE 12

BLOCK BLOCK BLOCK

WHILE true DO IF next-is-empty THEN move ELSE IF next-is-wall THEN Flee ELSE Avoid END IF END IF END WHILE

slide-13
SLIDE 13

Interfaces and Classes

Program- Kernel extends Standard extends

9 January 2019 OSU CSE 13

Program implements Program1

slide-14
SLIDE 14

Interfaces and Classes

Program- Kernel extends Standard extends

9 January 2019 OSU CSE 14

Program implements Program1 ProgramKernel has contracts for these methods: name setName newContext swapContext newBody swapBody

slide-15
SLIDE 15

Interfaces and Classes

Program- Kernel extends Standard extends

9 January 2019 OSU CSE 15

Program implements Program1 Program has these additional methods (not all discussed here): parse prettyPrint generatedCode

slide-16
SLIDE 16

Mathematical Model

CONTEXT is finite set of (name: IDENTIFIER, body: STATEMENT_MODEL) exemplar c constraint [the names of instructions in c are unique] and [the names of instructions in c do not match the names of primitive instructions in the BL language] and [the bodies of instructions in c are all BLOCK statements]

9 January 2019 OSU CSE 16

slide-17
SLIDE 17

Mathematical Model

PROGRAM_MODEL is ( name: IDENTIFIER, context: CONTEXT, body: STATEMENT_MODEL ) exemplar p constraint [p.body is a BLOCK statement]

type ProgramKernel is modeled by PROGRAM_MODEL

9 January 2019 OSU CSE 17

slide-18
SLIDE 18

No-argument Constructor

  • Ensures:

this = ("Unnamed", {}, compose((BLOCK, ?, ?), <>))

9 January 2019 OSU CSE 18

slide-19
SLIDE 19

No-argument Constructor

  • Ensures:

this = ("Unnamed", {}, compose((BLOCK, ?, ?), <>))

9 January 2019 OSU CSE 19

The corresponding BL program is: PROGRAM Unnamed IS BEGIN END Unnamed

slide-20
SLIDE 20

Example

9 January 2019 OSU CSE 20

Code State

Program p = new Program1();

slide-21
SLIDE 21

Example

9 January 2019 OSU CSE 21

Code State

Program p = new Program1();

p = ("Unnamed", {}, compose((BLOCK, ?, ?), <>))

slide-22
SLIDE 22

name

String name()

  • Returns the name of this.
  • Ensures:

name = this.name

9 January 2019 OSU CSE 22

slide-23
SLIDE 23

Example

9 January 2019 OSU CSE 23

Code State

p = ("MyBug", p.context, p.body)

String pn = p.name();

slide-24
SLIDE 24

Example

9 January 2019 OSU CSE 24

Code State

p = ("MyBug", p.context, p.body)

String pn = p.name();

p = ("MyBug", p.context, p.body) pn = "MyBug"

slide-25
SLIDE 25

setName

void setName(String n)

  • Replaces the name of this with n.
  • Replaces: this.name
  • Requires:

[n is a valid IDENTIFIER]

  • Ensures:

this.name = n

9 January 2019 OSU CSE 25

slide-26
SLIDE 26

Example

9 January 2019 OSU CSE 26

Code State

p = ("Unnamed", p.context, p.body)

p.setName("Pest");

slide-27
SLIDE 27

Example

9 January 2019 OSU CSE 27

Code State

p = ("Unnamed", p.context, p.body)

p.setName("Pest");

p = ("Pest", p.context, p.body)

slide-28
SLIDE 28

newContext

Map<String,Statement> newContext()

  • Creates and returns an empty Map<String,

Statement> of the dynamic type needed in swapContext.

  • Ensures:

newContext = {}

9 January 2019 OSU CSE 28

slide-29
SLIDE 29

Example

9 January 2019 OSU CSE 29

Code State

p = (p.name, p.context, p.body)

Map<String,Statement> c = p.newContext();

slide-30
SLIDE 30

Example

9 January 2019 OSU CSE 30

Code State

p = (p.name, p.context, p.body)

Map<String,Statement> c = p.newContext();

p = (p.name, p.context, p.body) c = {}

slide-31
SLIDE 31

swapContext

void swapContext(Map<String,Statement> c)

  • Exchanges the context of this with that of c; c must have the

dynamic type returned by newContext.

  • Updates: this.context, c
  • Requires:

[names in c are valid IDENTIFIERs] and [names in c do not match the names of primitive instructions in the BL language] and [bodies in c are all BLOCK statements]

  • Ensures:

c = #this.context and this.context = #c

9 January 2019 OSU CSE 31

slide-32
SLIDE 32

Example

9 January 2019 OSU CSE 32

Code State

p = (p.name, {("Foo", )}, p.body) c = {("Bar", )}

p.swapContext(c);

slide-33
SLIDE 33

Example

9 January 2019 OSU CSE 33

Code State

p = (p.name, {("Foo", )}, p.body) c = {("Bar", )}

p.swapContext(c);

p = (p.name, {("Bar", )}, p.body) c = {("Foo", )}

slide-34
SLIDE 34

newBody

Statement newBody()

  • Creates and returns a Statement with a default

initial value, of the dynamic type needed in swapBody.

  • Ensures:

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

9 January 2019 OSU CSE 34

slide-35
SLIDE 35

Example

9 January 2019 OSU CSE 35

Code State

p = (p.name, p.context, p.body)

Statement b = p.newBody();

slide-36
SLIDE 36

Example

9 January 2019 OSU CSE 36

Code State

p = (p.name, p.context, p.body)

Statement b = p.newBody();

p = (p.name, p.context, p.body) b =

BLOCK

slide-37
SLIDE 37

swapBody

void swapBody(Statement b)

  • Exchanges the body of this with that of b; b must have

the dynamic type returned by newBody.

  • Updates: this.body, b
  • Requires:

[b is a BLOCK statement]

  • Ensures:

b = #this.body and this.body = #b

9 January 2019 OSU CSE 37

slide-38
SLIDE 38

Example

9 January 2019 OSU CSE 38

Code State

p = (p.name, p.context, ) b =

p.swapBody(b);

slide-39
SLIDE 39

Example

9 January 2019 OSU CSE 39

Code State

p = (p.name, p.context, ) b =

p.swapBody(b);

p = (p.name, p.context, ) b =

slide-40
SLIDE 40

Resources

  • OSU CSE Components API: Program

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

9 January 2019 OSU CSE 40