In contrast to many other programming languages, Scheme has a simple
grammar. It is shown in its entirety in
figure
.
The grammar defines two categories of
sentences: Scheme definitions, <def>, and expressions, <exp>. While
the grammar does not dictate the use of white space between the items of
sentences, we follow the convention to put at least one blank space behind
each item unless an item is followed by a right parenthesis ``)''. Scheme is
flexible concerning blank space, and we can replace a single blank space by
many spaces, line breaks, and page breaks.
The two grammar definitions describe how to form atomic sentences and compound sentences, which are sentences built from other sentences. For example, a function definition is formed by using ``('', followed by the keyword define, followed by another ``('', followed by a non-empty sequence of variables, followed by ``)'', followed by an expression, and closed by a right parenthesis ``)'' that matches the very first one. The keyword define distinguishes definitions from expressions.
The category of expressions consists of six alternatives: variables, constants, primitive applications, (function) applications, and two varieties of conditionals. The last four are again composed of other expressions. The keyword cond distinguishes conditional expressions from primitive and function applications.
Here are three examples of expressions: 'all, x, and (x x). The first one belongs to the class of symbols and is therefore an expression. The second is a variable, and every variable is an expression. Finally, the third is a function application, because x is a variable.
In contrast, the following parenthesized sentences are not expressions: (f define), (cond x), and (). The first one partially matches the shape of a function application but it uses define as if it were a variable. The second one fails to be a correct cond-expression because it contains a variable as the second item and not a pair of expressions surrounded by parentheses. The last one is just a pair of parentheses, but the grammar requires that every left parenthesis is followed by something other than a right parenthesis.
Exercise 8.2.1
syntactically legal expressions?
Explain why the following sentences are illegal expressions:
Exercise 8.2.2
syntactically legal definitions?
Explain why the following sentences are illegal definitions:
Exercise 8.2.3
Distinguish syntactically legal from illegal sentences:
Explain why the sentences are legal or illegal. Solution
Exercise 8.2.4
Distinguish syntactically legal from illegal sentences:
Explain why the sentences are legal definitions or why they
fail to be legal definitions. Solution
Grammatical Terminology: The components of compound sentences have names. We have introduced some of these names on an informal basis; for better communication, we introduce all useful ones here. The second component of a definition, that is, the non-empty sequence of variables, is a HEADER. Accordingly, the expression component of a definition is called BODY. The variables that follow the first variable in a header are the PARAMETERS of a function.
(define (<function-name> <parameter> ...<parameter>) <body>)
(<function> <argument> ...<argument>)
(cond (<question> <answer>) <cond-clause> ...)
People who think of definition as the definition of a mathematical function also use the terminology LEFT-HAND SIDE for a definition's header and RIGHT-HAND SIDE for the body. For the same reason, the first component in an application is called FUNCTION and the remaining components are referred to as ARGUMENTS. Occasionally, we also use ACTUAL ARGUMENTS.
Finally, a cond-expression consists of cond-lines or cond-clauses. Each line consists of two expressions: the QUESTION and the ANSWER. A question is also called a CONDITION.
Figure
provides a summary of the conventions.