1 / 13

Guarded Commands

COP4020 Programming Languages. Guarded Commands. COP4020 Programming Languages. Control statements were implemented ( in the 50s) in Fortran on the IBM704. Corrado Böhn and Giusseppe Jacopini (1966) proposed selection and iteration as

tyler
Télécharger la présentation

Guarded Commands

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP4020 Programming Languages Guarded Commands

  2. COP4020 Programming Languages • Control statements were implemented ( in the 50s) in Fortran on the IBM704. • CorradoBöhn and GiusseppeJacopini (1966) proposed selection and iteration as • control statements to describe how to construct structured flow charts. • C. Böhm, G. Jacopini, "Flow diagrams, Turing Machines and Languages with only Two Formation Rules", Comm. of the ACM, 9(5): 366-371,1966 • http://www.cs.unibo.it/~martini/PP/bohm-jac.pdf • EdsgerDijkstra (1970) proposed structured programming using three control • structures: sequential execution, selection, and iteration. • http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF

  3. COP4020 Programming Languages Control structures Sequential execution: S1:S2; S3; … ; Sn. Selection: (ADA) if x > y then S1; S2; else S3; S4; endif

  4. COP4020 Programming Languages Control structures Multiple Selection: (ALGOL, ADA, C, Java, C++) switch (expression) { case constant_exp_1: statement_1; case constant_exp_2: statement_2; . . . caseconstant_exp_n:statement_n; [default: constant_exp_n+1: statement_n;]

  5. COP4020 Programming Languages Control structures Example in ALGOL W case (expression) is when c hoice_list statement_sequence; when c hoice_list statement_sequence; . . . when c hoice_list statement_sequence; [whenothers c hoice_list statement_sequence; endcase

  6. COP4020 Programming Languages Control structures Iteration statements (loop) Fortran Do 20 count = 1, 10 S1. S2. 20 continue ADA For count in 1 .. 10 loop S1; S2; endloop

  7. COP4020 Programming Languages Control structures Iteration statements (loop) while while (expression) Loop body; Repeat Do S1; S2; until (expression);

  8. COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) In the paper Guarded Commands, Nondeterminacy, and Formal Derivation of programs, Dijkstra proposed new and quite different forms of selection and loop control structures. <guarded command> ::= <guard>  <guard list> <guard> ::= <booleanexpession> <guarded list> ::= <statement> {; <statement} <guarded command set> ::= <guarded command> {▯ <guarded command>} <alternative construct> ::= if <guarded command set> fi<repetitive construct > ::= do <guarded command set> od<statement> ::= <alternative construct> ∣ <repetitive construct> ∣"other statements".

  9. COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The alternative command If <boolean expression>  <statement> ▯ <boolean expression>  <statement> ▯ … ▯ <boolean expression>  <statement> fi Semantics All the boolean expressions are evaluated each time the construct is reached during execution. If more than one of the boolean expressions or guards are true, one of the corresponding statements is chosen at random for execution. If none of the guards is true, a run time error occurs that causes program termination.

  10. COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The alternative command (Example) If i = 0  sum := sum + i ▯ i > j  sum := sum + j ▯ j > i  sum := sum + j fi if i = 0 an d j > i the construct chooses nondeterministically between the first and the last statements If i = j and i != 0 a run time error occurs. Example 2: (find the largest of two numbers) If x ≥ y  max := x ▯ y ≥ x  max := y fi The orden of execution is irrelevant.

  11. COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The iterative command do <boolean expression>  <statement> ▯ <boolean expression>  <statement> ▯ … ▯ <boolean expression>  <statement> od Semantics All the boolean expressions are evaluated each iteration. If more than one of the boolean expressions or guards are true, one of the corresponding statements is nonderterministically chosen for execution. Once the statements are executed the guards are evaluated again. If all the guards is false, the loop terminates.

  12. COP4020 Programming Languages Guarded commands and nondeterministic control structures (Dijkstra 1975) The iterative command (Example: sorting) q1 , q2 , q3 , q4 := Q1, Q2, Q3, Q4; do q1 > q2 T:= q1; q1:= q2; q2:= T; ▯ q2 > q3  T:= q2; q2:= q3; q3:= T; ▯ q3 > q4  T:= q3; q3:= q4; q4:= T; od. Initial values Q1, Q2, Q3, Q4 are asiggned simultaneously to variables q1 , q2 , q3 , q4 .

  13. COP4020 Programming Languages The End

More Related