Bläddra i källkod

ch 1 copy edits for python

Jeremy G. Siek 2 år sedan
förälder
incheckning
9ef49d94d5
3 ändrade filer med 39 tillägg och 39 borttagningar
  1. 23 23
      book.tex
  2. 15 15
      defs.tex
  3. 1 1
      python.bib

+ 23 - 23
book.tex

@@ -26,7 +26,7 @@
 
 
 \def\racketEd{0}
 \def\racketEd{0}
 \def\pythonEd{1}
 \def\pythonEd{1}
-\def\edition{0}
+\def\edition{1}
 
 
 % material that is specific to the Racket edition of the book
 % material that is specific to the Racket edition of the book
 \newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
 \newcommand{\racket}[1]{{\if\edition\racketEd{#1}\fi}}
@@ -134,7 +134,7 @@ Cambridge, Massachusetts\\
 London, England}
 London, England}
 
 
 \begin{copyrightpage}
 \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]
   This work is subject to a Creative Commons CC-BY-ND-NC license. \\[2ex]
   Subject to such license, all rights are reserved. \\[2ex]
   Subject to such license, all rights are reserved. \\[2ex]
   \includegraphics{CCBY-logo}
   \includegraphics{CCBY-logo}
@@ -259,13 +259,13 @@ concepts and algorithms used in compilers.
   the fundamental tools of compiler construction: \emph{abstract
   the fundamental tools of compiler construction: \emph{abstract
     syntax trees} and \emph{recursive functions}. 
     syntax trees} and \emph{recursive functions}. 
 {\if\edition\pythonEd\pythonColor
 {\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
   parser framework to create a parser for the language of integer
   arithmetic and local variables. We learn about the parsing
   arithmetic and local variables. We learn about the parsing
   algorithms inside Lark, including Earley and LALR(1).
   algorithms inside Lark, including Earley and LALR(1).
 %
 %
 \fi}
 \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.
   \emph{graph coloring} to assign variables to machine registers.
 \item Chapter~\ref{ch:Lif} adds conditional expressions, which
 \item Chapter~\ref{ch:Lif} adds conditional expressions, which
   motivates an elegant recursive algorithm for translating them into
   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
 \python{We use Python classes and objects to represent ASTs, especially the
   classes defined in the standard \code{ast} module for the Python
   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
 We use grammars to define the abstract syntax of programming languages
 (section~\ref{sec:grammar}) and pattern matching to inspect individual
 (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
 The last grammar rule for \LangInt{} states that there is a
 \code{Program} node to mark the top of the whole program:
 \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:
 The \code{Program} structure is defined as follows:
 \begin{lstlisting}
 \begin{lstlisting}
@@ -941,10 +941,10 @@ The last grammar rule for \LangInt{} states that there is a
 \[
 \[
   \LangInt{} ::= \PROGRAM{}{\Stmt^{*}}
   \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}
 \begin{lstlisting}
 class Module:
 class Module:
     def __init__(self, body):
     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
 {\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
 \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
 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
 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
 {\if\edition\pythonEd\pythonColor
 \begin{lstlisting}
 \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}}
 grammar rule. For example, the pattern \ADDP{\code{e1}}{\code{e2}}
 corresponds to the right-hand side $\ADD{\Exp}{\Exp}$. When
 corresponds to the right-hand side $\ADD{\Exp}{\Exp}$. When
 translating from grammars to patterns, replace nonterminals such as
 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}).
 \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
     Programs} by \citet{Felleisen:2001aa}.}  \python{We define a
   second function, named \code{stmt}, that recognizes whether a value
   second function, named \code{stmt}, that recognizes whether a value
   is a \LangInt{} statement.}  \python{Finally, }
   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{}.
 \code{is\_Lint}, which determines whether an AST is a program in \LangInt{}.
 In general, we can write one recursive function to handle each
 In general, we can write one recursive function to handle each
 nonterminal in a grammar.\index{subject}{structural recursion} Of the
 nonterminal in a grammar.\index{subject}{structural recursion} Of the
