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 ﬂy 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. signedint = unsignedint signedint = + unsignedint signedint = − unsignedint unsignedint = digit digit = 0 .. 9 unsignedint = digit unsignedint 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 signedint and successively substitute the lefthand side of a rule with its righthand side. signedint = unsignedint = digit =0 Thus 0 is a integer constant. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 6 signedint = + unsignedint = + digit unsignedint = +3 unsignedint = +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 unsignedint + unsignedint signedint CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 8 Spelling rules for Beginning Student
Identiﬁers are the names of constants, parameters, and userdeﬁned functions. They are made up of letters, numbers, hyphens, underscores, and a few other punctuation marks. They must contain at least one nonnumber. They can’t contain spaces or any of these: ( ) , ; { } [ ] ‘ ’ “ ”. Symbols start with a single quote ’ followed by something obeying the rules for identiﬁers. 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 builtin 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. unsignedint = digit unsignedint = digit unsignedint
This idea is so common that we will abbreviate it: unsignedint = 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 deﬁnitions and expressions. program = deforexpr . . . deforexpr = deﬁnition deforexpr = expr The Help Desk uses another abbreviation, a vertical bar denoting ‘or’, to join multiple rules with the same lefthand side. deforexpr = deﬁnition  expr CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 12 Grammar rules for Beginning Student
deﬁnition = (deﬁne ( id id id . . . ) expr )
This rule shows how to form function deﬁnitions out of identiﬁers (already discussed) and expressions (not yet discussed). We need a separate rule for deﬁning constants. deﬁnition = (deﬁne 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 userdeﬁned 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 myvar, ’blue, and 42 are expressions. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 15 expr = ( primop expr expr . . . ) expr = ( id expr expr . . . )
expressions. These rules show that (+ 4 myvar) and (myfn 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 ﬁnished the grammar at this point, but we have enough to start deﬁning 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 ﬂies 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 deﬁnitions and expressions. More precisely, a program is a sequence of strings, each satisfying the deﬁnition of deﬁnition or expr. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 20 Our model will involve simpliﬁcation 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 deﬁnitions and values. A substitution step ﬁnds 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 builtin 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 builtin function and v the value of f (v1 , . . . , vn ).
CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 22 Application of userdeﬁned functions
As an example, consider (deﬁne (term x y) (∗ x (sqr y))). The function application (term 2 3) can be evaluated by taking the body of the function deﬁnition 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 ﬁrst be simpliﬁed 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 (deﬁne (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
(deﬁne (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 deﬁnitions is: deﬁnition = (deﬁne id expr )
A constant deﬁnition binds a name (the constant) to a value (the value of the expression). We add the substitution rule: id ⇒ val, where (deﬁne id val) occurs to the left. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 26 An example
(deﬁne x 3) (deﬁne y (+ x 1)) y ⇒ ⇒ ⇒ (deﬁne x 3) (deﬁne y (+ 3 1)) y (deﬁne x 3) (deﬁne y 4) y (deﬁne x 3) (deﬁne 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 ﬁrst 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 sufﬁce to simplify any cond expression. CS 135 Fall 2009 03: The syntax and semantics of Beginning Student 28 An example
(deﬁne 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 undeﬁned
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 runtime 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 simpliﬁcation 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 stepbystep 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 ﬁll 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 substitutionbased 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
 Fall '07
 VASIGA
 Grammar, Beginning Student

Click to edit the document details