Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Slides:



Advertisements
Similar presentations
BugsWorld Project The Game The Simulator The Language The Translator.
Advertisements

Review This is a summary/review of the model of software as presented in 221/222 and discussed in “Software Component Engineering With Resolve/C++”
Code Generator Translator Architecture Parser Tokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Remember Sorting_Machine? Isn’t it a beauty! Go ahead... kick the tires! Change_To_ Extraction_Phase InOut.
Models of Concurrency Manna, Pnueli.
A New Math Type: Tree. Math Tree Continued… E HK CGFL B AD J...
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
ITEC 320 Lecture 12 Records. Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
MIS 3200 – Unit 4 Complex Conditional Statements – else if – switch.
2Object-Oriented Program Development Using C++ 3 Relational Expressions Decision-making: comparison of two numerical values Relational expression –Also.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Sieve Loops Tony Hansen
UNIT II Decision Making And Branching Decision Making And Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Implementing Control Logic in C# Svetlin Nakov Telerik Corporation
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Imperative Programming
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Computer Science Standard Level Mastery Aspects. Mastery Item Claimed JustificationWhere Listed Arrays Used to store the student data Lines P.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
BL Program PROGRAM identifier IS BEGIN END identifier sequence of statements sequence of new instructions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Compiler Chapter# 5 Intermediate code generation.
Context-Free Grammars and Parsing
PART I: overview material
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Recursive Descent Parsers Read and recognize the input (in order to translate it or evaluate it) Implicitly construct the derivation tree Design is driven.
CS320n –Visual Programming Introduction to Recursion (Slides 8-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
JavaScript, Fourth Edition
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Week 10: Loop Invariants, Code correctness Jimmy Voss Disclaimer: Some material may have been borrowed from both the Official Course slides as well as.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
COMP Loop Statements Yi Hong May 21, 2015.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
COMP 2710 Software Construction Flow of Control – switch and loops Programming Exercises Dr. Xiao Qin Auburn University
CSC115 Introduction to Computer Programming
Control Structures.
Java Programming Control Structures Part 1
Pages:51-59 Section: Control1 : decisions
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Looping III (do … while statement)
Programming Concepts and Database
Using Decision Structures
Pages:51-59 Section: Control1 : decisions
8 Once features.
Presentation transcript:

Statement Component IF next-is-empty THEN move turnright ELSE IF next-is-enemy THEN infect END IF How can we use tree to model a BL statement ?

Statement Component What’s wrong with IF_ELSE NEXT_IS_EMPTY CALL move IF NEXT_IS_ENEMY CALL infect CALL turnright

Statement Component An Abstract Syntax Tree IF_ELSE NEXT_IS_EMPTY CALL turnright IF NEXT_IS_ENEMY CALL infect BLOCK CALL move

BL Statements IF test THEN END IF WHILE test DO END WHILE IF test THEN ELSE END IF instruction

BL Statements: IF IF test THEN END IF IF test BLOCK...

BL Statements: IF_ELSE BLOCK... IF test THEN ELSE END IF IF_ELSE test BLOCK...

BL Statements: WHILE WHILE test BLOCK... WHILE test DO END WHILE

BL Statements: CALL CALL instruction

An Example WHILE true DO IF next-is-enemy THEN infect ELSE IF next-is-empty THEN move ELSE turnleft END IF END WHILE CALL infect CALL move IF_ELSE NEXT_IS_EMPTY BLOCK CALL turnleft WHILE TRUE BLOCK IF_ELSE NEXT_IS_ENEMY BLOCK

Statement Continued… Type Statement_Kernel is modeled by STATEMENT Initial Value IS_INITIAL_STATEMENT (self)

Statement Continued… math subtype STATEMENT_LABEL is ( kind: Statement_Kind test: Condition instruction: IDENTIFIER ) constraint … math subtype STATEMENT is tree of STATEMENT_LABEL exemplar s constraint IS_LEGAL_STATEMENT (s)

Statement Continued… Statement_Kind BLOCK IF IF_ELSE WHILE CALL 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 These are all integer values, but we use the names because they are more meaningful than arbitrary integer values.

Statement Continued… IS_LEGAL_STATEMENT? IF condition BLOCK WHILE condition BLOCK CALL instruction IF_ELSE condition BLOCK …

Statement Operations Operations s.Add_To_Block (pos, statement) s.Remove_From_Block (pos, statement) s.Length_Of_Block () s.Compose_If (cond, block) s.Decompose_If (cond, block) s.Compose_If_Else (cond, if_block, else_block) s.Decompose_If_Else (cond, if_block, else_block) s.Compose_While (cond, block) s.Decompose_While (cond, block) s.Compose_Call (inst) s.Decompose_Call (inst) s.Kind ()

Statement Block Operations s.Add_To_Block (pos, statement) s.Remove_From_Block (pos, statement) s.Length_Of_Block ()

Statement If Operations s.Compose_If (cond, block) s.Decompose_If (cond, block)

Statement If_Else Operations s.Compose_If_Else ( cond, if_block, else_block) s.Decompose_If_Else ( cond, if_block, else_block)

Statement While Operations s.Compose_While (cond, block) s.Decompose_While (cond, block)

Statement Call Operations s.Compose_Call (inst) s.Decompose_Call (inst)

Statement Other Operations s.Kind ()

Using Statement Operations What operations are needed to produce Statement if_stmt = object Statement call, block, if_stmt; object Text inst = “infect”; IF NEXT_IS_ENEMY CALL infect BLOCK call.Compose_Call (inst); block.Add_To_Block (0, call); object Integer cond = NEXT_IS_ENEMY; if_stmt.Compose_If (cond, block);

Using Statement Operations Consider Statement object If_Else_stmt with this value: IF_ELSE NEXT_IS_EMPTY CALL turnright IF NEXT_IS_ENEMY CALL infect BLOCK CALL move

Using Statement Operations Show the operations to produce If_Else_stmt on the previous slide: object Text inst = “move”; object Statement If_Else_stmt, then, else, call, if_stmt, block; call.Compose_Call (inst); then.Add_To_Block (0, call); inst = “turnright”; call.Compose_Call (inst); then.Add_To_Block (1, call); object Integer cond = NEXT_IS_ENEMY; call.Compose_Call (inst); block.Add_To_Block (0, call); if_stmt.Compose_If (cond, block); inst = “infect”; else.Add_To_Block (0, if_stmt); If_Else_stmt.Compose_If_Else (cond, then, else); cond = NEXT_IS_EMPTY;

Practice Operation Most operations on Statement have to be recursive Use 5 step process to recursion: 0.State the problem 1.Visualize recursive structure 2.Verify that visualized recursive structure can be leveraged into an implementation 3.Visualize a recursive implementation 4.Write a skeleton for the operation body 5.Refine the skeleton into an operation body

Step 0: State the Problem Procedure Demobilize replaces every occurrence of the “move” instruction in statement s with a “skip” instruction. global_procedure Demobilize ( alters Statement& s ); /*! ensures s = DEMOBILIZE (#s) !*/

Demobilize this Statement BLOCK IF_ELSE NEXT_IS_EMPTY IF NEXT_IS_ENEMY CALL infect BLOCK CALL move CALL move CALL skip CALL skip

Step 0: State the Problem Continued… math definition DEMOBILIZE ( s: STATEMENT ): STATEMENT satisfies if root (s).kind = CALL then if root (s).instruction = “move” then DEMOBILIZE (s) = compose ((CALL, TRUE, “skip”), empty_string) else DEMOBILIZE (s) = s else there exists label: STATEMENT_LABEL, nested_stmts: string of STATEMENT (s = compose (label, nested_stmts) and DEMOBILIZE (s) = compose (label, STRING_DEMOBILIZE (nested_stmts))

Step 0: State the Problem Continued… math definition STRING_DEMOBILIZE ( str: string of STATEMENT ): string of STATEMENT satisfies if str = empty_string then STRING_DEMOBILIZE (str) = empty_string else there exists s: STATEMENT, rest: string of STATEMENT (str = * rest and STRING_DEMOBILIZE (str) = DEMOBILIZE (s) * STRING_DEMOBILIZE (rest))

Step 1: Visualize Recursive Structure s = BLOCK... root (s).kind = BLOCK IF test root (s).kind = IF IF_ELSE test root (s).kind = IF_ELSE WHILE test root (s).kind = WHILE CALL instruction root (s).kind = CALL

Step 2: Verify That Leveraging Works Ask yourself: If Demobilize could get a helper to demobilize the nested statements in each of the five (four?) cases, could it take advantage of this generous offer? Yes! Once you know how to demobilize the nested statements, you can demobilize the entire statement.

Step 2½: Let’s Jump Ahead procedure_body Demobilize ( alters Statement& s ) { case_select (s.Kind ()) { case BLOCK: {Demobilize_Block (s);} break; case IF: {Demobilize_If (s);} break; case IF_ELSE: {Demobilize_If_Else (s);} break; case WHILE: {Demobilize_While (s);} break; case CALL: { object Text inst, skipinst = “skip”; s.Decompose_Call (inst); if ( inst == “move”) { inst &= skipinst; } s.Compose_Call (inst); } break; }

Step 2¾ (or back to Step 0  ): State the Problem global_procedure Demobilize_Block ( alters Statement& s ); /*! requires root (s).kind = BLOCK ensures s = DEMOBILIZE (#s) !*/

Steps 1-5 Condensed (BLOCK) object Statement nested; object Integer pos; while (pos < s.Length_Of_Block ()) { s.Remove_From_Block (pos, nested); Demobilize (nested); s.Add_To_Block (pos, nested); pos ++; } s = BLOCK... procedure_body Demobilize_Block ( alters Statement& s ) { }

Step ? (or back to Step 2¾ ): State the Problem Continued… global_procedure Demobilize_If ( alters Statement& s ); /*! requires root (s).kind = IF ensures s = DEMOBILIZE (#s) !*/

procedure_body Demobilize_If ( alters Statement& s ) { } Steps 1-5 Condensed (IF) object Statement if_blk; object Integer cond; s.Decompose_If (cond, if_blk); Demobilize (if_blk); s.Compose_If (cond, if_blk); IF test s =

Steps 1-5 Condensed (IF_ELSE) procedure_body Demobilize_If_Else ( alters Statement& s ) { // You try it! } s = IF_ELSE test

Statement: Representation The representation for Statement_Kernel_1 has one field: tree_rep: a Tree_Of_Tree_Node where a Tree_Node is a Record with three fields: kind: Integer test: Integer instruction: Text

Statement: Correspondence self, the Statement, is equal to self.tree_rep, the Tree used to represent it.

Statement: Convention All the labels in the tree, tree_rep, are legal statement labels (i.e., they satisfy the constraint for the definition of STATEMENT_LABEL); The tree itself is a legal statement (i.e., it satisfies the constraint for the definition of STATEMENT).

Statement: CCD S_Pretty_Print ext i S_Pretty_Print_1 i ext S_KernelS_Kernel_1 i RepresentationRecordTree_Kernel u uuenc iii i

Statement: Compose_If object Tree_Of_Tree_Node new_tree_rep, t; // make t a valid representation for an initial value of Statement t[current][kind] = BLOCK; t[current][test] = TRUE; // extract representation tree from block and consume block block[tree_rep] &= t; // construct new representation tree new_tree_rep[current][kind] = IF; new_tree_rep[current][test] &= cond; // consume cond new_tree_rep.Add (0, t); // produce self self[tree_rep] &= new_tree_rep;

Statement: Decompose_If object Tree_Of_Tree_Node new_tree_rep, t; // make new_tree_rep a valid representation for // an initial value of Statement new_tree_rep[current][kind] = BLOCK; new_tree_rep[current][test] = TRUE; // consume self self[tree_rep] &= new_tree_rep; // extract condition and body from representation of if new_tree_rep[current][test] &= cond; new_tree_rep.Remove (0, block[tree_rep]);