The Grammar of Advanced Scheme

Even though Scheme is a full-fledged language with as much power as any other programming language, its designers have kept its grammar simple. The grammar of <#48414#>Advanced Student<#48414#> Scheme, which is most of Scheme, is only a few lines longer than that of <#48415#>Beginning Student<#48415#> Scheme.

#tabular48417#

<#68877#>Figure: <#48467#>Advanced Student<#48467#> Scheme: The core grammar<#68877#>


Figure~#figsynadvanced#48469> contains the essential grammar of <#48470#>Advanced Student<#48470#> Scheme. The extensions of <#48473#>Beginning Student<#48473#> Scheme are four new forms of expressions: <#68879#><#48474#>local<#48474#>-expression<#68879#>s, <#68880#><#48475#>lambda<#48475#>-expression<#68880#>s, <#68881#><#48476#>set!<#48476#>-expression<#68881#>s, and <#68882#><#48477#>begin<#48477#>-expression<#68882#>s. The specification of <#68883#><#48478#>local<#48478#>-expression<#68883#>s refers to the category of definitions, which refers back to the category of expressions. A <#48479#>lambda<#48479#>-expression\ consists of a sequence of variables, enclosed in parentheses, and an expression. Similarly, a <#48480#>set!<#48480#>-expression\ consists of a variable and an expression. Finally, a <#48481#>begin<#48481#>-expression\ is just a sequence of expressions prefixed with the keyword <#68884#><#48482#>begin<#48482#><#68884#> to distinguish it from an application. Since functions are values now, the grammar of <#48483#>Advanced Student<#48483#> Scheme is also simpler than that of <#48484#>Beginning Student<#48484#> Scheme in one regard. Specifically, it merges the lines for primitive and function application into a single line. The new line specifies applications, which are now sequences of expressions enclosed in parentheses. Due to the inclusion of primitive operations into the set of expressions,
<#48489#>(+<#48489#> <#48490#>1<#48490#> <#48491#>2)<#48491#>
is still an expression. After all, <#68885#><#48495#>+<#48495#><#68885#> is now an expression, and so are <#68886#><#48496#>1<#48496#><#68886#> and <#68887#><#48497#>2<#48497#><#68887#>. The application of <#68888#><#48498#>define<#48498#><#68888#>d functions works similarly:
<#48503#>(f<#48503#> <#48504#>1<#48504#> <#48505#>2)<#48505#>
The first expression is a variable, the others are numbers. The application is thus a legal expression. Unfortunately, a language grammar can only specify the large contours of what is a legal sentence construction. It cannot express restrictions that require some knowledge about the context of a phrase. <#48509#>Advanced Student<#48509#> Scheme requires a few such restrictions:
  1. In a <#48511#>lambda<#48511#>-expression\ no variable may occur twice in the parameter sequence.
  2. In a <#48512#>local<#48512#>-expression\ no definition may introduce the same variable as any other definition in the same sequence.
  3. A <#48513#>set!<#48513#>-expression\ must occur in the lexical scope of a <#68889#><#48514#>define<#48514#><#68889#> that introduces the <#48515#>set!<#48515#>-expression's left-hand side.
In addition, the old restriction applies that keywords cannot be used as variables. Consider the following definition, which uses the four new constructs:
<#48521#>(d<#48521#><#48522#>efine<#48522#> <#48523#>switch<#48523#>
  <#48524#>(l<#48524#><#48525#>ocal<#48525#> <#48526#>(<#48526#><#48527#>(define-struct<#48527#> <#48528#>hide<#48528#> <#48529#>(it))<#48529#> 
          <#48530#>(define<#48530#> <#48531#>state<#48531#> <#48532#>(make-hide<#48532#> <#48533#>1)))<#48533#> 
    <#48534#>(l<#48534#><#48535#>ambda<#48535#> <#48536#>()<#48536#> 
      <#48537#>(b<#48537#><#48538#>egin<#48538#> 
        <#48539#>(set!<#48539#> <#48540#>state<#48540#> <#48541#>(make-hide<#48541#> <#48542#>(-<#48542#> <#48543#>1<#48543#> <#48544#>(hide-it<#48544#> <#48545#>state))))<#48545#> 
        <#48546#>state))))<#48546#> 
