CS代考 CSE 13

9 January 2019 OSU CSE 1

BL Compiler Structure
string of characters (source code)

Copyright By PowCoder代写 加微信 powcoder

string of tokens (“words”)
abstract program
integers (object code)
9 January 2019
A BL program consists of some Statements, and more …
Code Generator

• The Program component family allows you to manipulate values that are models
of 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

Structure of a BL Program
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).
PROGRAM MyProg IS
END MyProg
9 January 2019

Structure of a
The instruction’s name (must be an IDENTIFIER).
The body Statement (kind must be BLOCK).
INSTRUCTION Instr IS
9 January 2019 OSU CSE

PROGRAM MyProg IS
INSTRUCTION Instr1 IS BLOCK
END Instr1
INSTRUCTION Instr2 IS BLOCK
END Instr2
END MyProg
9 January 2019
The program’s
comprises zero or more new instructions.

Example: “Draw” the Value
PROGRAM SteerClear IS INSTRUCTION Avoid IS
IF random THEN turnright
ELSE turnleft
END Avoid INSTRUCTION Flee IS
turnright END Flee
WHILE true DO
IF next-is-empty THEN move
IF next-is-enemy THEN
Avoid END IF
END IF END WHILE
END SteerClear
9 January 2019

Example: “Draw” the Value
PROGRAM SteerClear IS BEGIN
INSTRUCTION Avoid IS
IF random THEN turnright
ELSE turnleft
END Avoid INSTRUCTION Flee IS
turnright turnright
WHILE true DO
IF next-is-empty THEN
IF next-is-enemy THEN Flee
ELSE Avoid
END IF END IF
9 January 2019 OSU CSE 8
END SteerClear

p = (“SteerClear”,
{(“Avoid”,
), (“Flee”,
9 January 2019

p = (“SteerClear”,
{(“Avoid”,
), (“Flee”,
9 January 2019
IF random THEN turnright
ELSE turnleft

p = (“SteerClear”,
{(“Avoid”,
), (“Flee”,
turnright turnright
9 January 2019

p = (“SteerClear”,
{(“Avoid”,
), (“Flee”, )},
WHILE true DO BLOCK
IF next-is-empty THEN move
IF next-is-wall THEN
Avoid END IF
END IF END WHILE
9 January 2019

Interfaces and Classes
Program- Kernel
implements
9 January 2019
OSU CSE 13

Interfaces and Classes
Program- Kernel
implements
ProgramKernel
has contracts for these methods:
newContext
swapContext
9 January 2019
OSU CSE 14

Interfaces and Classes
has these additional methods (not all
discussed here):
prettyPrint
generatedCode
Program- Kernel
implements
9 January 2019
OSU CSE 15

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

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

No-argument Constructor
• Ensures:
this = (“Unnamed”, {}, compose((BLOCK, ?, ?), <>))
9 January 2019 OSU CSE 18

No-argument Constructor
• Ensures:
this = (“Unnamed”, {}, compose((BLOCK, ?, ?), <>))
The corresponding BL program is:
PROGRAM Unnamed IS BEGIN
END Unnamed
9 January 2019 OSU CSE 19

Program p =
new Program1();
9 January 2019 OSU CSE 20

Program p =
new Program1();
p = (“Unnamed”, {}, compose((BLOCK, ?, ?),
9 January 2019 OSU CSE 21

String name()
• Returnsthenameofthis. • Ensures:
name = this.name
9 January 2019 OSU CSE

p = (“MyBug”,
p.context,
String pn = p.name();
9 January 2019 OSU CSE 23

p = (“MyBug”,
p.context,
String pn = p.name();
p = (“MyBug”,
p.context,
pn = “MyBug”
9 January 2019 OSU CSE 24

void setName(String n)
• Replacesthenameofthiswithn.
• Replaces:this.name
• Requires:
[n is a valid IDENTIFIER]
• Ensures:
this.name = n
9 January 2019 OSU CSE

p = (“Unnamed”,
p.context,
p.setName(“Pest”);
9 January 2019 OSU CSE 26

p = (“Unnamed”,
p.context,
p.setName(“Pest”);
p = (“Pest”,
p.context,
9 January 2019 OSU CSE 27

newContext Map newContext()
• CreatesandreturnsanemptyMap of the dynamic type needed in swapContext.
• Ensures:
newContext = {}
9 January 2019 OSU CSE 28

p = (p.name,
p.context,
Map c =
p.newContext();
9 January 2019 OSU CSE 29

p = (p.name,
p.context,
Map c =
p.newContext();
p = (p.name,
p.context,
p.body) c = {}
9 January 2019 OSU CSE 30

swapContext void swapContext(Map 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

p = (p.name,
{(“Foo”, )},
c = {(“Bar”, )}
p.swapContext(c);
9 January 2019 OSU CSE 32

p = (p.name,
{(“Foo”, )},
c = {(“Bar”, )}
p.swapContext(c);
p = (p.name,
{(“Bar”, )},
c = {(“Foo”, )}
9 January 2019 OSU CSE 33

newBody Statement newBody()
• CreatesandreturnsaStatementwithadefault initial value, of the dynamic type needed in
swapBody. • Ensures:
compose((BLOCK, ?, ?), <>)
9 January 2019 OSU CSE

p = (p.name,
p.context,
Statement b = p.newBody();
9 January 2019 OSU CSE 35

p = (p.name,
p.context,
Statement b = p.newBody();
p = (p.name,
p.context,
9 January 2019 OSU CSE 36

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

p = (p.name,
p.context,
p.swapBody(b);
9 January 2019 OSU CSE 38

p = (p.name,
p.context,
p.swapBody(b);
p = (p.name,
p.context,
9 January 2019 OSU CSE 39

• OSU CSE Components API: Program
– http://cse.osu.edu/software/common/doc/
9 January 2019 OSU CSE 40

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com