|
@@ -26,7 +26,7 @@
|
|
|
|
|
|
\def\racketEd{0}
|
|
|
\def\pythonEd{1}
|
|
|
-\def\edition{0}
|
|
|
+\def\edition{1}
|
|
|
|
|
|
% material that is specific to the Racket edition of the book
|
|
|
\newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
|
|
@@ -134,7 +134,7 @@ Cambridge, Massachusetts\\
|
|
|
London, England}
|
|
|
|
|
|
\begin{copyrightpage}
|
|
|
- \textcopyright\ 2023 Massachusetts Institute of Technology \\[2ex]
|
|
|
+ \textcopyright\ 2023 Jeremy G. Siek \\[2ex]
|
|
|
This work is subject to a Creative Commons CC-BY-ND-NC license. \\[2ex]
|
|
|
Subject to such license, all rights are reserved. \\[2ex]
|
|
|
\includegraphics{CCBY-logo}
|
|
@@ -259,13 +259,13 @@ concepts and algorithms used in compilers.
|
|
|
the fundamental tools of compiler construction: \emph{abstract
|
|
|
syntax trees} and \emph{recursive functions}.
|
|
|
{\if\edition\pythonEd\pythonColor
|
|
|
-\item In Chapter~\ref{ch:parsing} we learn how to use the Lark
|
|
|
+\item In chapter~\ref{ch:parsing} we learn how to use the Lark
|
|
|
parser framework to create a parser for the language of integer
|
|
|
arithmetic and local variables. We learn about the parsing
|
|
|
algorithms inside Lark, including Earley and LALR(1).
|
|
|
%
|
|
|
\fi}
|
|
|
-\item In Chapter~\ref{ch:register-allocation-Lvar} we apply
|
|
|
+\item In chapter~\ref{ch:register-allocation-Lvar} we apply
|
|
|
\emph{graph coloring} to assign variables to machine registers.
|
|
|
\item Chapter~\ref{ch:Lif} adds conditional expressions, which
|
|
|
motivates an elegant recursive algorithm for translating them into
|
|
@@ -542,7 +542,7 @@ feature to represent ASTs (section~\ref{sec:ast}).}
|
|
|
%
|
|
|
\python{We use Python classes and objects to represent ASTs, especially the
|
|
|
classes defined in the standard \code{ast} module for the Python
|
|
|
- source language.}%
|
|
|
+ source language.}
|
|
|
%
|
|
|
We use grammars to define the abstract syntax of programming languages
|
|
|
(section~\ref{sec:grammar}) and pattern matching to inspect individual
|
|
@@ -924,7 +924,7 @@ and a statement that evaluates an expression but ignores the result.
|
|
|
The last grammar rule for \LangInt{} states that there is a
|
|
|
\code{Program} node to mark the top of the whole program:
|
|
|
\[
|
|
|
- \LangInt{} ::= \PROGRAM{\code{'()}}{\Exp}
|
|
|
+ \LangInt{} ::= \PROGRAM{\code{\textquotesingle()}}{\Exp}
|
|
|
\]
|
|
|
The \code{Program} structure is defined as follows:
|
|
|
\begin{lstlisting}
|
|
@@ -941,10 +941,10 @@ The last grammar rule for \LangInt{} states that there is a
|
|
|
\[
|
|
|
\LangInt{} ::= \PROGRAM{}{\Stmt^{*}}
|
|
|
\]
|
|
|
-The asterisk symbol $*$ indicates a list of the preceding grammar item, in
|
|
|
-this case, a list of statements.
|
|
|
+The asterisk $*$ indicates a list of the preceding grammar item, in
|
|
|
+this case a list of statements.
|
|
|
%
|
|
|
-The \code{Module} class is defined as follows
|
|
|
+The \code{Module} class is defined as follows:
|
|
|
\begin{lstlisting}
|
|
|
class Module:
|
|
|
def __init__(self, body):
|
|
@@ -1125,15 +1125,15 @@ pattern variables can be used in the scope of the body, such as
|
|
|
%
|
|
|
{\if\edition\pythonEd\pythonColor
|
|
|
%
|
|
|
-In the above example, the \texttt{match} form checks whether the AST
|
|
|
+In the example above, the \texttt{match} form checks whether the AST
|
|
|
\eqref{eq:arith-prog} is a binary operator and binds its parts to the
|
|
|
-three pattern variables \texttt{child1}, \texttt{op}, and
|
|
|
-\texttt{child2}, and then prints out the operator. In general, each
|
|
|
-\code{case} consists of a \emph{pattern} and a
|
|
|
-\emph{body}.\index{subject}{pattern} Patterns are recursively defined
|
|
|
-to be either a pattern variable, a class name followed by a pattern
|
|
|
-for each of its constructor's arguments, or other
|
|
|
-literals\index{subject}{literals} such as strings, lists, etc.
|
|
|
+three pattern variables (\texttt{child1}, \texttt{op}, and
|
|
|
+\texttt{child2}). In general, each \code{case} consists of a
|
|
|
+\emph{pattern} and a \emph{body}.\index{subject}{pattern} Patterns are
|
|
|
+recursively defined to be one of the following: a pattern variable, a
|
|
|
+class name followed by a pattern for each of its constructor's
|
|
|
+arguments, or other literals\index{subject}{literals} such as strings
|
|
|
+or lists.
|
|
|
%
|
|
|
The body of each \code{case} may contain arbitrary Python code. The
|
|
|
pattern variables can be used in the body, such as \code{op} in
|
|
@@ -1205,6 +1205,7 @@ print(leaf(Constant(8)))
|
|
|
{\if\edition\pythonEd\pythonColor
|
|
|
\begin{lstlisting}
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -1240,7 +1241,7 @@ nonterminal has four alternatives, so the \code{match} has four
|
|
|
grammar rule. For example, the pattern \ADDP{\code{e1}}{\code{e2}}
|
|
|
corresponds to the right-hand side $\ADD{\Exp}{\Exp}$. When
|
|
|
translating from grammars to patterns, replace nonterminals such as
|
|
|
-$\Exp$ with pattern variables of your choice (for example, \code{e1} and
|
|
|
+$\Exp$ with pattern variables of your choice (such as \code{e1} and
|
|
|
\code{e2}).
|
|
|
|
|
|
|
|
@@ -1264,7 +1265,7 @@ node.\footnote{This principle of structuring code according to the
|
|
|
Programs} by \citet{Felleisen:2001aa}.} \python{We define a
|
|
|
second function, named \code{stmt}, that recognizes whether a value
|
|
|
is a \LangInt{} statement.} \python{Finally, }
|
|
|
-Figure~\ref{fig:exp-predicate} \racket{also} contains the definition of
|
|
|
+figure~\ref{fig:exp-predicate} \racket{also} contains the definition of
|
|
|
\code{is\_Lint}, which determines whether an AST is a program in \LangInt{}.
|
|
|
In general, we can write one recursive function to handle each
|
|
|
nonterminal in a grammar.\index{subject}{structural recursion} Of the
|
|
@@ -1379,7 +1380,7 @@ programming language.
|
|
|
reference manual~\citep{plt-tr}.}
|
|
|
%
|
|
|
\python{For example, the Python language is defined in the Python
|
|
|
- Language Reference~\citep{PSF21:python_ref} and the CPython interpreter~\citep{PSF21:cpython}.}
|
|
|
+ language reference~\citep{PSF21:python_ref} and the CPython interpreter~\citep{PSF21:cpython}.}
|
|
|
%
|
|
|
In this book we use interpreters to specify each language that we
|
|
|
consider. An interpreter that is designated as the definition of a
|
|
@@ -1399,7 +1400,7 @@ figure~\ref{fig:interp_Lint}.
|
|
|
\python{The body of the function matches on the \code{Module} AST node
|
|
|
and then invokes \code{interp\_stmt} on each statement in the
|
|
|
module. The \code{interp\_stmt} function includes a case for each
|
|
|
- grammar rule of the \Stmt{} nonterminal and it calls
|
|
|
+ grammar rule of the \Stmt{} nonterminal, and it calls
|
|
|
\code{interp\_exp} on each subexpression. The \code{interp\_exp}
|
|
|
function includes a case for each grammar rule of the \Exp{}
|
|
|
nonterminal. We use several auxiliary functions such as \code{add64}
|
|
@@ -13880,10 +13881,9 @@ Figure~\ref{fig:Lvecof-concrete-syntax} presents the definition of the
|
|
|
concrete syntax for \LangArray{}, and figure~\ref{fig:Lvecof-syntax}
|
|
|
presents the definition of the abstract syntax, extending \LangVec{}
|
|
|
with the \racket{\code{Vectorof}}\python{\code{list}} type and the
|
|
|
-%
|
|
|
\racket{\code{make-vector} primitive operator for creating an array,
|
|
|
whose arguments are the length of the array and an initial value for
|
|
|
-all the elements in the array.}
|
|
|
+all the elements in the array.}%
|
|
|
\python{bracket notation for creating an array literal.}
|
|
|
\racket{The \code{vector-length},
|
|
|
\code{vector-ref}, and \code{vector-ref!} operators that we defined
|