program
play

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


  1. Program 9 January 2019 OSU CSE 1

  2. 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 some Statement s, and more … 9 January 2019 OSU CSE 2

  3. Program • 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

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

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

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

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

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

  9. Example p = ("SteerClear", {("Avoid", ), ("Flee", )}, BLOCK BLOCK ) BLOCK 9 January 2019 OSU CSE 9

  10. Example p = ("SteerClear", {("Avoid", ), ("Flee", )}, BLOCK BLOCK IF random THEN ) turnright BLOCK ELSE turnleft END IF 9 January 2019 OSU CSE 10

  11. Example p = ("SteerClear", {("Avoid", ), ("Flee", )}, BLOCK BLOCK ) BLOCK turnright turnright 9 January 2019 OSU CSE 11

  12. Example p = ("SteerClear", {("Avoid", ), ("Flee", )}, WHILE true DO BLOCK BLOCK IF next-is-empty THEN move ELSE IF next-is-wall THEN Flee ) ELSE BLOCK Avoid END IF END IF END WHILE 9 January 2019 OSU CSE 12

  13. Interfaces and Classes Standard extends Program- Kernel extends Program implements Program1 9 January 2019 OSU CSE 13

  14. Interfaces and Classes Standard extends Program- ProgramKernel Kernel has contracts for these methods: extends name setName Program newContext swapContext implements newBody Program1 swapBody 9 January 2019 OSU CSE 14

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

  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

  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

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

  19. 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

  20. Example Code State Program p = new Program1(); 9 January 2019 OSU CSE 20

  21. Example Code State Program p = new Program1(); p = ("Unnamed", {}, compose ((BLOCK, ?, ?), <>)) 9 January 2019 OSU CSE 21

  22. name String name() • Returns the name of this . • Ensures: name = this .name 9 January 2019 OSU CSE 22

  23. Example Code State p = ("MyBug", p.context, p.body) String pn = p.name(); 9 January 2019 OSU CSE 23

  24. Example Code State p = ("MyBug", p.context, p.body) String pn = p.name(); p = ("MyBug", p.context, p.body) pn = "MyBug" 9 January 2019 OSU CSE 24

  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

  26. Example Code State p = ("Unnamed", p.context, p.body) p.setName("Pest"); 9 January 2019 OSU CSE 26

  27. Example Code State p = ("Unnamed", p.context, p.body) p.setName("Pest"); p = ("Pest", p.context, p.body) 9 January 2019 OSU CSE 27

  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

  29. Example Code State p = (p.name, p.context, p.body) Map<String,Statement> c = p.newContext(); 9 January 2019 OSU CSE 29

  30. Example Code State p = (p.name, p.context, p.body) Map<String,Statement> c = p.newContext(); p = (p.name, p.context, p.body) c = {} 9 January 2019 OSU CSE 30

  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

  32. Example Code State p = (p.name, {("Foo", )}, p.body) c = {("Bar", )} p.swapContext(c); 9 January 2019 OSU CSE 32

  33. Example Code State p = (p.name, {("Foo", )}, p.body) c = {("Bar", )} p.swapContext(c); p = (p.name, {("Bar", )}, p.body) c = {("Foo", )} 9 January 2019 OSU CSE 33

  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

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

  36. Example Code State p = (p.name, p.context, p.body) Statement b = p.newBody(); p = (p.name, p.context, p.body) BLOCK b = 9 January 2019 OSU CSE 36

  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

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

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

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

Recommend


More recommend