Download presentation

Presentation is loading. Please wait.

1
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann

2
Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Today’s Topics Questions/comments? Question from last time re: Context-sensitive grammars Syntax & Semantics –Attribute grammars continued –Operational semantics –Axiomatic semantics –Denotational semantics

3
CFG A Context Free Grammar is a four-tuple (T, N, S, P) where –T is the set of terminal symbols –N is the set of non-terminal symbols –S is the start symbol (which is one of the non- terminals) –P is the set of productions of the form: A -> X 1... X M where –A element of N –X i element of N U T, 1 =0 Michael Eckmann - Skidmore College - CS 330 - Fall 2007

4
Syntax The nonterminals are abstractions of language constructs The terminals are tokens and lexemes The productions are used to describe programs, individual statements, expressions etc. Michael Eckmann - Skidmore College - CS 330 - Fall 2007

5
Context-sensitive Grammar A Context-sensitive Grammar is a four-tuple (T, N, S, P) where –T is the set of terminal symbols –N is the set of non-terminal symbols –S is the start symbol (which is one of the non- terminals) –P is the set of productions of the form: –xAy -> xby where A element of N x, b, y element of (N U T)* = 0 or more terminals and non-terminals –another rule with empty string Michael Eckmann - Skidmore College - CS 330 - Fall 2007

6
Syntax While it is true that CFG cannot fully describe programming languages in general, CSG's are generally not useful for program language definitions because –inappropriate –too complicated The other two kinds of formal grammars in the Chomsky hierarchy are –regular grammars (we will learn about these when we learn regular expressions) –unrestricted (any language accepted by a Turing Machine) Michael Eckmann - Skidmore College - CS 330 - Fall 2007

7
Syntax Recognizers and generators are used to define languages. Generators generate valid programs in a language. Recognizers determine whether or not a program is in the language (valid syntactically.) Generators are studied in Chapter 3 (stuff coming up next in this lecture) and recognizers (parsers) in Chapter 4. Michael Eckmann - Skidmore College - CS 330 - Fall 2007

8
Attribute Grammars Michael Eckmann - Skidmore College - CS 330 - Fall 2007 The example on page 138 shows the use of an attribute grammar to enhance the BNF of an assignment statement with rules that specify the allowable types that can be assigned to each other. e.g. A float (real) cannot be assigned to a variable whose type is int. But the opposite is allowed. Also, the example shows how one can determine the resulting type of an expression.

9
Attribute Grammars Michael Eckmann - Skidmore College - CS 330 - Fall 2007 I'm not concerned with us knowing all the ins and outs of attribute grammars, but what I feel is important is the general concepts involved and the intended purpose of them. Attribute grammars are generally not used in practice for a few reasons. Can you guess them?

10
Attribute Grammars Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Attribute grammars are generally not used in practice for a few reasons. Can you guess them? –Size and complexity of the grammar will be high for a typical modern programming language –The many attributes and rules that need to be added cause the grammar to be difficult to read and write, formally –The attribute values during parsing would be costly to evaluate (the way it is described in the text.) So, in practice less formal ways are used to check for “static semantics” at compile-time but the ideas are the same.

11
Practice Problems Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Before moving on to our discussion of formally describing the Semantics of a language, let's take a look at problem 8, 10 and 11 on pages 163-164.

12
Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Now that we know how to specify the syntax of a language, we move to specifying the semantics of a language. The text uses the phrase “dynamic semantics” and semantics interchangeably. Semantics is harder to describe than syntax. Programmers need to know the semantics of the syntax and so do compiler writers.

13
Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Both programmers and compiler writers often rely on English descriptions of the language. Why might you think? Are there any problems with this?

14
Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Our text describes 3 different ways to formally describe the semantics of a programming language. –Operational semantics –Axiomatic semantics –Denotational semantics We will cover these superficially --- just enough to give you an idea of how they work and how they differ from each other.

15
Operational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 First create a converter from the language you wish to describe semantically to some lower level language (e.g. assembly). Then, execute the statements of the lower level language on a real or simulated machine. Examine the behaviour of the statements by looking at the state of the machine before and after. The state of a machine includes the values of CPU registers, values of memory locations, etc. The changes of the state determine the meaning of the language statements.

16
Operational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 The conversions that the converter from the language to a lower level language uses can be used to describe the semantics of the different language constructs to a programmer as is often done in manuals. This way is good if used informally as for programming manuals. However, it is extremely complex, formally.

17
Operational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Example: –To describe a while loop in Java using operational semantics one might do the following: while (expr) { // statements } So, assuming the programmer can understand all the code in the “lower level” language in the operational semantics description of the code, then he/she can understand how the while loop construct works. Operational semantics for a while loop: label: if (expr) { // statements goto label; }

18
Operational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Operational semantics can be summed up as: The statements in one language (the one you're trying to describe semantically) are described in terms of a lower-level programming language. (page 141 in Sebesta). Operational semantics uses lower level programming languages not mathematics. The other two methods (which are about to be described) are more formal and based on logic and mathematics.

19
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Based on formal logic (predicate calculus) –Original purpose was for formal program verification --- that is, to prove the correctness of programs –We 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 (aka predicates)

20
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 An assertion before a statement is a precondition and 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 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Notation for specifying the Axiomatic semantics of a statement: {P} statement {Q} {P} is precondition, {Q} is postcondition Example: a = b + 1 {a > 1} –Read this as a must be greater than one after this statement executes. So, –One possible precondition: {b > 4} –What's the weakest precondition here? In other words “what's the least restrictive precondition that will guarantee that a is > 1 after the statement?”

22
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 For: a = b + 1 {a > 1} –The weakest precondition is: {b > 0} –Because a > 1 implies that b + 1 has to be > 1 which implies that b must be > 0.

23
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Looking at the big picture, we might be able to gleam from the example that to prove program correctness, one might have a post condition for the entire program and then work backwards through the program until we get to the beginning and generate a weakest precondition for the entire program. If that is within the program specifications, then it is correct. What does that mean? What would we do as we went backwards through the program?

24
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 When multiple statements or more complex structures are involved, we need what are called inference rules. For a sequence of statements S1;S2: {P1} S1 {P2} {P2} S2 {P3} The inference rule is: And it is read like: If {P1}S1 {P2} is true and {P2} S2 {P3} is true, then we infer that {P1} S1,S2 {P3} is true.

25
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 It gets more complex to determine the precondition for while loops and other structures that might iterate different numbers of times depending on values of variables.

26
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Developing axioms or inference rules for all of the statements in a language is difficult It is a good tool for correctness proofs, but there are limits to this (or any) approach that tries to prove correctness of an arbitrary program. It is an excellent framework for reasoning about programs It is not very useful for language users and compiler writers

27
Axiomatic Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Before moving on to Denotational Semantics let's try problem 20 a) on page 165

28
Denotational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 This is the most widely known and rigorous method for describing the semantics (meaning) of programs. The book only touches on how it works and we will do the same.

29
Denotational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 The process of building a denotational specification 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

30
Denotational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Example: –The following (next slide) denotational semantics description maps decimal numbers as strings of symbols into numeric values –M dec is the semantic function and it is used to map the syntactic objects to the decimal numbers (their meaning)

31
Denotational Semantics Michael Eckmann - Skidmore College - CS 330 - Fall 2007 '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') M dec - a function that maps from the syntactic elements to their meaning M dec ('0') = 0, M dec ('1') = 1, …, M dec ('9') = 9 M dec ( '0') = 10 * M dec ( ) M dec ( '1’) = 10 * M dec ( ) + 1 … M dec ( '9') = 10 * M dec ( ) + 9

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google