Programming Languages Implementation of Control Structures. Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM. Contents. Sequence control Data control. Sequence Control. Expressions Statements Subprograms. Expressions. Control mechanism Syntax
01 / 125Programming Languages Implementation of Control Structures
1 / 17
Télécharger la présentation
Programming Languages Implementation of Control Structures
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Control Mechanism • Functional composition: (A + B)*(C - A) * (+ (A, B), - (C, A))
Syntax • Infix: A * B + C • binary operations only • computation order ambiguity
Syntax • Prefix: • ordinary * (+ (A, B), - (C, A)) • Cambridge Polish (* (+ A B)(- C A)) • Polish * + A B - C A
Syntax • Prefix: • different numbers of operands • ordinary/ Cambridge Polish: cumbersome with parentheses • Polish: number of operands known in advance
Syntax • Postfix: • ordinary ((A, B) +,(C, A) -) * • Polish A B + C A - * • suitable execution-time representation
Execution-Time Representation • Compilation: machine code sequences PUSHA PUSHB ADD PUSHC PUSHA SUB MUL A A A + B B A + B A + B A + B (A+B)*(C-A) C C C - A A
Execution-Time Representation • Compilation: machine code sequences PUSHA PUSHB ADD PUSHC PUSHA SUB MUL A A A + B B A B + C A - * A + B A + B A + B (A+B)*(C-A) C C C - A A
Evaluation • No simple uniform evaluation rule is satisfactory: * + - A B C A
Evaluation • Side effects: A*FOO(X) + A + * A A FOO(X)
Simple Call-Return • No recursive calls • Explicit calls • Complete execution • Immediate control transfer • Single execution sequence
Simple Call-Return MAIN A B I0 I2 I4 call A call B I1 I3 return return end
Simple Call-Return MAIN I0 call A I1 end local data MAIN CIP(current instruction pointer) I0
Simple Call-Return MAIN A I0 I2 call A call B I1 I3 return end local data A local data MAIN I1 I2 CIP
Simple Call-Return MAIN A B I0 I2 I4 call A call B I1 I3 return return end local data B local data A local data MAIN I3 I1 I4 CIP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 -- -- local data MAIN CEP(current environment pointer) I0 CIP R0
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 -- I1 -- R0 local data MAIN local data A I2 CIP R1 CEP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 R2 -- I1 I3 -- R0 R1 local data MAIN local data A local data B I4 CIP R2 CEP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 R2 R3 -- I1 I3 I5 -- R0 R1 R2 local data MAIN local data A local data B local data A I2 CIP R3 CEP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 R2 R3 -- I1 I3 I5 -- R0 R1 R2 local data MAIN local data A local data B local data A Dynamic chain I2 CIP R3 CEP
Central Stack MAIN I0 R0 CIP I0 -- MAIN -- call A CEP R0 local data I1 end
Central Stack MAIN I0 R0 CIP I2 -- -- MAIN call A CEP R1 local data I1 R1 I1 end R0 A A local data I2 call B I3 return
Central Stack A I2 R0 CIP I4 -- -- MAIN call B CEP R2 local data I3 R1 I1 return R0 A B local data R2 I4 I3 R1 B call A local data I5 return
Central Stack B R0 -- I4 CIP I2 -- MAIN call A local data CEP R3 I5 R1 I1 return R0 A local data A R2 I3 I2 R1 B call B local data R2 I3 I5 return R2 A local data
Exercises • Illustrate the storage representation of A:array[0..1, 1..2, 1..3]of integer using the column-major order. Give the accessing formula for computing the location of A[I, J, K], supposing that the size of an integer is 2 bytes.
Exercises • Given the following program: programMAIN; functionFAC(N:integer): integer; beginifN <= 1 thenFAC := 1 else FAC := N * FAC(N - 1) end; begin write(FAC(3)) end. Illustrate the code segment and activation records of MAIN and FAC in the central stack during execution of the program.
Data Control • Basic concepts • Local data and environments • Shared data: dynamic scope • Shared data: block structure • Shared data: parameter transmission