[previous] [up] [next]     [index]
Next: The Meaning of Scheme Up: Intermezzo 1 Syntax and Semantics Previous: The Scheme Vocabulary

The Scheme Grammar

external

In contrast to many other programming languages, Scheme has a simple grammar. It is shown in its entirety in figure [cross-reference].[footnote] 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.


tabular7062

Figure: Beginning Student Scheme: The core grammar


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.


external

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.


Exercises

Exercise 8.2.1

Why are the sentences

tabular7121


syntactically legal expressions?

Explain why the following sentences are illegal expressions:

tabular7133

Exercise 8.2.2

Why are the sentences

tabular7147


syntactically legal definitions?

Explain why the following sentences are illegal definitions:

tabular7163

Exercise 8.2.3

Distinguish syntactically legal from illegal sentences:

tabular7186


Explain why the sentences are legal or illegal. Solution

Exercise 8.2.4

Distinguish syntactically legal from illegal sentences:

tabular7204


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> ...)

Figure: Syntactic naming conventions


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 [cross-reference] provides a summary of the conventions.


[previous] [up] [next]     [index]
Next: The Meaning of Scheme Up: Intermezzo 1 Syntax and Semantics Previous: The Scheme Vocabulary

PLT