The Scheme Grammar

In contrast to many other programming languages, Scheme has a simple grammar. It is shown in its entirety in figure~#figsynbeginner#7403>. The grammar defines two categories of sentences: Scheme definitions, <#70845#><#61707#>;SPMlt;<#7405#>def<#7405#>;SPMgt;<#61707#><#70845#>, and expressions, <#70846#><#61708#>;SPMlt;<#7406#>exp<#7406#>;SPMgt;<#61708#><#70846#>. 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.

#tabular7413#

<#61732#>Figure: <#7444#>Beginning Student Scheme<#7444#>: The core grammar<#61732#>


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 <#61733#><#7446#>define<#7446#><#61733#>, 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 <#61734#><#7447#>define<#7447#><#61734#> distinguishes definitions from expressions. The category of expressions consists of six alternatives: variables, constants, primitive applications, (function) applications, and two varieties of <#61735#><#7448#>cond<#7448#><#61735#>itionals. The last four are again composed of other expressions. The keyword <#61736#><#7449#>cond<#7449#><#61736#> distinguishes conditional expressions from primitive and function applications. external ~<#7451#>Yes, grammars are similar to data definitions. We could use the language of grammars to define data. We don't, because that may confuse students.<#7451#> Here are three examples of expressions: <#61737#><#7452#>'<#7452#><#7453#>all<#7453#><#61737#>, <#61738#><#7454#>x<#7454#><#61738#>, and <#61739#><#7455#>(x<#7455#>\ <#7456#>x)<#7456#><#61739#>. 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 <#61740#><#7457#>x<#7457#><#61740#> is a variable. In contrast, the following parenthesized sentences are not expressions: <#61741#><#7458#>(f<#7458#>\ <#7459#>define)<#7459#><#61741#>, <#61742#><#7460#>(cond<#7460#>\ <#7461#>x)<#7461#><#61742#>, and <#61743#><#7462#>()<#7462#><#61743#>. The first one partially matches the shape of a function application but it uses <#61744#><#7463#>define<#7463#><#61744#> as if it were a variable. The second one fails to be a correct <#61745#><#7464#>cond<#7464#>-expression<#61745#> 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 all left parenthesis are followed by something other than a right parenthesis.
<#7467#>Exercise 8.2.1<#7467#> Why are the sentences

#tabular7469#


syntactically legal expressions? Explain why the following sentences are illegal expressions:

#tabular7481#

<#7494#>Exercise 8.2.2<#7494#> Why are the sentences

#tabular7496#


syntactically legal definitions? Explain why the following sentences are illegal definitions:

#tabular7512#

<#7534#>Exercise 8.2.3<#7534#> Distinguish syntactically legal from illegal sentences:

#tabular7536#


Explain why the sentences are legal or illegal.~ external Solution<#61763#><#61763#> <#7553#>Exercise 8.2.4<#7553#> Distinguish syntactically legal from illegal sentences:

#tabular7555#


Explain why the sentences are legal definitions or why they fail to be legal definitions.~ external Solution<#61767#><#61767#>

<#7582#>Grammatical Terminology<#7582#>:\ 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 <#61768#><#7583#>HEADER<#7583#><#61768#>. Accordingly, the expression component of a definition is called <#61769#><#7584#>BODY<#7584#><#61769#>. The variables that follow the first variable in a header are the <#61770#><#7585#>PARAMETERS<#7585#><#61770#> of a function.~<#61771#><#61771#>


(<#7589#>define<#7589#> (<#70868#><#61772#>;SPMlt;<#7590#>function-name<#7590#>;SPMgt;<#61772#><#70868#> <#70869#><#61773#>;SPMlt;<#7591#>parameter<#7591#>;SPMgt;<#61773#><#70869#> ...<#70870#><#61774#>;SPMlt;<#7592#>parameter<#7592#>;SPMgt;<#61774#><#70870#>) <#70871#><#61775#>;SPMlt;<#7593#>body<#7593#>;SPMgt;<#61775#><#70871#>)
(<#70872#><#61776#>;SPMlt;<#7594#>function<#7594#>;SPMgt;<#61776#><#70872#> <#70873#><#61777#>;SPMlt;<#7595#>argument<#7595#>;SPMgt;<#61777#><#70873#> ...<#70874#><#61778#>;SPMlt;<#7596#>argument<#7596#>;SPMgt;<#61778#><#70874#>)
(<#7597#>cond<#7597#> (<#70875#><#61779#>;SPMlt;<#7598#>question<#7598#>;SPMgt;<#61779#><#70875#> <#70876#><#61780#>;SPMlt;<#7599#>answer<#7599#>;SPMgt;<#61780#><#70876#>) <#72179#><#70877#>;SPMlt;<#61781#><#7600#>cond<#7600#>-clause<#61781#>;SPMgt;<#70877#><#72179#> ...)
<#7602#>Figure: Syntactic naming conventions<#7602#>
People who think of definition as the definition of a mathematical function also use the terminology <#7604#>left-hand side<#7604#> for a definition's header and <#7605#>right-hand side<#7605#> for the body. For the same reason, the first component in an application is called <#7606#>function<#7606#> and the remaining components are referred to as <#7607#>arguments<#7607#>. Occasionally, we also use <#7608#>actual arguments<#7608#>. Finally, a <#61782#><#7609#>cond<#7609#>-expression<#61782#> consists of <#61783#><#7610#>cond<#7610#><#61783#>-lines or <#61784#><#7611#>cond<#7611#><#61784#>-clauses. Each line consists of two expressions: the <#7612#>question<#7612#> and the <#7613#>answer<#7613#>. A question is also called a <#7614#>condition<#7614#>. Figure~#figterminology#7615> provides a summary of the conventions.