@@ -1379,7 +1380,7 @@ programming language.
   reference manual~\citep{plt-tr}.}
   reference manual~\citep{plt-tr}.}
 %
 %
 \python{For example, the Python language is defined in the Python
 \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
 In this book we use interpreters to specify each language that we
 consider. An interpreter that is designated as the definition of a
 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
 \python{The body of the function matches on the \code{Module} AST node
   and then invokes \code{interp\_stmt} on each statement in the
   and then invokes \code{interp\_stmt} on each statement in the
   module.  The \code{interp\_stmt} function includes a case for each
   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}
   \code{interp\_exp} on each subexpression.  The \code{interp\_exp}
   function includes a case for each grammar rule of the \Exp{}
   function includes a case for each grammar rule of the \Exp{}
   nonterminal. We use several auxiliary functions such as \code{add64}
   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}
 concrete syntax for \LangArray{}, and figure~\ref{fig:Lvecof-syntax}
 presents the definition of the abstract syntax, extending \LangVec{}
 presents the definition of the abstract syntax, extending \LangVec{}
 with the \racket{\code{Vectorof}}\python{\code{list}} type and the
 with the \racket{\code{Vectorof}}\python{\code{list}} type and the
-%
 \racket{\code{make-vector} primitive operator for creating an array,
 \racket{\code{make-vector} primitive operator for creating an array,
 whose arguments are the length of the array and an initial value for
 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.}
 \python{bracket notation for creating an array literal.}
 \racket{The \code{vector-length},
 \racket{The \code{vector-length},
 \code{vector-ref}, and \code{vector-ref!} operators that we defined
 \code{vector-ref}, and \code{vector-ref!} operators that we defined

+ 15 - 15
defs.tex

@@ -130,15 +130,15 @@
 \newcommand{\MID}{\;\mid\;}
 \newcommand{\MID}{\;\mid\;}
 
 
 \if\edition\racketEd
 \if\edition\racketEd
-\newcommand{\QUOTE}[1]{\code{'}#1}
+\newcommand{\QUOTE}[1]{\code{\textquotesingle}#1}
 \newcommand{\INT}[1]{{\key{(Int}~#1\key{)}}}
 \newcommand{\INT}[1]{{\key{(Int}~#1\key{)}}}
 \newcommand{\READOP}{{\key{read}}}
 \newcommand{\READOP}{{\key{read}}}
-\newcommand{\READ}{{\key{(Prim}~\code{'read}~\key{())}}}
+\newcommand{\READ}{{\key{(Prim}~\code{\textquotesingle read}~\key{())}}}
 \newcommand{\CREAD}{\key{(read)}}
 \newcommand{\CREAD}{\key{(read)}}
-\newcommand{\NEG}[1]{{\key{(Prim}~\code{'-}~\code{(}#1\code{))}}}
-\newcommand{\ADD}[2]{{\key{(Prim}~\code{'+}~\code{(}#1~#2\code{))}}}
-\newcommand{\ADDP}[2]{{\key{(Prim}~\code{'+}~\code{(list}~#1~#2\code{))}}}
-\newcommand{\SUB}[2]{\key{(Prim}~\code{'-}~\code{(}#1~#2\code{))}}
+\newcommand{\NEG}[1]{{\key{(Prim}~\code{\textquotesingle -}~\code{(}#1\code{))}}}
+\newcommand{\ADD}[2]{{\key{(Prim}~\code{\textquotesingle +}~\code{(}#1~#2\code{))}}}
+\newcommand{\ADDP}[2]{{\key{(Prim}~\code{\textquotesingle +}~\code{(list}~#1~#2\code{))}}}
+\newcommand{\SUB}[2]{\key{(Prim}~\code{\textquotesingle -}~\code{(}#1~#2\code{))}}
 \newcommand{\PROGRAM}[2]{\LP\code{Program}~#1~#2\RP}
 \newcommand{\PROGRAM}[2]{\LP\code{Program}~#1~#2\RP}
 \newcommand{\VAR}[1]{\key{(Var}~#1\key{)}}
 \newcommand{\VAR}[1]{\key{(Var}~#1\key{)}}
 \newcommand{\BOOL}[1]{\key{(Bool}~#1\key{)}}
 \newcommand{\BOOL}[1]{\key{(Bool}~#1\key{)}}
@@ -156,8 +156,8 @@
 \newcommand{\ANDNAME}{\key{and}}
 \newcommand{\ANDNAME}{\key{and}}
 \newcommand{\ORNAME}{\key{or}}
 \newcommand{\ORNAME}{\key{or}}
 \newcommand{\NOTNAME}{\key{not}}
 \newcommand{\NOTNAME}{\key{not}}
-\newcommand{\AND}[2]{\key{(Prim}~\code{'and}~\code{(}#1~#2\code{))}}
-\newcommand{\OR}[2]{\key{(Prim}~\code{'or}~\code{(}#1~#2\code{))}}
+\newcommand{\AND}[2]{\key{(Prim}~\code{\textquotesingle and}~\code{(}#1~#2\code{))}}
+\newcommand{\OR}[2]{\key{(Prim}~\code{\textquotesingle or}~\code{(}#1~#2\code{))}}
 \newcommand{\CAND}[2]{\CBINOP{\key{and}}{#1}{#2}}
 \newcommand{\CAND}[2]{\CBINOP{\key{and}}{#1}{#2}}
 \newcommand{\COR}[2]{\CBINOP{\key{or}}{#1}{#2}}
 \newcommand{\COR}[2]{\CBINOP{\key{or}}{#1}{#2}}
 \newcommand{\INTTY}{{\key{Integer}}}
 \newcommand{\INTTY}{{\key{Integer}}}
@@ -188,7 +188,7 @@
 \newcommand{\CGLOBAL}[1]{#1\key{(\%rip)}}
 \newcommand{\CGLOBAL}[1]{#1\key{(\%rip)}}
 \newcommand{\GLOBALVALUE}[1]{\LP\key{GlobalValue}~#1\RP}
 \newcommand{\GLOBALVALUE}[1]{\LP\key{GlobalValue}~#1\RP}
 \newcommand{\CGLOBALVALUE}[1]{\LP\key{global-value}~#1\RP}
 \newcommand{\CGLOBALVALUE}[1]{\LP\key{global-value}~#1\RP}
-\newcommand{\ARITY}[1]{\LP\key{Prim}~\code{'procedure-arity}~\LP#1\RP\RP}
+\newcommand{\ARITY}[1]{\LP\key{Prim}~\code{\textquotesingle procedure-arity}~\LP#1\RP\RP}
 \newcommand{\CARITY}[1]{\LP\key{procedure-arity}~#1\RP}
 \newcommand{\CARITY}[1]{\LP\key{procedure-arity}~#1\RP}
 \fi
 \fi
 
 
@@ -202,16 +202,16 @@
 \newcommand{\CPTUPLETY}[1]{\key{ProxyOrTupleType}\LP#1\RP}
 \newcommand{\CPTUPLETY}[1]{\key{ProxyOrTupleType}\LP#1\RP}
 \newcommand{\PARRAYTY}[1]{\key{ProxyOrListType}\LP#1\RP}
 \newcommand{\PARRAYTY}[1]{\key{ProxyOrListType}\LP#1\RP}
 \newcommand{\CPARRAYTY}[1]{\key{ProxyOrListType}\LP#1\RP}
 \newcommand{\CPARRAYTY}[1]{\key{ProxyOrListType}\LP#1\RP}
-\newcommand{\QUOTE}[1]{\code{'}#1\code{'}}
+\newcommand{\QUOTE}[1]{\code{\textquotesingle}#1\code{\textquotesingle}}
 \newcommand{\INT}[1]{{\key{Constant}\LP#1\RP}}
 \newcommand{\INT}[1]{{\key{Constant}\LP#1\RP}}
 \newcommand{\READOP}{{\key{input\_int}}}
 \newcommand{\READOP}{{\key{input\_int}}}
-\newcommand{\READ}{{\key{Call(Name('input\_int'),[])}}}
+\newcommand{\READ}{{\key{Call(Name(\textquotesingle input\_int\textquotesingle),[])}}}
 \newcommand{\CREAD}{\key{input\_int()}}
 \newcommand{\CREAD}{\key{input\_int()}}
 \newcommand{\NEG}[1]{{\key{UnaryOp(USub(),} #1\code{)}}}
 \newcommand{\NEG}[1]{{\key{UnaryOp(USub(),} #1\code{)}}}
 \newcommand{\ADD}[2]{{\key{BinOp}\LP #1\code{,}\key{Add()}\key{,}#2\code{)}}}
 \newcommand{\ADD}[2]{{\key{BinOp}\LP #1\code{,}\key{Add()}\key{,}#2\code{)}}}
 \newcommand{\ADDP}[2]{{\key{BinOp}\LP #1\code{,}\key{Add()}\key{,}#2\code{)}}}
 \newcommand{\ADDP}[2]{{\key{BinOp}\LP #1\code{,}\key{Add()}\key{,}#2\code{)}}}
 \newcommand{\SUB}[2]{{\key{BinOp}\LP #1\code{,}\key{Sub()}\key{,}#2\code{)}}}
 \newcommand{\SUB}[2]{{\key{BinOp}\LP #1\code{,}\key{Sub()}\key{,}#2\code{)}}}
-\newcommand{\PRINT}[1]{{\key{Expr}\LP\key{Call}\LP\key{Name}\LP\key{'print'}\RP\key{,}\LS#1\RS\RP\RP}}
+\newcommand{\PRINT}[1]{{\key{Expr}\LP\key{Call}\LP\key{Name}\LP\key{\textquotesingle print\textquotesingle}\RP\key{,}\LS#1\RS\RP\RP}}
 \newcommand{\CPRINT}[1]{\key{print}\LP #1\RP}
 \newcommand{\CPRINT}[1]{\key{print}\LP #1\RP}
 \newcommand{\EXPR}[1]{{\key{Expr}\LP #1\RP}}
 \newcommand{\EXPR}[1]{{\key{Expr}\LP #1\RP}}
 \newcommand{\PROGRAM}[2]{\code{Module}\LP #2\RP}
 \newcommand{\PROGRAM}[2]{\code{Module}\LP #2\RP}
@@ -229,7 +229,7 @@
 \newcommand{\CGET}[2]{#1 \LS #2 \RS}
 \newcommand{\CGET}[2]{#1 \LS #2 \RS}
 \newcommand{\GET}[2]{\key{Subscript}\LP #1 \code{,} #2 \code{,} \code{Load()} \RP}
 \newcommand{\GET}[2]{\key{Subscript}\LP #1 \code{,} #2 \code{,} \code{Load()} \RP}
 \newcommand{\CLEN}[1]{\code{len}\LP #1 \RP}
 \newcommand{\CLEN}[1]{\code{len}\LP #1 \RP}
-\newcommand{\LEN}[1]{\code{Call}\LP \code{Name('len')} \code{,} \LS #1 \RS \RP}
+\newcommand{\LEN}[1]{\code{Call}\LP \code{Name(\textquotesingle len\textquotesingle)} \code{,} \LS #1 \RS \RP}
 \newcommand{\PUT}[2]{\key{Subscript}\LP #1 \code{,} #2 \code{,} \code{Store()} \RP}
 \newcommand{\PUT}[2]{\key{Subscript}\LP #1 \code{,} #2 \code{,} \code{Store()} \RP}
 \newcommand{\CPUT}[2]{#1[#2]}
 \newcommand{\CPUT}[2]{#1[#2]}
 \newcommand{\TUPLE}[1]{\key{Tuple}\LP #1 \code{,} \code{Load()} \RP}
 \newcommand{\TUPLE}[1]{\key{Tuple}\LP #1 \code{,} \code{Load()} \RP}
@@ -276,7 +276,7 @@
 \newcommand{\FUNREF}[2]{\key{FunRef}\LP#1\code{, }#2\RP}
 \newcommand{\FUNREF}[2]{\key{FunRef}\LP#1\code{, }#2\RP}
 \newcommand{\CFUNREF}[2]{#1\code{(\%rip)}}
 \newcommand{\CFUNREF}[2]{#1\code{(\%rip)}}
 \newcommand{\CLOSURE}[2]{\key{Closure}\LP#1\code{, }#2\RP}
 \newcommand{\CLOSURE}[2]{\key{Closure}\LP#1\code{, }#2\RP}
-\newcommand{\ARITY}[1]{\key{Call}\LP\key{Name}\LP\code{'arity'}\RP \code{, }\LS#1\RS\RP}
+\newcommand{\ARITY}[1]{\key{Call}\LP\key{Name}\LP\code{\textquotesingle arity\textquotesingle}\RP \code{, }\LS#1\RS\RP}
 \newcommand{\CARITY}[1]{\key{arity}\LP#1\RP}
 \newcommand{\CARITY}[1]{\key{arity}\LP#1\RP}
 \newcommand{\CMUL}[2]{#1~\key{*}~#2}
 \newcommand{\CMUL}[2]{#1~\key{*}~#2}
 \newcommand{\MUL}[2]{{\key{BinOp}\LP #1\code{,} \key{Mult()}\key{,}#2\code{)}}}
 \newcommand{\MUL}[2]{{\key{BinOp}\LP #1\code{,} \key{Mult()}\key{,}#2\code{)}}}
@@ -294,7 +294,7 @@
 \newcommand{\CFUNREF}[2]{\LP\key{fun-ref}~#1~#2\RP}
 \newcommand{\CFUNREF}[2]{\LP\key{fun-ref}~#1~#2\RP}
 \newcommand{\CLOSURE}[2]{\LP\key{Closure}~#1~#2\RP}
 \newcommand{\CLOSURE}[2]{\LP\key{Closure}~#1~#2\RP}
 \newcommand{\CMUL}[2]{\LP\key{*}~#1~#2\RP}
 \newcommand{\CMUL}[2]{\LP\key{*}~#1~#2\RP}
-\newcommand{\MUL}[2]{{\key{(Prim}~\code{'*}~\code{(}#1~#2\code{))}}}
+\newcommand{\MUL}[2]{{\key{(Prim}~\code{\textquotesingle *}~\code{(}#1~#2\code{))}}}
 \fi
 \fi
 \newcommand{\PRIM}[2]{\LP\key{Prim}~#1~\LP #2\RP\RP}
 \newcommand{\PRIM}[2]{\LP\key{Prim}~#1~\LP #2\RP\RP}
 \newcommand{\PROGRAMDEFSEXP}[3]{\code{(ProgramDefsExp}~#1~#2~#3\code{)}}
 \newcommand{\PROGRAMDEFSEXP}[3]{\code{(ProgramDefsExp}~#1~#2~#3\code{)}}

+ 1 - 1
python.bib

@@ -26,7 +26,7 @@
 	title = {Python Crash Course},
 	title = {Python Crash Course},
 	year = {2019}}
 	year = {2019}}
 
 
-@Misc{PSF21:cpython,
+@Manual{PSF21:cpython,
   title = 	 {Python {GitHub} {Repository}},
   title = 	 {Python {GitHub} {Repository}},
   organization = {Python Software Foundation},
   organization = {Python Software Foundation},
   year = 	 2021
   year = 	 2021