1 / 51

CS 363 Comparative Programming Languages

CS 363 Comparative Programming Languages. Semantics. Semantics. Semantics - the meaning of the expressions, statements, and program units. Attribute Grammars (AGs) (Knuth, 1968). CFGs cannot describe all of the syntax of programming languages Example: correct type usage, scope, …

loe
Télécharger la présentation

CS 363 Comparative Programming Languages

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. CS 363 Comparative Programming Languages Semantics

  2. Semantics • Semantics - the meaning of the expressions, statements, and program units

  3. Attribute Grammars (AGs) (Knuth, 1968) • CFGs cannot describe all of the syntax of programming languages • Example: correct type usage, scope, … • Additions to CFGs to carry semantic info along through parse trees – tie semantics to syntax • Primary value of AGs: • Static semantics specification • Compiler design (static semantics checking)

  4. Attribute Grammars • Def: An attribute grammar is a CFG G = (S, N, T, P) with the following additions: • For each grammar symbol x there is a set A(x) of attribute values • Each production has a set of functions that define certain attributes of the nonterminals in the rule

  5. Expression Grammar

  6. Annotating the parse tree E Val = 10 E + T Val = 6 Val = 4 T F Val = 6 Val = 4 T * F Num = 4 Val = 2 Val = 3 Ident = lookup(x) = 3 F Val = 2 Num = 2 Input: 2 * x + 4

  7. Annotating the parse tree E Val = E + T Val = Val = T T * F Val = Val = Val = Num = 4 F F Val = Val = Num = 3 Num = 2 Input: 2 + 3 * 4

  8. Annotating the parse tree E Val = 14 E + T Val = 12 Val = 2 T T * F Val = 4 Val = 2 Val = 3 Num = 4 F F Val = 2 Val = 3 Num = 3 Num = 2 Input: 2 + 3 * 4

  9. Another Semantics E t = E + T t = t = T F t = t = T * F Num = 4 t = t = Ident = lookup(x) = INT F t = Num = 2 Input: 2 * x + 4

  10. Another Semantics E t = int E + T t = int t = int T F t = int t = int T * F Num = 4 t = int t = int Ident = lookup(x) = INT F t = int Num = 2 Input: 2 * x + 4

  11. Another Semantics E t = E + T t = t = T F t = ERROR!! t = T * F Num = 4 t = int t = string Ident = lookup(x) = string F t = int Num = 2 Input: 2 * x + 4

  12. Synthesized Attributes Synthesized attributes – the value of a synthesized attribute for a node is computed using only information associated with the node’s children (or the lexical analyzer for leaf nodes). Example:

  13. Inherited Attributes Inherited attributes – if an attribute is not synthesized, it is inherited. Example:

  14. Attribute Grammars • How are attribute values computed? • If all attributes are inherited, the tree could be decorated in top-down order. • If all attributes are synthesized, the tree could be decorated in bottom-up order. • In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used.

  15. Semantics There is no single widely acceptable notation or formalism for describing semantics • Operational Semantics • Axiomatic Semantics • Denotational Semantics

  16. Operational Semantics • Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement

  17. Operational Semantics • To use operational semantics for a high-level language, a virtual machine is needed • The process: • Build a translator (translates source code to the machine code of an idealized computer) • Build a simulator for the idealized computer • Evaluation of operational semantics: • Good if used informally (language manuals, etc.) • Extremely complex if used formally (e.g., VDL)

  18. for i = 1, 10 do body end for i = 1; loop: if (i > 10) goto endloop body i= i + 1 goto loop endloop: Example

  19. Axiomatic Semantics • Based on formal logic (predicate calculus) • Original purpose: formal program verification • Approach: Define axioms or inference rules for each statement type in the language (to allow transformations of expressions to other expressions) • The expressions are called assertions

  20. Axiomatic Semantics • An assertion before a statement (a precondition) states the relationships and constraints among variables that are true at that point in execution • An assertion following a statement is a postcondition • A weakest precondition is the least restrictive precondition that will guarantee the postcondition

  21. Axiomatic Semantics • Pre-post form: {P} statement {Q} • An example: a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0}

  22. Axiomatic Semantics • Program proof process: The postcondition for the whole program is the desired result. Work back through the program to the first statement. If the precondition on the first statement is the same as the program spec, the program is correct. • {pre} program {post}

  23. Axiomatic Semantics Axiom for assignment statement (x = E): {Qx->E} x = E {Q} Apply this to: {???} a = b + 1 {a > 1} {a>1ab+1} = {b+1 > 1} = {b > 0} {b > 0} a = b + 1 { a > 1}

  24. Assignment Statements { ?? } c := 5 { } { } b := b + 5 { } { } b := b + 5 { a > b - 5 }

  25. Assignment Statements { ?? } c := 5 { } { } b := b + 5 { } { a > b } b := b + 5 { a > b - 5 }

  26. Sequence of Assignments { ?? } c := 5 { } { } b := b + 5 { a > b } { a > b } b := b + 5 { a > b - 5 }

  27. Sequence of Assignments { ?? } c := 5 { } { a > b + 5 } b := b + 5 { a > b } { a > b } b := b + 5 { a > b - 5 }

  28. Axiomatic Semantics An inference rule for sequences S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is:

  29. Sequence of Assignments { ?? } c := 5 { a > b + 5 } { a > b + 5 } b := b + 5 { a > b } { a > b } b := b + 5 { a > b - 5 } Can apply sequence rule { a > b + 5 } b := b + 5; b := b + 5 { a > b - 5 }

  30. Sequence of Assignments {} c := 5 { a > b + 5 } { a > b + 5} b := b + 5; b := b + 5 { a > b - 5 } Assignment axiom and sequence rule: {a > b + 5} c := 5; b := b + 5; b := b + 5 { a > b - 5 }

  31. { ?? } if (x > 0) y = y – 1 else y = y + 1 {y > 0} Inference Rule: {B and P} S1 {Q}, {~B and P}S2{Q} {P} if B then S1 else S2 {Q} Conditional Statements

  32. {} if (x > 0) y = y – 1 else y = y + 1 {y > 0} Assignment axiom: {(x > 0) and (y > 1)} y = y – 1 {y > 0} {(x <= 0) and (y > -1) y = y + 1 {y > 0} Conditional Statements

  33. Axiomatic Semantics • The Rule of Consequence: Since we know: {(x <= 0) and (y > -1)}y = y + 1 {y > 0}, {y > 1}  {y > -1} we know: {(x <=0) and (y>1)}y = y + 1{y > 0}

  34. {y > 1} if (x > 0) y = y – 1 else y = y + 1 {y > 0} {(x > 0) and (y > 1)} y = y – 1 {y > 0} {(x <= 0) and (y > 1) y = y + 1 {y > 0} Conditional Statements

  35. {B and P} S1 {Q}, {~B and P}  Q {P} if B then S1 {Q} Conditional Semantics • Other conditionals have rules as well:

  36. { P } while y <> x do y = y + 1 end {y = x} What should P be? What happens to P as the loop executes? It seems like P and ~(y<>x) should tell us something. Loop example

  37. Axiomatic Semantics • An inference rule for logical pretest loops {P} while B do S end {Q} the inference rule is: where I is the loop invariant (the inductive hypothesis)

  38. Axiomatic Semantics • Loop invariant I must meet the following conditions: • P => I (the loop invariant must be true initially) • {I} B {I} (evaluation of the loop test must not change the validity of I) • {I and B} S {I} (I is not changed by executing the body of the loop) • (I and (not B)) => Q (if I is true and B is false, Q is implied) • The loop terminates (this can be difficult to prove)

  39. { P } while y <> x do y = y + 1 end {y = x} use I = P = {y <= x} Need to show P => I {I} B {I} {I and B} S {I} (I and (not B)) => Q The loop terminates Loop example

  40. { n >= 0} count = n; fact = 1; while count <> 0 do fact = fact * count count = count – 1 end {fact = n!} I = {fact=(count+1) * …* n AND count >= 0} Need to show P => I {I} B {I} {I and B} S {I} (I and (not B)) => Q The loop terminates Loop example

  41. Axiomatic Semantics • The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. • I must be weak enough to be satisfied prior to the beginning of the loop, but when combined with the loop exit condition, it must be strong enough to force the truth of the postcondition • Can be tricky – not automatically derivable from the loop

  42. Semantics • Evaluation of axiomatic semantics: • Developing axioms or inference rules for all of the statements in a language is difficult • It is a good tool for correctness proofs, and an excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers

  43. Semantics • Denotational Semantics • Based on recursive function theory • The most abstract semantics description method • Originally developed by Scott and Strachey (1970)

  44. Denotational Semantics • The process of building a denotational spec for a language (not necessarily easy): • Define a mathematical object for each language entity • Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects • The meaning of language constructs are defined by only the values of the program's variables

  45. Semantics • The difference between denotational and operational semantics: In operational semantics, the state changes are defined by coded algorithms; in denotational semantics, they are defined by rigorous mathematical functions

  46. Denotational Semantics • The state of a program is the values of all its current variables s = {<i1, v1>, <i2, v2>, …, <in, vn>} • Let VARMAP be a function that, when given a variable name and a state, returns the current value of the variable VARMAP(ij, s) = vj

  47. Semantics • Decimal Numbers • The following denotational semantics description maps decimal numbers as strings of symbols into numeric values

  48. Semantics <dec_num>  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | <dec_num> (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9) Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9 Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>) Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1 … Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9

  49. Semantics • Expressions • Map expressions onto Z  {error} • We assume expressions are decimal numbers, variables, or binary expressions having one arithmetic operator and two operands, each of which can be an expression

  50. Semantics • The meaning of the loop is the value of the program variables after the statements in the loop have been executed the prescribed number of times, assuming there have been no errors • In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions • Recursion, when compared to iteration, is easier to describe with mathematical rigor

More Related