The definition introduces the variable <#68890#><#48550#>switch<#48550#><#68890#>. The right-hand side of the definition is a <#48551#>local<#48551#>-expression. It in turn defines the structure <#68891#><#48552#>hide<#48552#><#68891#> and the variable <#68892#><#48553#>state<#48553#><#68892#>, which stands for an instance of <#68893#><#48554#>hide<#48554#><#68893#>. The body of the <#48555#>local<#48555#>-expression\ is a <#48556#>lambda<#48556#>-expression, whose parameter sequence is empty. The function's body consists of a <#48557#>begin<#48557#>-expression\ with two expressions: a <#48558#>set!<#48558#>-expression\ and an expression that consists of just the variable <#68894#><#48559#>state<#48559#><#68894#>. All subexpressions in our program satisfy the necessary restrictions. First, the <#48560#>local<#48560#>-expression\ introduces four distinct names: <#68895#><#48561#>make-hide<#48561#><#68895#>, <#68896#><#48562#>hide?<#48562#><#68896#>, <#68897#><#48563#>hide-it<#48563#><#68897#>, and <#68898#><#48564#>state<#48564#><#68898#>. Second, the parameter list of the <#48565#>lambda<#48565#>-expression\ is empty, so there is no possible conflict. Finally, the <#48566#>set!<#48566#>-expression's variable is the <#68899#><#48567#>local<#48567#><#68899#>ly defined variable <#68900#><#48568#>state<#48568#><#68900#>, so it is legal, too.
<#48571#>Exercise 38.2.1<#48571#> Determine whether the following phrases are syntactically legal or illegal programs:
<#48577#>1.<#48577#> <#48578#>(defin<#48578#><#48579#>e<#48579#> <#48580#>(f<#48580#> <#48581#>x)<#48581#> 
         <#48582#>(b<#48582#><#48583#>egin<#48583#> 
           <#48584#>(set!<#48584#> <#48585#>y<#48585#> <#48586#>x)<#48586#> 
           <#48587#>x))<#48587#> 
<#48588#>2.<#48588#> <#48589#>(defin<#48589#><#48590#>e<#48590#> <#48591#>(f<#48591#> <#48592#>x)<#48592#> 
         <#48593#>(b<#48593#><#48594#>egin<#48594#> 
           <#48595#>(set!<#48595#> <#48596#>f<#48596#> <#48597#>x)<#48597#> 
           <#48598#>x))<#48598#> 
<#48599#>3.<#48599#> <#48600#>(local<#48600#> <#48601#>((def<#48601#><#48602#>ine-struct<#48602#> <#48603#>hide<#48603#> <#48604#>(it))<#48604#> 
               <#48605#>(define<#48605#> <#48606#>make-hide<#48606#> <#48607#>10))<#48607#> 
         <#48608#>(hide?<#48608#> <#48609#>10))<#48609#> 
<#48610#>4.<#48610#> <#48611#>(local<#48611#> <#48612#>((def<#48612#><#48613#>ine-struct<#48613#> <#48614#>loc<#48614#> <#48615#>(con))<#48615#> 
               <#48616#>(define<#48616#> <#48617#>loc<#48617#> <#48618#>10))<#48618#> 
         <#48619#>(loc?<#48619#> <#48620#>10))<#48620#> 
<#48621#>5.<#48621#> <#48622#>(def<#48622#><#48623#>in<#48623#><#48624#>e<#48624#> <#48625#>f<#48625#> 
         <#48626#>(l<#48626#><#48627#>ambda<#48627#> <#48628#>(x<#48628#> <#48629#>y<#48629#> <#48630#>x)<#48630#> 
           <#48631#>(*<#48631#> <#48632#>x<#48632#> <#48633#>y<#48633#> <#48634#>z)))<#48634#> 
       
       <#48635#>(define<#48635#> <#48636#>z<#48636#> <#48637#>3.14)<#48637#> 
Explain why a phrase is legal or illegal.~ external Solution<#68901#><#68901#>