03-synsem-post3up

03-synsem-post3up - The syntax and semantics of Beginning...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The syntax and semantics of Beginning Student Readings: HtDP, Intermezzo 1 (Section 8). We are covering the ideas of section 8, but not the parts of it dealing with section 6/7 material (which will come later), and in a somewhat different fashion. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 1 • A program has a precise meaning and effect. • A model of a programming language provides a way of describing the meaning of a program. • Typically this is done informally, by examples. • With Scheme, we can do better. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 2 Advantages in modelling Scheme • Few language constructs, so model description is short • We don’t need anything more than the language itself! – No diagrams – No vague descriptions of the underlying machine CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 3 Syntax and grammar Syntax: the way we’re allowed to say things. Semantics: the meaning of what we can say. Example: the English sentence ‘Trombones fly hungrily.’ English grammatical rules are not rigidly enforced. The rules for computer languages are rigidly enforced. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 4 Here is a grammar describing integer constants. ￿signed-int￿ = ￿unsigned-int￿ ￿signed-int￿ = + ￿unsigned-int￿ ￿signed-int￿ = − ￿unsigned-int￿ ￿unsigned-int￿ = ￿digit￿ ￿digit￿ = 0 .. 9 ￿unsigned-int￿ = ￿digit￿ ￿unsigned-int￿ CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 5 Generation from a grammar We can use the grammar to generate integer constants. We start with ￿signed-int￿ and successively substitute the left-hand side of a rule with its right-hand side. ￿signed-int￿ = ￿unsigned-int￿ = ￿digit￿ =0 Thus 0 is a integer constant. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 6 ￿signed-int￿ = + ￿unsigned-int￿ = + ￿digit￿ ￿unsigned-int￿ = +3 ￿unsigned-int￿ = +3 ￿digit￿ = +37 In this fashion, we can generate any integer constant. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 7 Parsing using a grammar The reverse process, called parsing, is of greater interest. +37 + ￿digit￿ 7 + ￿digit￿ ￿digit￿ + ￿digit￿ ￿unsigned-int￿ + ￿unsigned-int￿ ￿signed-int￿ CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 8 Spelling rules for Beginning Student Identifiers are the names of constants, parameters, and user-defined functions. They are made up of letters, numbers, hyphens, underscores, and a few other punctuation marks. They must contain at least one non-number. They can’t contain spaces or any of these: ( ) , ; { } [ ] ‘ ’ “ ”. Symbols start with a single quote ’ followed by something obeying the rules for identifiers. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 9 There are rules for numbers (integers, rationals, decimals) which are fairly intuitive. There are some built-in constants, like true and false. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 10 We saw how to describe a sequence of digits with two rules. ￿unsigned-int￿ = ￿digit￿ ￿unsigned-int￿ = ￿digit￿ ￿unsigned-int￿ This idea is so common that we will abbreviate it: ￿unsigned-int￿ = ￿digit￿ ￿digit￿ . . . Intuitively, the dots mean ‘zero or more repetitions’ of what they follow. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 11 A program is a sequence of definitions and expressions. ￿program￿ = ￿def-or-expr￿ . . . ￿def-or-expr￿ = ￿definition￿ ￿def-or-expr￿ = ￿expr￿ The Help Desk uses another abbreviation, a vertical bar denoting ‘or’, to join multiple rules with the same left-hand side. ￿def-or-expr￿ = ￿definition￿ | ￿expr￿ CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 12 Grammar rules for Beginning Student ￿definition￿ = (define ( ￿id￿ ￿id￿ ￿id￿ . . . ) ￿expr￿ ) This rule shows how to form function definitions out of identifiers (already discussed) and expressions (not yet discussed). We need a separate rule for defining constants. ￿definition￿ = (define ￿id￿ ￿expr￿ ) CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 13 The rules for forming expressions are more complicated. An expression can be (among other things): • a single variable or constant; • an application of a primitive or user-defined function • a cond with or without an else This leads to several grammar rules. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 14 ￿expr￿ = ￿id￿ | ￿number￿ | ￿symbol￿ These rules show that my-var, ’blue, and 42 are expressions. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 15 ￿expr￿ = ( ￿prim-op￿ ￿expr￿ ￿expr￿ . . . ) ￿expr￿ = ( ￿id￿ ￿expr￿ ￿expr￿ . . . ) expressions. These rules show that (+ 4 my-var) and (my-fn 7 8 9) are CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 16 ￿expr￿ = (cond [ ￿expr￿ ￿expr￿ ] . . . ) ￿expr￿ = (cond [ ￿expr￿ ￿expr￿ ] . . . [else ￿expr￿ ]) rules. A legal expression such as (+ 1 (∗ 2 3)) can be parsed using these Illegal expressions such as (6 + 7) and ((+ 3 4)) cannot be parsed. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 17 We have not finished the grammar at this point, but we have enough to start defining the semantics (meaning) of programs that are described by the grammar. Our grammar is closer to that of Help Desk than the textbook. Our semantics will be different from both the textbook and what the Stepper shows you, but all of these give the same end result. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 18 The semantics of Scheme The English sentence “Cans fry cloud” is syntactically correct but has no semantic interpretation. The English sentence “Fruit flies like a banana” is ambiguous; it has more than one semantic interpretation. We must make sure that our Scheme programs are unambiguous (have exactly one interpretation). CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 19 A semantic model A semantic model of a programming language provides a method of predicting the result of running any program. A Scheme program is an interleaved sequence of definitions and expressions. More precisely, a program is a sequence of strings, each satisfying the definition of ￿definition￿ or ￿expr￿. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 20 Our model will involve simplification of the program via substitution, as we have been doing all along. Every substitution step yields a valid Scheme program, until all that remains is a sequence of definitions and values. A substitution step finds the leftmost subexpression eligible for rewriting, and rewrites it by the rules we are about to describe. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 21 Application of built-in functions We reuse the rules for the arithmetic expressions we are familiar with to substitute the appropriate value for expressions like (+ 3 5) and (expt 2 10). ( + 3 5) ⇒ 8 (expt 2 10) ⇒ 1024 Formally, the substitution rule is: (f v1 . . . vn) ⇒ v where f is a built-in function and v the value of f (v1 , . . . , vn ). CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 22 Application of user-defined functions As an example, consider (define (term x y) (∗ x (sqr y))). The function application (term 2 3) can be evaluated by taking the body of the function definition and replacing x by 2 and y by 3. The result is (∗ 2 (sqr 3)). The rule does not apply if an argument is not a value, as in the case of the second argument in (term 2 (+ 1 2)). Any argument which is not a value must first be simplified to a value using the rules for expressions. 23 CS 135 Fall 2009 03: The syntax and semantics of Beginning Student The general substitution rule is: (f v1 . . . vn) ⇒ exp’ where (define (f x1 . . . xn) exp) occurs to the left, and exp’ is obtained by substituting into the expression exp, with all occurrences of the formal parameter xi replaced by the value vi (for i from 1 to n). CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 24 An example (define (term x y) (∗ x (sqr y))) (term (− 3 1) (+ 1 2)) ⇒ (term 2 (+ 1 2)) ⇒ (term 2 3) ⇒ (∗ 2 (sqr 3)) ⇒ (∗ 2 9) ⇒ 18 CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 25 The grammar rule for constant definitions is: ￿definition￿ = (define ￿id￿ ￿expr￿ ) A constant definition binds a name (the constant) to a value (the value of the expression). We add the substitution rule: id ⇒ val, where (define id val) occurs to the left. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 26 An example (define x 3) (define y (+ x 1)) y ⇒ ⇒ ⇒ (define x 3) (define y (+ 3 1)) y (define x 3) (define y 4) y (define x 3) (define y 4) 4 CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 27 Substitution in cond expressions There are three rules: when the first expression is false, when it is true, and when it is else. (cond [false exp] [q a]. . . ) ⇒ (cond [q a]. . . ). (cond [true exp] [q a]. . . ) ⇒ exp. (cond [else exp]) ⇒ exp. These suffice to simplify any cond expression. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 28 An example (define n 5) (cond [(even? n) x][(odd? n) y]) ⇒ (cond [(even? 5) x] [(odd? n) y]) ⇒ (cond [false x][(odd? n) y]) ⇒ (cond [(odd? n) y]) ⇒ (cond [(odd? 5) y]) ⇒ (cond [true y]) ⇒y Error: y undefined CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 29 Errors A syntax error occurs when a sentence cannot be interpreted using the grammar. A run-time error occurs when an expression cannot be reduced to a value by application of our (still incomplete) evaluation rules. Examples: (and 3 4), (cond [(> 3 4) x]) CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 30 Completing the grammar and semantics ￿expr￿ = (and ￿expr￿ . . . ) ￿expr￿ = (or ￿expr￿ . . . ) ￿expr￿ = true ￿expr￿ = false CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 31 The grammar in the book is incomplete, because it only allows two arguments for Boolean functions. The grammar presented here allows multiple arguments. The simplification rules we use are different from the ones the Stepper in DrScheme uses. The end result is the same, but the intermediate steps are different. The implementers of the Stepper made choices which result in more complicated rules, but whose intermediate steps appear to help students in lab situations. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 32 (and true exp2 . . . ) ⇒ (and exp2 . . . ). (and false exp2 . . . ) ⇒ false. (or true exp2 . . . ) ⇒ true. (or false exp2 . . . ) ⇒ (or exp2 . . . ). (and) ⇒ true. (or) ⇒ false. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 33 Importance of the model We can add to the semantic model when we introduce a new feature of Scheme. Understanding the semantic model is very important in understanding the meaning of a Scheme program. Doing a step-by-step reduction according to these rules is called tracing a program. It is an important skill in any programming language or computational system. We will test this skill on assignments and exams. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 34 Unfortunately, once we start dealing with unbounded data, traces get very long, and the intermediate steps are hard to make sense of in the Stepper. In future traces, we will often do a condensed trace by skipping several steps in order to put the important transformations onto one slide. It is very important to know when these gaps occur and to be able to fill in the missing transformations. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 35 Goals of this module You should understand the method of using a grammar to specify the syntax of Scheme. You should be able to show why legal expressions satisfy the grammar we have given, and why illegal expressions do not. You should understand the substitution-based semantic model of Scheme, and be prepared for future extensions. You should be able to trace the series of simplifying transformations of a Scheme program. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 36 ...
View Full Document

This note was uploaded on 02/12/2010 for the course CS 135 taught by Professor Vasiga during the Fall '07 term at Waterloo.

Ask a homework question - tutors are online