ITI Introduction to Computing II

Similar documents
ITI Introduction to Computing II

Chapter 7. Kleene s Theorem. 7.1 Kleene s Theorem. The following theorem is the most important and fundamental result in the theory of FA s:

Lecture 10. Solution of Nonlinear Equations - II

Previously. Extensions to backstepping controller designs. Tracking using backstepping Suppose we consider the general system

( ) D x ( s) if r s (3) ( ) (6) ( r) = d dr D x

Data Structures. Element Uniqueness Problem. Hash Tables. Example. Hash Tables. Dana Shapira. 19 x 1. ) h(x 4. ) h(x 2. ) h(x 3. h(x 1. x 4. x 2.

Class Summary. be functions and f( D) , we define the composition of f with g, denoted g f by

School of Electrical and Computer Engineering, Cornell University. ECE 303: Electromagnetic Fields and Waves. Fall 2007

Convert the NFA into DFA

9.4 The response of equilibrium to temperature (continued)

(a) Counter-Clockwise (b) Clockwise ()N (c) No rotation (d) Not enough information

Language Processors F29LP2, Lecture 5

Deterministic simulation of a NFA with k symbol lookahead

This immediately suggests an inverse-square law for a "piece" of current along the line.

Electric Potential. and Equipotentials

CHAPTER 18: ELECTRIC CHARGE AND ELECTRIC FIELD

5.1 Definitions and Examples 5.2 Deterministic Pushdown Automata

Radial geodesics in Schwarzschild spacetime

Pushdown Automata (PDAs)

Optimization. x = 22 corresponds to local maximum by second derivative test

Physics 604 Problem Set 1 Due Sept 16, 2010

HOW TO TEACH THE FUNDAMENTALS OF INFORMATION SCIENCE, CODING, DECODING AND NUMBER SYSTEMS?

EECE 260 Electrical Circuits Prof. Mark Fowler

FI 2201 Electromagnetism

CHAPTER 2d. MATRICES

Mark Scheme (Results) January 2008

A Bijective Approach to the Permutational Power of a Priority Queue

7.5-Determinants in Two Variables

π,π is the angle FROM a! TO b

CS 275 Automata and Formal Language Theory

Week 8. Topic 2 Properties of Logarithms

2-Way Finite Automata Radboud University, Nijmegen. Writer: Serena Rietbergen, s Supervisor: Herman Geuvers

School of Electrical and Computer Engineering, Cornell University. ECE 303: Electromagnetic Fields and Waves. Fall 2007

The Formulas of Vector Calculus John Cullinan

CS 275 Automata and Formal Language Theory

Discrete Model Parametrization

Section 35 SHM and Circular Motion

CSCI 340: Computational Models. Transition Graphs. Department of Computer Science

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Continuous Charge Distributions

For convenience, we rewrite m2 s m2 = m m m ; where m is repeted m times. Since xyz = m m m nd jxyj»m, we hve tht the string y is substring of the fir

The Area of a Triangle

On the Eötvös effect

Unit #9 : Definite Integral Properties; Fundamental Theorem of Calculus

Physics 11b Lecture #11

Two dimensional polar coordinate system in airy stress functions

Finite Automata-cont d

Jim Lambers MAT 169 Fall Semester Lecture 4 Notes

Energy Dissipation Gravitational Potential Energy Power

dx was area under f ( x ) if ( ) 0

This lecture covers Chapter 8 of HMU: Properties of CFLs

Review of Calculus, cont d

Riemann Sums and Riemann Integrals

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Theory of Computation Regular Languages

Homework 3 MAE 118C Problems 2, 5, 7, 10, 14, 15, 18, 23, 30, 31 from Chapter 5, Lamarsh & Baratta. The flux for a point source is:

Lecture 14. Protocols. Key Distribution Center (KDC) or Trusted Third Party (TTP) KDC generates R1

Friedmannien equations

Riemann Sums and Riemann Integrals

MA 513: Data Structures Lecture Note Partha Sarathi Mandal

A Mathematica package to cope with partially ordered sets

Improper Integrals, and Differential Equations

Linear Inequalities. Work Sheet 1

Recursively Enumerable and Recursive. Languages

Handout: Natural deduction for first order logic

3.1 Magnetic Fields. Oersted and Ampere

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Before we can begin Ch. 3 on Radicals, we need to be familiar with perfect squares, cubes, etc. Try and do as many as you can without a calculator!!!

CS 314 Principles of Programming Languages

The Regulated and Riemann Integrals

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Solution of fuzzy multi-objective nonlinear programming problem using interval arithmetic based alpha-cut

Probabilistic Retrieval

1 Using Integration to Find Arc Lengths and Surface Areas

Vyacheslav Telnin. Search for New Numbers.

SUMMER KNOWHOW STUDY AND LEARNING CENTRE

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

Multiplying integers EXERCISE 2B INDIVIDUAL PATHWAYS. -6 ì 4 = -6 ì 0 = 4 ì 0 = -6 ì 3 = -5 ì -3 = 4 ì 3 = 4 ì 2 = 4 ì 1 = -5 ì -2 = -6 ì 2 = -6 ì 1 =

NS-IBTS indices calculation procedure

Lecture 3 ( ) (translated and slightly adapted from lecture notes by Martin Klazar)

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

Math 426: Probability Final Exam Practice

Michael Rotkowitz 1,2

Answers to test yourself questions

Nondeterminism and Nodeterministic Automata


Let's start with an example:

1 Probability Density Functions

We partition C into n small arcs by forming a partition of [a, b] by picking s i as follows: a = s 0 < s 1 < < s n = b.

Lecture 08: Feb. 08, 2019

Andersen s Algorithm. CS 701 Final Exam (Reminder) Friday, December 12, 4:00 6:00 P.M., 1289 Computer Science.

n f(x i ) x. i=1 In section 4.2, we defined the definite integral of f from x = a to x = b as n f(x i ) x; f(x) dx = lim i=1

Chapter 3. Vector Spaces

Review of Mathematical Concepts

Regular Language. Nonregular Languages The Pumping Lemma. The pumping lemma. Regular Language. The pumping lemma. Infinitely long words 3/17/15

Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem 2/16/15

Bases for Vector Spaces

10 m, so the distance from the Sun to the Moon during a solar eclipse is. The mass of the Sun, Earth, and Moon are = =

Transcription:

ITI 1121. Intoduction to Computing II Mcel Tucotte School of Electicl Engineeing nd Compute Science Abstct dt type: Stck Stck-bsed lgoithms Vesion of Febuy 2, 2013 Abstct These lectue notes e ment to be looked t on compute sceen. Do not pint them unless it is necessy.

Evluting ithmetic expessions Stck-bsed lgoithms e used fo syntcticl nlysis (psing). Fo exmple to evlute the following expession: 1 + 2 3 4 Compiles use simil lgoithms to check the syntx of you pogms nd genete mchine instuctions (executble). To veify tht pentheses e blnced: ([]) is ok, but not ([)] o )((())(.

The fist two steps of the nlysis of souce pogm by compile e the lexicl nlysis nd the syntcticl nlysis. Duing the lexicl nlysis (scnning) the souce code is ed fom left to ight nd the chctes e egouped into tokens, which e successive chctes tht constitute numbes o identifies. One of the tsks of the lexicl nlyse is to emove spces fom the input. E.g.: 10 + 2 + 300 whee epesent blnk spces, is tnsfomed into the following list of tokens: [10,+,2,+,300]

The next step is the syntcticl nlysis (psing) nd consists in egouping the tokens into gmmticl units, fo exmple the sub-expessions of RPN expessions (seen in clss this week). In the next slides, we look t simple exmples of lexicl nd syntcticl nlysis.

public clss Test { public sttic void scn( Sting expession ) { Rede ede = new Rede( expession ); } while ( ede.hsmoetokens() ) { System.out.pintln( ede.nexttoken() ); } public sttic void min( Sting[] gs ) { scn( " 3 + 4 * 567 " ); } } // > jv Test // INTEGER: 3 // SYMBOL: + // INTEGER: 4 // SYMBOL: * // INTEGER: 567

public clss Token { pivte sttic finl int INTEGER = 1; pivte sttic finl int SYMBOL = 2; pivte int ivlue; pivte Sting svlue; pivte int type; public Token( int ivlue ) { this.ivlue = ivlue; type = INTEGER; } public Token( Sting svlue ) { this.svlue = svlue; type = SYMBOL; } public int ivlue() {... } public Sting svlue() {... } } public boolen isintege() { etun type == INTEGER; } public boolen issymbol() { etun type == SYMBOL; }

LR Scn public sttic int execute( Sting expession ) { Token op = null; int l = 0, = 0; Rede ede = new Rede( expession ); l = ede.nexttoken().ivlue(); } while ( ede.hsmoetokens() ) { op = ede.nexttoken(); = ede.nexttoken().ivlue(); l = evl( op, l, ); } etun l;

evl( Token op, int l, int ) public sttic int evl( Token op, int l, int ) { int esult = 0; if ( op.svlue().equls( "+" ) ) esult = l + ; else if ( op.svlue().equls( "-" ) ) esult = l - ; else if ( op.svlue().equls( "*" ) ) esult = l * ; else if ( op.svlue().equls( "/" ) ) esult = l / ; else System.e.pintln( "not vlid symbol" ); } etun esult;

Evluting n ithmetic expession: LR Scn Left-to-ight lgoithm: Decle L, R nd OP Red L While not end-of-expession do: Red OP Red R Evlute L OP R Stoe esult in L At the end of the loop the esult cn be found in L.

3 * 8-10

3 + 4-5 ^ L = 3 OP = R = > Red L While not end-of-expession do: Red OP Red R Evlute L OP R Stoe esult in L

3 + 4-5 ^ L = 3 OP = + R = Red L While not end-of-expession do: > Red OP Red R Evlute L OP R Stoe esult in L

3 + 4-5 ^ L = 3 OP = + R = 4 Red L While not end-of-expession do: Red OP > Red R Evlute L OP R Stoe esult in L

3 + 4-5 ^ L = 3 OP = + R = 4 Red L While not end-of-expession do: Red OP Red R > Evlute L OP R (7) Stoe esult in L

3 + 4-5 ^ L = 7 OP = + R = 4 Red L While not end-of-expession do: Red OP Red R Evlute L OP R > Stoe esult in L

3 + 4-5 ^ L = 7 OP = - R = 4 Red L While not end-of-expession do: > Red OP Red R Evlute L OP R Stoe esult in L

3 + 4-5 ^ L = 7 OP = - R = 5 Red L While not end-of-expession do: Red OP > Red R Evlute L OP R Stoe esult in L

3 + 4-5 ^ L = 7 OP = - R = 5 Red L While not end-of-expession do: Red OP Red R > Evlute L OP R (2) Stoe esult in L

3 + 4-5 ^ L = 2 OP = - R = 5 Red L While not end-of-expession do: Red OP Red R Evlute L OP R > Stoe esult in L

3 + 4-5 ^ L = 2 OP = - R = 5 > Red L While not end-of-expession do: Red OP Red R Evlute L OP R Stoe esult in L end of expession, exit the loop, L contins the esult.

Wht do you think? Without pentheses the following expession cnnot be evluted coectly: 7 - (3-2) Becuse the esult of the left-to-ight nlysis coesponds to: (7-3) - 2 Similly the following expession cnnot be evluted by ou simple lgoithm: 7-3 * 2 Since the left-to-ight nlysis coesponds to: (7-3) * 2 But ccoding to the opeto pecedences, the evlution should hve poceeded s follows: 7 - (3 * 2)

Remks The left-to-ight lgoithm: Does not hndle pentheses; No pecedence. Solutions: 1. Use diffeent nottion; 2. Develop moe complex lgoithms. Both solutions involve stcks!

Nottions Thee e 3 wys to epesent the following expession: L OP R. infix: this is the usul nottion, the opeto is sndwiched in between its opends: L OP R; postfix: in postfix nottion, the opends e plced befoe the opeto, L R OP. This nottion is lso clled Revese Polish Nottion o RPN, it s the nottion used by cetin scientific clcultos (such s the HP-35 fom Hewlett-Pckd o the Texs Instuments TI-89 using the RPN Intefce by Ls Fedeiksen 1 ) o PostScipt pogmming lnguge. 7 - (3-2) = 7 3 2 - - (7-3) - 2 = 7 3-2 - pefix: the thid nottion consists in plcing the opeto befoe the opends, OP L R. The pogmming lnguge Lisp uses combintion of pentheses nd pefix nottion: (- 7 (* 3 2)). 1 www.clculto.og/pn.html

Infix postfix (mentlly) Successively tnsfom, one by one, ll the sub-expessions following the sme ode of evlution tht you would nomlly follow to evlute n infix expession. An infix expession l becomes l, whee l nd e sub-expessions nd is n opeto.

9 / ( 2 4 5 ) 9 / ( 2 }{{} l 9 / ( l {}}{ 2 }{{} {}}{ 4 4 }{{} 5 ) {}}{ 5 ) 9 / ( [ 2 4 ] 5 ) 9 / ( [ 2 4 ] }{{} l l {}}{ 9 / [ [ 2 4 ] 9 }{{} l l {}}{ 9 }{{} {}}{ 5 9 / [ 2 4 5 ] / }{{} 5 }{{} ) {}}{ ] [ 2 4 5 ] }{{} {}}{ [ 2 4 5 ] 9 2 4 5 / {}}{ /

Evluting postfix expession (mentlly) Scn the expession fom left to ight. When the cuent element is n opeto, pply the opeto to its opends, i.e. eplce l by the esult of the evlution of l. 9 2 }{{} l 9 2 4 5 / 9 9 }{{} 8 l 9 }{{} 9 l }{{} 4 l {}}{ }{{} 8 5 / }{{} 5 }{{} l {}}{ 3 / 3 }{{} l {}}{ 3 / }{{} 5 / /

Evluting postfix expession Until the end of the expession hs been eched: 1. Fom left to ight until the fist opeto; 2. Apply the opeto to the two peceding opends; 3. Replce the opeto nd its opends by the esult. At the end we hve esult.

9 3 / 10 2 3 * - +

9 2 4 * 5 - /

Remks: infix vs postfix The ode of the opends is the sme fo both nottions, howeve opetos e inseted t diffeent plces: 2 + (3 * 4) = 2 3 4 * + (2 + 3) * 4 = 2 3 + 4 * Evluting n infix expession involves hndling opetos pecedence nd penthesis in the cse of the postfix nottion, those two concepts e embedded in the expession, i.e. the ode of the opends nd opetos.

Wht ole will the stck be plying? opends = new stck; Algoithm: Evl Infix while ( "hs moe tokens" ) { t = next token; if ( "t is n opend" ) { opends.push( "the intege vlue of t" ); } else { // this is n opeto op = "opeto vlue of t"; = opends.pop(); l = opends.pop(); opends.push( "evl( l, op, )" ); } } etun opends.pop();

Evluting postfix expession The lgoithm equies stck (Numbes), vible tht contins the lst element tht ws ed (X) nd two moe vibles, L nd R, whose pupose is the sme s befoe. Numbes = [ While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes (ight befoe left?!) L = POP Numbes Evlute L X R; PUSH esult onto Numbes To obtin the finl esult: POP Numbes.

9 3-2 /

9 3 / 10 2 3 * - +

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ > Numbes = [ X = L = R = While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Cete new stck

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [ X = 9 L = R = While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 X = 9 L = R = While not end-of-expession do: Red X > If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Push X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 X = 2 L = R = While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 2 X = 2 L = R = While not end-of-expession do: Red X > If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Push X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 2 X = 4 L = R = While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 2 4 X = 4 L = R = While not end-of-expession do: Red X > If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Push X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 2 4 X = * L = R = While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 2 X = * L = R = 4 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, > R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Ah! X is n opeto, pop the top element sve into R

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 X = * L = 2 R = 4 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes > L = POP Numbes Evlute L X R; PUSH esult onto Numbes Top element is emoved nd sved into L

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 8 X = * L = 2 R = 4 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes > Evlute L X R; PUSH esult onto Numbes Push the esult of L X R, 2 4 = 8, onto the stck

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 8 X = 5 L = 2 R = 4 While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 8 5 X = 5 L = 2 R = 4 While not end-of-expession do: Red X > If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Push X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 8 5 X = - L = 2 R = 4 While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 8 X = - L = 2 R = 5 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, > R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Remove the top element nd sve it into R

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 X = - L = 8 R = 5 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes > L = POP Numbes Evlute L X R; PUSH esult onto Numbes Remove the top element nd sve it into L

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 3 X = - L = 8 R = 5 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes > Evlute L X R; PUSH esult onto Numbes Push the esult of L X R, 8 5 = 3, onto the stck

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 3 X = / L = 8 R = 5 While not end-of-expession do: > Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes Red X

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [9 X = / L = 8 R = 3 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, > R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes R = POP Numbes.

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [ X = / L = 9 R = 3 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes > L = POP Numbes Evlute L X R; PUSH esult onto Numbes L = POP Numbes.

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [3 X = / L = 9 R = 3 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes > Evlute L X R; PUSH esult onto Numbes Push L X R, 9 3 = 3, onto the stck su l pile.

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [3 X = / L = 9 R = 3 While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes > Evlute L X R; PUSH esult onto Numbes End-of-expession

9 / ((2 * 4) - 5) = 9 2 4 * 5 - / ^ Numbes = [ X = / L = 9 R = 3 > While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Evlute L X R; PUSH esult onto Numbes The esult is POP Numbes = 3 ; the stck is now empty

Poblem Rthe thn evluting n RPN expession, we would like to convet n RPN expession to infix (usul nottion). Hum? Do we need new lgoithm? No, simple modifiction will do, eplce Evlute L OP R by Conctente (L OP R). Note: pentheses e essentil (not ll of them but some e). This time the stck does not contin numbes but chcte stings tht epesent sub-expessions.

9 5 6 3 / - /

Postfix infix Sting pntoinfix(sting[] tokens) Numbes = [ X = L = R = While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Conctente ( L X R ); PUSH esult onto Numbes

Postfix? While not end-of-expession do: Red X If X isnumbe, PUSH X onto Numbes If X isopeto, R = POP Numbes L = POP Numbes Pocess L X R; PUSH esult onto Numbes We ve seen n exmple whee Pocess == Evlute, then one whee Pocess == Conctente, but Pocess could lso poduce ssembly code (i.e. mchine instuctions). This shows how pogms e compiled o tnslted.

Memoy mngement hep (instnces) fee bsept Stck fo method clls Vibles sttic pogm (byte code) Jv Vitul Mchine Method n ctivtion ecod... Method 2 ctivtion ecod Method 1 (min) ctivtion ecod Pogm counte Locl vibles Pmetes Retun ddess Pevious bsept Retun vlue Schemtic nd simplified epesenttion of the memoy duing the execution of Jv pogm.

The Jv Vitul Mchine (JVM) must: Method cll 1. Cete new ctivtion ecod/block (which contins spce fo the locl vibles nd the pmetes mong othe things); 2. Sve the cuent vlue of bsept in the ctivtion ecod nd set the bsept to the ddess of the cuent ecod; 3. Sve the vlue of the pogm counte in the designted spce of the ctivtion ecod, set the pogm counte to the fist instuction of the cuent method; 4. Copy the vlues of the effective pmetes into the designted e of the cuent ctivtion ecod; 5. Initil the locl vibles; 6. Stt executing the instuction designted by the pogm counte. ctivtion block = stck fme, cll fme o ctivtion ecod.

When the method ends The JVM must: 1. Sve the etun vlue (t the designted spce) 2. Retun the contol to the clling method, i.e. set the pogm counte nd bsept bck to thei pevious vlue; 3. Remove the cuent block; 4. Execute instuction designted by the cuent vlue of the pogm counte.

Exmple 1 (simplified) public clss Clls { public sttic int c( int v ) { int n; n = v + 1; etun n; } public sttic int b( int v ) { int m,n; m = v + 1; n = c( m ); etun n; } public sttic int ( int v ) { int m,n; m = v + 1; n = b( m ); etun n; }

} public sttic void min( Sting[] gs ) { int m,n; m = 1; n = ( m ); System.out.pintln( n ); }

Exmple 1 (simplified) : min: v m n gs m n 1 1 (1)

Exmple 1 (simplified) b: : min: v m n v m n gs m n 2 1 2 1 b(2) (1)

Exmple 1 (simplified) c: v n 3 c(3) b: v m 2 3 n b(2) : v 1 m n 2 (1) min: gs m 1 n

Exmple 1 (simplified) 4 c(3) b: : min: v m n v m n gs m n 2 3 4 1 2 1 b(2) (1)

Exmple 1 (simplified) 4 b(2) : v m n 1 2 4 (1) min: gs m n 1

Exmple 1 (simplified) 4 (1) min: gs m n 1 4

Exmple 1: summy c: 4 v n 3 4 c(3) b: 4 v m n 2 3 4 b(2) : 4 v m n 1 2 4 (1) min: gs m n 1 4

Exmple 2 (with pogm counte) 01 public clss Fct { 02 public sttic int fct( int n ) { 03 // pe-condition: n >= 0 04 int, ; 05 if ( n == 0 n == 1 ) { 06 = 1; 07 } else { 08 = fct( n-1 ); 09 = n * ; 10 } 11 etun ; 12 } 13 public sttic void min( Sting[] gs ) { 14 int, n; 15 n = 3; 16 = fct( n ); 17 } 18}

min gs n pogm counte 15

min gs n 3 pogm counte 16

fct n etun ddess min gs n 3 fct( 3 ) pogm counte 16

fct n 3 etun ddess min gs n 3 fct( 3 ) pogm counte 16

fct n 3 etun ddess 16 min gs n 3 fct( 3 ) pogm counte 16

fct n 3 etun ddess 16 min gs n 3 fct( 3 ) pogm counte 5

fct n 3 etun ddess 16 min gs n 3 fct( 3 ) pogm counte 8

fct n etun ddess fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 8

fct n 2 etun ddess fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 8

fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 8

fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 5

fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 8

fct n etun ddess fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 8

fct n 1 etun ddess fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 8

fct n 1 etun ddess 8 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 8

fct n 1 etun ddess 8 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 5

fct n 1 etun ddess 8 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 6

fct n 1 1 etun ddess 8 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 11

fct n 1 1 etun ddess 8 1 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 11

fct n 1 1 etun ddess 8 1 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 1 ) fct( 2 ) fct( 3 ) pogm counte 8

1 fct n 2 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 8

1 fct n 2 1 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 9

1 fct n 2 2 1 etun ddess 8 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 11

1 fct n 2 2 1 etun ddess 8 2 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 11

1 fct n 2 2 1 etun ddess 8 2 fct n 3 etun ddess 16 min gs n 3 fct( 2 ) fct( 3 ) pogm counte 8

2 fct n 3 etun ddess 16 min gs n 3 fct( 3 ) pogm counte 8

2 fct n 3 2 etun ddess 16 min gs n 3 fct( 3 ) pogm counte 9

2 fct n 3 6 2 etun ddess 16 min gs n 3 fct( 3 ) pogm counte 11

2 fct n 3 6 2 etun ddess 16 6 min gs n 3 fct( 3 ) pogm counte 11

2 fct n 3 6 2 etun ddess 16 6 min gs n 3 fct( 3 ) pogm counte 16

min 6 gs n 3 pogm counte 16

min 6 gs n 3 6 pogm counte 17