Commit cb4638a2 authored by Fred Drake's avatar Fred Drake

Change the grammar productions to use the new productionlist environment;

this supports a hyperlinked version of the grammar that can make tracking
down details and definitions a little easier.
parent b2d10062
...@@ -237,13 +237,18 @@ lexical definitions: ...@@ -237,13 +237,18 @@ lexical definitions:
\index{identifier} \index{identifier}
\index{name} \index{name}
\begin{verbatim} \begin{productionlist}
identifier: (letter|"_") (letter|digit|"_")* \production{identifier}
letter: lowercase | uppercase {(\token{letter}|"_") (\token{letter} | \token{digit} | "_")*}
lowercase: "a"..."z" \production{letter}
uppercase: "A"..."Z" {\token{lowercase} | \token{uppercase}}
digit: "0"..."9" \production{lowercase}
\end{verbatim} {"a"..."z"}
\production{uppercase}
{"A"..."Z"}
\production{digit}
{"0"..."9"}
\end{productionlist}
Identifiers are unlimited in length. Case is significant. Identifiers are unlimited in length. Case is significant.
...@@ -303,17 +308,27 @@ Literals are notations for constant values of some built-in types. ...@@ -303,17 +308,27 @@ Literals are notations for constant values of some built-in types.
String literals are described by the following lexical definitions: String literals are described by the following lexical definitions:
\index{string literal} \index{string literal}
\begin{verbatim}
stringliteral: shortstring | longstring
shortstring: "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring: "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
shortstringitem: shortstringchar | escapeseq
longstringitem: longstringchar | escapeseq
shortstringchar: <any ASCII character except "\" or newline or the quote>
longstringchar: <any ASCII character except "\">
escapeseq: "\" <any ASCII character>
\end{verbatim}
\index{ASCII@\ASCII{}} \index{ASCII@\ASCII{}}
\begin{productionlist}
\production{stringliteral}
{\token{shortstring} | \token{longstring}}
\production{shortstring}
{"'" \token{shortstringitem}* "'"
| '"' \token{shortstringitem}* '"'}
\production{longstring}
{"'''" \token{longstringitem}* "'''"
| '"""' \token{longstringitem}* '"""'}
\production{shortstringitem}
{\token{shortstringchar} | \token{escapeseq}}
\production{longstringitem}
{\token{longstringchar} | \token{escapeseq}}
\production{shortstringchar}
{<any ASCII character except "\e" or newline or the quote>}
\production{longstringchar}
{<any ASCII character except "\e">}
\production{escapeseq}
{"\e" <any ASCII character>}
\end{productionlist}
\index{triple-quoted string} \index{triple-quoted string}
\index{Unicode Consortium} \index{Unicode Consortium}
...@@ -452,16 +467,24 @@ Note that numeric literals do not include a sign; a phrase like ...@@ -452,16 +467,24 @@ Note that numeric literals do not include a sign; a phrase like
Integer and long integer literals are described by the following Integer and long integer literals are described by the following
lexical definitions: lexical definitions:
\begin{verbatim} \begin{productionlist}
longinteger: integer ("l"|"L") \production{longinteger}
integer: decimalinteger | octinteger | hexinteger {\token{integer} ("l" | "L")}
decimalinteger: nonzerodigit digit* | "0" \production{integer}
octinteger: "0" octdigit+ {\token{decimalinteger} | \token{octinteger} | \token{hexinteger}}
hexinteger: "0" ("x"|"X") hexdigit+ \production{decimalinteger}
nonzerodigit: "1"..."9" {\token{nonzerodigit} \token{digit}* | "0"}
octdigit: "0"..."7" \production{octinteger}
hexdigit: digit|"a"..."f"|"A"..."F" {"0" \token{octdigit}+}
\end{verbatim} \production{hexinteger}
{"0" ("x" | "X") \token{hexdigit}+}
\production{nonzerodigit}
{"1"..."9"}
\production{octdigit}
{"0"..."7"}
\production{hexdigit}
{\token{digit} | "a"..."f" | "A"..."F"}
\end{productionlist}
Although both lower case `l' and upper case `L' are allowed as suffix Although both lower case `l' and upper case `L' are allowed as suffix
for long integers, it is strongly recommended to always use `L', since for long integers, it is strongly recommended to always use `L', since
...@@ -487,14 +510,21 @@ Some examples of plain and long integer literals: ...@@ -487,14 +510,21 @@ Some examples of plain and long integer literals:
Floating point literals are described by the following lexical Floating point literals are described by the following lexical
definitions: definitions:
\begin{verbatim} \begin{productionlist}
floatnumber: pointfloat | exponentfloat \production{floatnumber}
pointfloat: [intpart] fraction | intpart "." {\token{pointfloat} | \token{exponentfloat}}
exponentfloat: (nonzerodigit digit* | pointfloat) exponent \production{pointfloat}
intpart: nonzerodigit digit* | "0" {[\token{intpart}] \token{fraction} | \token{intpart} "."}
fraction: "." digit+ \production{exponentfloat}
exponent: ("e"|"E") ["+"|"-"] digit+ {(\token{nonzerodigit} \token{digit}* | \token{pointfloat})
\end{verbatim} \token{exponent}}
\production{intpart}
{\token{nonzerodigit} \token{digit}* | "0"}
\production{fraction}
{"." \token{digit}+}
\production{exponent}
{("e" | "E") ["+" | "-"] \token{digit}+}
\end{productionlist}
Note that the integer part of a floating point number cannot look like Note that the integer part of a floating point number cannot look like
an octal integer, though the exponent may look like an octal literal an octal integer, though the exponent may look like an octal literal
...@@ -517,9 +547,9 @@ Note that numeric literals do not include a sign; a phrase like ...@@ -517,9 +547,9 @@ Note that numeric literals do not include a sign; a phrase like
Imaginary literals are described by the following lexical definitions: Imaginary literals are described by the following lexical definitions:
\begin{verbatim} \begin{productionlist}
imagnumber: (floatnumber | intpart) ("j"|"J") \production{imagnumber}{(\token{floatnumber} | \token{intpart}) ("j" | "J")}
\end{verbatim} \end{productionlist}
An imaginary literal yields a complex number with a real part of An imaginary literal yields a complex number with a real part of
0.0. Complex numbers are represented as a pair of floating point 0.0. Complex numbers are represented as a pair of floating point
......
...@@ -8,9 +8,9 @@ Python. ...@@ -8,9 +8,9 @@ Python.
BNF\index{BNF} notation will be used to describe syntax, not lexical BNF\index{BNF} notation will be used to describe syntax, not lexical
analysis. When (one alternative of) a syntax rule has the form analysis. When (one alternative of) a syntax rule has the form
\begin{verbatim} \begin{productionlist}[*]
name: othername \production{name}{\token{othername}}
\end{verbatim} \end{productionlist}
and no semantics are given, the semantics of this form of \code{name} and no semantics are given, the semantics of this form of \code{name}
are the same as for \code{othername}. are the same as for \code{othername}.
...@@ -50,10 +50,13 @@ are identifiers or literals. Forms enclosed in ...@@ -50,10 +50,13 @@ are identifiers or literals. Forms enclosed in
reverse quotes or in parentheses, brackets or braces are also reverse quotes or in parentheses, brackets or braces are also
categorized syntactically as atoms. The syntax for atoms is: categorized syntactically as atoms. The syntax for atoms is:
\begin{verbatim} \begin{productionlist}
atom: identifier | literal | enclosure \production{atom}
enclosure: parenth_form|list_display|dict_display|string_conversion {\token{identifier} | \token{literal} | \token{enclosure}}
\end{verbatim} \production{enclosure}
{\token{parenth_form} | \token{list_display}
| \token{dict_display} | \token{string_conversion}}
\end{productionlist}
\subsection{Identifiers (Names)\label{atom-identifiers}} \subsection{Identifiers (Names)\label{atom-identifiers}}
...@@ -107,9 +110,12 @@ consists only of underscores, no transformation is done. ...@@ -107,9 +110,12 @@ consists only of underscores, no transformation is done.
Python supports string literals and various numeric literals: Python supports string literals and various numeric literals:
\begin{verbatim} \begin{productionlist}
literal: stringliteral | integer | longinteger | floatnumber | imagnumber \production{literal}
\end{verbatim} {\token{stringliteral} | \token{integer}
| \token{longinteger} | \token{floatnumber}
| \token{imagnumber}}
\end{productionlist}
Evaluation of a literal yields an object of the given type (string, Evaluation of a literal yields an object of the given type (string,
integer, long integer, floating point number, complex number) with the integer, long integer, floating point number, complex number) with the
...@@ -132,9 +138,10 @@ the same object or a different object with the same value. ...@@ -132,9 +138,10 @@ the same object or a different object with the same value.
A parenthesized form is an optional expression list enclosed in A parenthesized form is an optional expression list enclosed in
parentheses: parentheses:
\begin{verbatim} \begin{productionlist}
parenth_form: "(" [expression_list] ")" \production{parenth_form}
\end{verbatim} {"(" [\token{expression_list}] ")"}
\end{productionlist}
A parenthesized expression list yields whatever that expression list A parenthesized expression list yields whatever that expression list
yields: if the list contains at least one comma, it yields a tuple; yields: if the list contains at least one comma, it yields a tuple;
...@@ -162,13 +169,20 @@ pass uncaught. ...@@ -162,13 +169,20 @@ pass uncaught.
A list display is a possibly empty series of expressions enclosed in A list display is a possibly empty series of expressions enclosed in
square brackets: square brackets:
\begin{verbatim} \begin{productionlist}
list_display: "[" [listmaker] "]" \production{list_display}
listmaker: expression ( list_for | ( "," expression)* [","] ) {"[" [\token{listmaker}] "]"}
list_iter: list_for | list_if \production{listmaker}
list_for: "for" expression_list "in" testlist [list_iter] {\token{expression} ( \token{list_for}
list_if: "if" test [list_iter] | ( "," \token{expression})* [","] )}
\end{verbatim} \production{list_iter}
{\token{list_for} | \token{list_if}}
\production{list_for}
{"for" \token{expression_list} "in" \token{testlist}
[\token{list_iter}]}
\production{list_if}
{"if" \token{test} [\token{list_iter}]}
\end{productionlist}
A list display yields a new list object. Its contents are specified A list display yields a new list object. Its contents are specified
by providing either a list of expressions or a list comprehension. by providing either a list of expressions or a list comprehension.
...@@ -196,11 +210,14 @@ enclosed in curly braces: ...@@ -196,11 +210,14 @@ enclosed in curly braces:
\index{datum} \index{datum}
\index{key/datum pair} \index{key/datum pair}
\begin{verbatim} \begin{productionlist}
dict_display: "{" [key_datum_list] "}" \production{dict_display}
key_datum_list: key_datum ("," key_datum)* [","] {"{" [\token{key_datum_list}] "}"}
key_datum: expression ":" expression \production{key_datum_list}
\end{verbatim} {\token{key_datum} ("," \token{key_datum})* [","]}
\production{key_datum}
{\token{expression} ":" \token{expression}}
\end{productionlist}
A dictionary display yields a new dictionary object. A dictionary display yields a new dictionary object.
\obindex{dictionary} \obindex{dictionary}
...@@ -226,9 +243,10 @@ stored for a given key value prevails. ...@@ -226,9 +243,10 @@ stored for a given key value prevails.
A string conversion is an expression list enclosed in reverse (a.k.a. A string conversion is an expression list enclosed in reverse (a.k.a.
backward) quotes: backward) quotes:
\begin{verbatim} \begin{productionlist}
string_conversion: "`" expression_list "`" \production{string_conversion}
\end{verbatim} {"`" \token{expression_list} "`"}
\end{productionlist}
A string conversion evaluates the contained expression list and A string conversion evaluates the contained expression list and
converts the resulting object into a string according to rules converts the resulting object into a string according to rules
...@@ -263,9 +281,11 @@ similar but more user-friendly conversion. ...@@ -263,9 +281,11 @@ similar but more user-friendly conversion.
Primaries represent the most tightly bound operations of the language. Primaries represent the most tightly bound operations of the language.
Their syntax is: Their syntax is:
\begin{verbatim} \begin{productionlist}
primary: atom | attributeref | subscription | slicing | call \production{primary}
\end{verbatim} {\token{atom} | \token{attributeref}
| \token{subscription} | \token{slicing} | \token{call}}
\end{productionlist}
\subsection{Attribute references\label{attribute-references}} \subsection{Attribute references\label{attribute-references}}
...@@ -273,9 +293,10 @@ primary: atom | attributeref | subscription | slicing | call ...@@ -273,9 +293,10 @@ primary: atom | attributeref | subscription | slicing | call
An attribute reference is a primary followed by a period and a name: An attribute reference is a primary followed by a period and a name:
\begin{verbatim} \begin{productionlist}
attributeref: primary "." identifier \production{attributeref}
\end{verbatim} {\token{primary} "." \token{identifier}}
\end{productionlist}
The primary must evaluate to an object of a type that supports The primary must evaluate to an object of a type that supports
attribute references, e.g., a module, list, or an instance. This attribute references, e.g., a module, list, or an instance. This
...@@ -302,9 +323,10 @@ or mapping (dictionary) object: ...@@ -302,9 +323,10 @@ or mapping (dictionary) object:
\obindex{dictionary} \obindex{dictionary}
\indexii{sequence}{item} \indexii{sequence}{item}
\begin{verbatim} \begin{productionlist}
subscription: primary "[" expression_list "]" \production{subscription}
\end{verbatim} {\token{primary} "[" \token{expression_list} "]"}
\end{productionlist}
The primary must evaluate to an object of a sequence or mapping type. The primary must evaluate to an object of a sequence or mapping type.
...@@ -339,20 +361,32 @@ targets in assignment or del statements. The syntax for a slicing: ...@@ -339,20 +361,32 @@ targets in assignment or del statements. The syntax for a slicing:
\obindex{tuple} \obindex{tuple}
\obindex{list} \obindex{list}
\begin{verbatim} \begin{productionlist}
slicing: simple_slicing | extended_slicing \production{slicing}
simple_slicing: primary "[" short_slice "]" {\token{simple_slicing} | \token{extended_slicing}}
extended_slicing: primary "[" slice_list "]" \production{simple_slicing}
slice_list: slice_item ("," slice_item)* [","] {\token{primary} "[" \token{short_slice} "]"}
slice_item: expression | proper_slice | ellipsis \production{extended_slicing}
proper_slice: short_slice | long_slice {\token{primary} "[" \token{slice_list} "]" }
short_slice: [lower_bound] ":" [upper_bound] \production{slice_list}
long_slice: short_slice ":" [stride] {\token{slice_item} ("," \token{slice_item})* [","]}
lower_bound: expression \production{slice_item}
upper_bound: expression {\token{expression} | \token{proper_slice} | \token{ellipsis}}
stride: expression \production{proper_slice}
ellipsis: "..." {\token{short_slice} | \token{long_slice}}
\end{verbatim} \production{short_slice}
{[\token{lower_bound}] ":" [\token{upper_bound}]}
\production{long_slice}
{\token{short_slice} ":" [\token{stride}]}
\production{lower_bound}
{\token{expression}}
\production{upper_bound}
{\token{expression}}
\production{stride}
{\token{expression}}
\production{ellipsis}
{"..."}
\end{productionlist}
There is ambiguity in the formal syntax here: anything that looks like There is ambiguity in the formal syntax here: anything that looks like
an expression list also looks like a slice list, so any subscription an expression list also looks like a slice list, so any subscription
...@@ -401,14 +435,19 @@ A call calls a callable object (e.g., a function) with a possibly empty ...@@ -401,14 +435,19 @@ A call calls a callable object (e.g., a function) with a possibly empty
series of arguments: series of arguments:
\obindex{callable} \obindex{callable}
\begin{verbatim} \begin{productionlist}
call: primary "(" [argument_list [","]] ")" \production{call}
argument_list: positional_arguments ["," keyword_arguments] {\token{primary} "(" [\token{argument_list} [","]] ")"}
| keyword_arguments \production{argument_list}
positional_arguments: expression ("," expression)* {\token{positional_arguments} ["," \token{keyword_arguments}]
keyword_arguments: keyword_item ("," keyword_item)* | \token{keyword_arguments}}
keyword_item: identifier "=" expression \production{positional_arguments}
\end{verbatim} {\token{expression} ("," \token{expression})*}
\production{keyword_arguments}
{\token{keyword_item} ("," \token{keyword_item})*}
\production{keyword_item}
{\token{identifier} "=" \token{expression}}
\end{productionlist}
A trailing comma may be present after an argument list but does not A trailing comma may be present after an argument list but does not
affect the semantics. affect the semantics.
...@@ -521,9 +560,10 @@ The power operator binds more tightly than unary operators on its ...@@ -521,9 +560,10 @@ The power operator binds more tightly than unary operators on its
left; it binds less tightly than unary operators on its right. The left; it binds less tightly than unary operators on its right. The
syntax is: syntax is:
\begin{verbatim} \begin{productionlist}
power: primary ["**" u_expr] \production{power}
\end{verbatim} {\token{primary} ["**" \token{u_expr}]}
\end{productionlist}
Thus, in an unparenthesized sequence of power and unary operators, the Thus, in an unparenthesized sequence of power and unary operators, the
operators are evaluated from right to left (this does not constrain operators are evaluated from right to left (this does not constrain
...@@ -545,9 +585,11 @@ power, or a negative floating point number to a broken power), a ...@@ -545,9 +585,11 @@ power, or a negative floating point number to a broken power), a
All unary arithmetic (and bit-wise) operations have the same priority: All unary arithmetic (and bit-wise) operations have the same priority:
\begin{verbatim} \begin{productionlist}
u_expr: power | "-" u_expr | "+" u_expr | "~" u_expr \production{u_expr}
\end{verbatim} {\token{power} | "-" \token{u_expr}
| "+" \token{u_expr} | "~" \token{u_expr}}
\end{productionlist}
The unary \code{-} (minus) operator yields the negation of its The unary \code{-} (minus) operator yields the negation of its
numeric argument. numeric argument.
...@@ -578,11 +620,15 @@ non-numeric types. Apart from the power operator, there are only two ...@@ -578,11 +620,15 @@ non-numeric types. Apart from the power operator, there are only two
levels, one for multiplicative operators and one for additive levels, one for multiplicative operators and one for additive
operators: operators:
\begin{verbatim} \begin{productionlist}
m_expr: u_expr | m_expr "*" u_expr \production{m_expr}
| m_expr "/" u_expr | m_expr "%" u_expr {\token{u_expr} | \token{m_expr} "*" \token{u_expr}
a_expr: m_expr | aexpr "+" m_expr | aexpr "-" m_expr | \token{m_expr} "/" \token{u_expr}
\end{verbatim} | \token{m_expr} "\%" \token{u_expr}}
\production{a_expr}
{\token{m_expr} | \token{aexpr} "+" \token{m_expr}
\token{aexpr} "-" \token{m_expr}}
\end{productionlist}
The \code{*} (multiplication) operator yields the product of its The \code{*} (multiplication) operator yields the product of its
arguments. The arguments must either both be numbers, or one argument arguments. The arguments must either both be numbers, or one argument
...@@ -646,9 +692,11 @@ type. ...@@ -646,9 +692,11 @@ type.
The shifting operations have lower priority than the arithmetic The shifting operations have lower priority than the arithmetic
operations: operations:
\begin{verbatim} \begin{productionlist}
shift_expr: a_expr | shift_expr ( "<<" | ">>" ) a_expr \production{shift_expr}
\end{verbatim} {\token{a_expr}
| \token{shift_expr} ( "<<" | ">>" ) \token{a_expr}}
\end{productionlist}
These operators accept plain or long integers as arguments. The These operators accept plain or long integers as arguments. The
arguments are converted to a common type. They shift the first arguments are converted to a common type. They shift the first
...@@ -670,11 +718,14 @@ exception. ...@@ -670,11 +718,14 @@ exception.
Each of the three bitwise operations has a different priority level: Each of the three bitwise operations has a different priority level:
\begin{verbatim} \begin{productionlist}
and_expr: shift_expr | and_expr "&" shift_expr \production{and_expr}
xor_expr: and_expr | xor_expr "^" and_expr {\token{shift_expr} | \token{and_expr} "\&" \token{shift_expr}}
or_expr: xor_expr | or_expr "|" xor_expr \production{xor_expr}
\end{verbatim} {\token{and_expr} | \token{xor_expr} "\textasciicircum" \token{and_expr}}
\production{or_expr}
{\token{xor_expr} | \token{or_expr} "|" \token{xor_expr}}
\end{productionlist}
The \code{\&} operator yields the bitwise AND of its arguments, which The \code{\&} operator yields the bitwise AND of its arguments, which
must be plain or long integers. The arguments are converted to a must be plain or long integers. The arguments are converted to a
...@@ -703,10 +754,13 @@ operation. Also unlike C, expressions like \code{a < b < c} have the ...@@ -703,10 +754,13 @@ operation. Also unlike C, expressions like \code{a < b < c} have the
interpretation that is conventional in mathematics: interpretation that is conventional in mathematics:
\indexii{C}{language} \indexii{C}{language}
\begin{verbatim} \begin{productionlist}
comparison: or_expr (comp_operator or_expr)* \production{comparison}
comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in" {\token{or_expr} ( \token{comp_operator} \token{or_expr} )*}
\end{verbatim} \production{comp_operator}
{"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"}
\end{productionlist}
Comparisons yield integer values: \code{1} for true, \code{0} for false. Comparisons yield integer values: \code{1} for true, \code{0} for false.
...@@ -830,13 +884,18 @@ truth value. ...@@ -830,13 +884,18 @@ truth value.
Boolean operations have the lowest priority of all Python operations: Boolean operations have the lowest priority of all Python operations:
\begin{verbatim} \begin{productionlist}
expression: or_test | lambda_form \production{expression}
or_test: and_test | or_test "or" and_test {\token{or_test} | \token{lambda_form}}
and_test: not_test | and_test "and" not_test \production{or_test}
not_test: comparison | "not" not_test {\token{and_test} | \token{or_test} "or" \token{and_test}}
lambda_form: "lambda" [parameter_list]: expression \production{and_test}
\end{verbatim} {\token{not_test} | \token{and_test} "and" \token{not_test}}
\production{not_test}
{\token{comparison} | "not" \token{not_test}}
\production{lambda_form}
{"lambda" [\token{parameter_list}]: \token{expression}}
\end{productionlist}
In the context of Boolean operations, and also when expressions are In the context of Boolean operations, and also when expressions are
used by control flow statements, the following values are interpreted used by control flow statements, the following values are interpreted
...@@ -914,9 +973,10 @@ def make_incrementor(increment): ...@@ -914,9 +973,10 @@ def make_incrementor(increment):
\section{Expression lists\label{exprlists}} \section{Expression lists\label{exprlists}}
\indexii{expression}{list} \indexii{expression}{list}
\begin{verbatim} \begin{productionlist}
expression_list: expression ("," expression)* [","] \production{expression_list}
\end{verbatim} {\token{expression} ( "," \token{expression} )* [","]}
\end{productionlist}
An expression list containing at least one comma yields a An expression list containing at least one comma yields a
tuple. The length of the tuple is the number of expressions in the tuple. The length of the tuple is the number of expressions in the
......
...@@ -5,22 +5,23 @@ Simple statements are comprised within a single logical line. ...@@ -5,22 +5,23 @@ Simple statements are comprised within a single logical line.
Several simple statements may occur on a single line separated Several simple statements may occur on a single line separated
by semicolons. The syntax for simple statements is: by semicolons. The syntax for simple statements is:
\begin{verbatim} \begin{productionlist}
simple_stmt: expression_stmt \production{simple_stmt}
| assert_stmt {\token{expression_stmt}
| assignment_stmt | \token{assert_stmt}
| augmented_assignment_stmt | \token{assignment_stmt}
| pass_stmt | \token{augmented_assignment_stmt}
| del_stmt | \token{pass_stmt}
| print_stmt | \token{del_stmt}
| return_stmt | \token{print_stmt}
| raise_stmt | \token{return_stmt}
| break_stmt | \token{raise_stmt}
| continue_stmt | \token{break_stmt}
| import_stmt | \token{continue_stmt}
| global_stmt | \token{import_stmt}
| exec_stmt | \token{global_stmt}
\end{verbatim} | \token{exec_stmt}}
\end{productionlist}
\section{Expression statements \label{exprstmts}} \section{Expression statements \label{exprstmts}}
...@@ -32,9 +33,10 @@ returns no meaningful result; in Python, procedures return the value ...@@ -32,9 +33,10 @@ returns no meaningful result; in Python, procedures return the value
\code{None}). Other uses of expression statements are allowed and \code{None}). Other uses of expression statements are allowed and
occasionally useful. The syntax for an expression statement is: occasionally useful. The syntax for an expression statement is:
\begin{verbatim} \begin{productionlist}
expression_stmt: expression_list \production{expression_stmt}
\end{verbatim} {\token{expression_list}}
\end{productionlist}
An expression statement evaluates the expression list (which may be a An expression statement evaluates the expression list (which may be a
single expression). single expression).
...@@ -59,9 +61,10 @@ any output.) ...@@ -59,9 +61,10 @@ any output.)
Assert statements\stindex{assert} are a convenient way to insert Assert statements\stindex{assert} are a convenient way to insert
debugging assertions\indexii{debugging}{assertions} into a program: debugging assertions\indexii{debugging}{assertions} into a program:
\begin{verbatim} \begin{productionlist}
assert_statement: "assert" expression ["," expression] \production{assert_statement}
\end{verbatim} {"assert" \token{expression} ["," \token{expression}]}
\end{productionlist}
The simple form, \samp{assert expression}, is equivalent to The simple form, \samp{assert expression}, is equivalent to
...@@ -102,12 +105,19 @@ objects: ...@@ -102,12 +105,19 @@ objects:
\obindex{mutable} \obindex{mutable}
\indexii{attribute}{assignment} \indexii{attribute}{assignment}
\begin{verbatim} \begin{productionlist}
assignment_stmt: (target_list "=")+ expression_list \production{assignment_stmt}
target_list: target ("," target)* [","] {(\token{target_list} "=")+ \token{expression_list}}
target: identifier | "(" target_list ")" | "[" target_list "]" \production{target_list}
| attributeref | subscription | slicing {\token{target} ("," \token{target})* [","]}
\end{verbatim} \production{target}
{\token{identifier}
| "(" \token{target_list} ")"
| "[" \token{target_list} "]"
| \token{attributeref}
| \token{subscription}
| \token{slicing}}
\end{productionlist}
(See section \ref{primaries} for the syntax definitions for the last (See section \ref{primaries} for the syntax definitions for the last
three symbols.) three symbols.)
...@@ -260,13 +270,20 @@ operation and an assignment statement: ...@@ -260,13 +270,20 @@ operation and an assignment statement:
\indexii{augmented}{assignment} \indexii{augmented}{assignment}
\index{statement!assignment, augmented} \index{statement!assignment, augmented}
\begin{verbatim} \begin{productionlist}
augmented_assignment_stmt: target augop expression_list \production{augmented_assignment_stmt}
augop: "+=" | "-=" | "*=" | "/=" | "%=" | "**=" {\token{target} \token{augop} \token{expression_list}}
| ">>=" | "<<=" | "&=" | "^=" | "|=" \production{augop}
target: identifier | "(" target_list ")" | "[" target_list "]" {"+=" | "-=" | "*=" | "/=" | "\%=" | "**="
| attributeref | subscription | slicing | ">>=" | "<<=" | "\&=" | "\textasciicircum=" | "|="}
\end{verbatim} \production{target}
{\token{identifier}
| "(" \token{target_list} ")"
| "[" \token{target_list} "]"
| \token{attributeref}
| \token{subscription}
| \token{slicing}}
\end{productionlist}
(See section \ref{primaries} for the syntax definitions for the last (See section \ref{primaries} for the syntax definitions for the last
three symbols.) three symbols.)
...@@ -294,9 +311,10 @@ augmented assignment is the same as the normal binary operations. ...@@ -294,9 +311,10 @@ augmented assignment is the same as the normal binary operations.
\section{The \keyword{pass} statement \label{pass}} \section{The \keyword{pass} statement \label{pass}}
\stindex{pass} \stindex{pass}
\begin{verbatim} \begin{productionlist}
pass_stmt: "pass" \production{pass_stmt}
\end{verbatim} {"pass"}
\end{productionlist}
\keyword{pass} is a null operation --- when it is executed, nothing \keyword{pass} is a null operation --- when it is executed, nothing
happens. It is useful as a placeholder when a statement is happens. It is useful as a placeholder when a statement is
...@@ -313,9 +331,10 @@ class C: pass # a class with no methods (yet) ...@@ -313,9 +331,10 @@ class C: pass # a class with no methods (yet)
\section{The \keyword{del} statement \label{del}} \section{The \keyword{del} statement \label{del}}
\stindex{del} \stindex{del}
\begin{verbatim} \begin{productionlist}
del_stmt: "del" target_list \production{del_stmt}
\end{verbatim} {"del" \token{target_list}}
\end{productionlist}
Deletion is recursively defined very similar to the way assignment is Deletion is recursively defined very similar to the way assignment is
defined. Rather that spelling it out in full details, here are some defined. Rather that spelling it out in full details, here are some
...@@ -342,9 +361,12 @@ right type (but even this is determined by the sliced object). ...@@ -342,9 +361,12 @@ right type (but even this is determined by the sliced object).
\section{The \keyword{print} statement \label{print}} \section{The \keyword{print} statement \label{print}}
\stindex{print} \stindex{print}
\begin{verbatim} \begin{productionlist}
print_stmt: "print" [ expression ("," expression)* [","] ] \production{print_stmt}
\end{verbatim} {"print" ( \optional{\token{expression} ("," \token{expression})* \optional{","}}
| ">\code{>}" \token{expression}
\optional{("," \token{expression})+ \optional{","}})}
\end{productionlist}
\keyword{print} evaluates each expression in turn and writes the \keyword{print} evaluates each expression in turn and writes the
resulting object to standard output (see below). If an object is not resulting object to standard output (see below). If an object is not
...@@ -376,13 +398,9 @@ exception is raised. ...@@ -376,13 +398,9 @@ exception is raised.
\withsubitem{(in module sys)}{\ttindex{stdout}} \withsubitem{(in module sys)}{\ttindex{stdout}}
\exindex{RuntimeError} \exindex{RuntimeError}
\keyword{print} also has an extended form, defined as \keyword{print} also has an extended\index{extended print statement}
\index{extended print statement} form, defined by the second portion of the syntax described above.
This form is sometimes referred to as ``\keyword{print} chevron.''
\begin{verbatim}
print_stmt: "print" ">>" expression [ ("," expression)+ [","] ]
\end{verbatim}
In this form, the first expression after the \code{>}\code{>} must In this form, the first expression after the \code{>}\code{>} must
evaluate to a ``file-like'' object, specifically an object that has a evaluate to a ``file-like'' object, specifically an object that has a
\method{write()} method as described above. With this extended form, \method{write()} method as described above. With this extended form,
...@@ -394,9 +412,10 @@ used as the file for output. ...@@ -394,9 +412,10 @@ used as the file for output.
\section{The \keyword{return} statement \label{return}} \section{The \keyword{return} statement \label{return}}
\stindex{return} \stindex{return}
\begin{verbatim} \begin{productionlist}
return_stmt: "return" [expression_list] \production{return_stmt}
\end{verbatim} {"return" [\token{expression_list}]}
\end{productionlist}
\keyword{return} may only occur syntactically nested in a function \keyword{return} may only occur syntactically nested in a function
definition, not within a nested class definition. definition, not within a nested class definition.
...@@ -418,9 +437,11 @@ before really leaving the function. ...@@ -418,9 +437,11 @@ before really leaving the function.
\section{The \keyword{raise} statement \label{raise}} \section{The \keyword{raise} statement \label{raise}}
\stindex{raise} \stindex{raise}
\begin{verbatim} \begin{productionlist}
raise_stmt: "raise" [expression ["," expression ["," expression]]] \production{raise_stmt}
\end{verbatim} {"raise" [\token{expression} ["," \token{expression}
["," \token{expression}]]]}
\end{productionlist}
If no expressions are present, \keyword{raise} re-raises the last If no expressions are present, \keyword{raise} re-raises the last
expression that was raised in the current scope. expression that was raised in the current scope.
...@@ -459,9 +480,10 @@ transparently in an except clause. ...@@ -459,9 +480,10 @@ transparently in an except clause.
\section{The \keyword{break} statement \label{break}} \section{The \keyword{break} statement \label{break}}
\stindex{break} \stindex{break}
\begin{verbatim} \begin{productionlist}
break_stmt: "break" \production{break_stmt}
\end{verbatim} {"break"}
\end{productionlist}
\keyword{break} may only occur syntactically nested in a \keyword{for} \keyword{break} may only occur syntactically nested in a \keyword{for}
or \keyword{while} loop, but not nested in a function or class definition or \keyword{while} loop, but not nested in a function or class definition
...@@ -487,9 +509,10 @@ before really leaving the loop. ...@@ -487,9 +509,10 @@ before really leaving the loop.
\section{The \keyword{continue} statement \label{continue}} \section{The \keyword{continue} statement \label{continue}}
\stindex{continue} \stindex{continue}
\begin{verbatim} \begin{productionlist}
continue_stmt: "continue" \production{continue_stmt}
\end{verbatim} {"continue"}
\end{productionlist}
\keyword{continue} may only occur syntactically nested in a \keyword{for} or \keyword{continue} may only occur syntactically nested in a \keyword{for} or
\keyword{while} loop, but not nested in a function or class definition or \keyword{while} loop, but not nested in a function or class definition or
...@@ -507,13 +530,17 @@ It continues with the next cycle of the nearest enclosing loop. ...@@ -507,13 +530,17 @@ It continues with the next cycle of the nearest enclosing loop.
\section{The \keyword{import} statement \label{import}} \section{The \keyword{import} statement \label{import}}
\stindex{import} \stindex{import}
\begin{verbatim} \begin{productionlist}
import_stmt: "import" module ["as" name] ("," module ["as" name] )* \production{import_stmt}
| "from" module "import" identifier ["as" name] {"import" \token{module} ["as" \token{name}]
("," identifier ["as" name] )* ( "," \token{module} ["as" \token{name}] )*
| "from" module "import" "*" | "from" \token{module} "import" \token{identifier}
module: (identifier ".")* identifier ["as" \token{name}]
\end{verbatim} ( "," \token{identifier} ["as" \token{name}] )*
| "from" \token{module} "import" "*"}
\production{module}
{(\token{identifier} ".")* \token{identifier}}
\end{productionlist}
Import statements are executed in two steps: (1) find a module, and Import statements are executed in two steps: (1) find a module, and
initialize it if necessary; (2) define a name or names in the local initialize it if necessary; (2) define a name or names in the local
...@@ -608,9 +635,10 @@ about how the module search works from inside a package.] ...@@ -608,9 +635,10 @@ about how the module search works from inside a package.]
\section{The \keyword{global} statement \label{global}} \section{The \keyword{global} statement \label{global}}
\stindex{global} \stindex{global}
\begin{verbatim} \begin{productionlist}
global_stmt: "global" identifier ("," identifier)* \production{global_stmt}
\end{verbatim} {"global" \token{identifier} ("," \token{identifier})*}
\end{productionlist}
The \keyword{global} statement is a declaration which holds for the The \keyword{global} statement is a declaration which holds for the
entire current code block. It means that the listed identifiers are to be entire current code block. It means that the listed identifiers are to be
...@@ -649,9 +677,11 @@ containing the \keyword{exec} statement. The same applies to the ...@@ -649,9 +677,11 @@ containing the \keyword{exec} statement. The same applies to the
\section{The \keyword{exec} statement \label{exec}} \section{The \keyword{exec} statement \label{exec}}
\stindex{exec} \stindex{exec}
\begin{verbatim} \begin{productionlist}
exec_stmt: "exec" expression ["in" expression ["," expression]] \production{exec_stmt}
\end{verbatim} {"exec" \token{expression}
["in" \token{expression} ["," \token{expression}]]}
\end{productionlist}
This statement supports dynamic execution of Python code. The first This statement supports dynamic execution of Python code. The first
expression should evaluate to either a string, an open file object, or expression should evaluate to either a string, an open file object, or
......
...@@ -40,13 +40,18 @@ if x < y < z: print x; print y; print z ...@@ -40,13 +40,18 @@ if x < y < z: print x; print y; print z
Summarizing: Summarizing:
\begin{verbatim} \begin{productionlist}
compound_stmt: if_stmt | while_stmt | for_stmt \production{compound_stmt}
| try_stmt | funcdef | classdef {\token{if_stmt} | \token{while_stmt} | \token{for_stmt}
suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT | \token{try_stmt} | \token{funcdef} | \token{classdef}}
statement: stmt_list NEWLINE | compound_stmt \production{suite}
stmt_list: simple_stmt (";" simple_stmt)* [";"] {\token{stmt_list} NEWLINE
\end{verbatim} | NEWLINE INDENT \token{statement}+ DEDENT}
\production{statement}
{\token{stmt_list} NEWLINE | \token{compound_stmt}}
\production{stmt_list}
{\token{simple_stmt} (";" \token{simple_stmt})* [";"]}
\end{productionlist}
Note that statements always end in a Note that statements always end in a
\code{NEWLINE}\index{NEWLINE token} possibly followed by a \code{NEWLINE}\index{NEWLINE token} possibly followed by a
...@@ -66,11 +71,12 @@ each clause on a separate line for clarity. ...@@ -66,11 +71,12 @@ each clause on a separate line for clarity.
The \keyword{if} statement is used for conditional execution: The \keyword{if} statement is used for conditional execution:
\begin{verbatim} \begin{productionlist}
if_stmt: "if" expression ":" suite \production{if_stmt}
("elif" expression ":" suite)* {"if" \token{expression} ":" \token{suite}
["else" ":" suite] ( "elif" \token{expression} ":" \token{suite} )*
\end{verbatim} ["else" ":" \token{suite}]}
\end{productionlist}
It selects exactly one of the suites by evaluating the expressions one It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section \ref{Booleans} for by one until one is found to be true (see section \ref{Booleans} for
...@@ -89,10 +95,11 @@ present, is executed. ...@@ -89,10 +95,11 @@ present, is executed.
The \keyword{while} statement is used for repeated execution as long The \keyword{while} statement is used for repeated execution as long
as an expression is true: as an expression is true:
\begin{verbatim} \begin{productionlist}
while_stmt: "while" expression ":" suite \production{while_stmt}
["else" ":" suite] {"while" \token{expression} ":" \token{suite}
\end{verbatim} ["else" ":" \token{suite}]}
\end{productionlist}
This repeatedly tests the expression and, if it is true, executes the This repeatedly tests the expression and, if it is true, executes the
first suite; if the expression is false (which may be the first time it first suite; if the expression is false (which may be the first time it
...@@ -116,10 +123,12 @@ The \keyword{for} statement is used to iterate over the elements of a ...@@ -116,10 +123,12 @@ The \keyword{for} statement is used to iterate over the elements of a
sequence (such as a string, tuple or list) or other iterable object: sequence (such as a string, tuple or list) or other iterable object:
\obindex{sequence} \obindex{sequence}
\begin{verbatim} \begin{productionlist}
for_stmt: "for" target_list "in" expression_list ":" suite \production{for_stmt}
["else" ":" suite] {"for" \token{target_list} "in" \token{expression_list}
\end{verbatim} ":" \token{suite}
["else" ":" \token{suite}]}
\end{productionlist}
The expression list is evaluated once; it should yield a sequence. The The expression list is evaluated once; it should yield a sequence. The
suite is then executed once for each item in the sequence, in the suite is then executed once for each item in the sequence, in the
...@@ -179,14 +188,18 @@ for x in a[:]: ...@@ -179,14 +188,18 @@ for x in a[:]:
The \keyword{try} statement specifies exception handlers and/or cleanup The \keyword{try} statement specifies exception handlers and/or cleanup
code for a group of statements: code for a group of statements:
\begin{verbatim} \begin{productionlist}
try_stmt: try_exc_stmt | try_fin_stmt \production{try_stmt}
try_exc_stmt: "try" ":" suite {\token{try_exc_stmt} | \token{try_fin_stmt}}
("except" [expression ["," target]] ":" suite)+ \production{try_exc_stmt}
["else" ":" suite] {"try" ":" \token{suite}
try_fin_stmt: "try" ":" suite ("except" [\token{expression} ["," \token{target}]] ":"
"finally" ":" suite \token{suite})+
\end{verbatim} ["else" ":" \token{suite}]}
\production{try_fin_stmt}
{"try" ":" \token{suite}
"finally" ":" \token{suite}}
\end{productionlist}
There are two forms of \keyword{try} statement: There are two forms of \keyword{try} statement:
\keyword{try}...\keyword{except} and \keyword{try}...\keyword{except} and
...@@ -291,16 +304,24 @@ section \ref{types}): ...@@ -291,16 +304,24 @@ section \ref{types}):
\obindex{user-defined function} \obindex{user-defined function}
\obindex{function} \obindex{function}
\begin{verbatim} \begin{productionlist}
funcdef: "def" funcname "(" [parameter_list] ")" ":" suite \production{funcdef}
parameter_list: (defparameter ",")* ("*" identifier [, "**" identifier] {"def" \token{funcname} "(" [\token{parameter_list}] ")"
| "**" identifier ":" \token{suite}}
| defparameter [","]) \production{parameter_list}
defparameter: parameter ["=" expression] {(\token{defparameter} ",")*
sublist: parameter ("," parameter)* [","] ("*" \token{identifier} [, "**" \token{identifier}]
parameter: identifier | "(" sublist ")" | "**" \token{identifier}
funcname: identifier | \token{defparameter} [","])}
\end{verbatim} \production{defparameter}
{\token{parameter} ["=" \token{expression}]}
\production{sublist}
{\token{parameter} ("," \token{parameter})* [","]}
\production{parameter}
{\token{identifier} | "(" \token{sublist} ")"}
\production{funcname}
{\token{identifier}}
\end{productionlist}
A function definition is an executable statement. Its execution binds A function definition is an executable statement. Its execution binds
the function name in the current local namespace to a function object the function name in the current local namespace to a function object
...@@ -376,11 +397,15 @@ description of the new semantics. ...@@ -376,11 +397,15 @@ description of the new semantics.
A class definition defines a class object (see section \ref{types}): A class definition defines a class object (see section \ref{types}):
\obindex{class} \obindex{class}
\begin{verbatim} \begin{productionlist}
classdef: "class" classname [inheritance] ":" suite \production{classdef}
inheritance: "(" [expression_list] ")" {"class" \token{classname} [\token{inheritance}] ":"
classname: identifier \token{suite}}
\end{verbatim} \production{inheritance}
{"(" [\token{expression_list}] ")"}
\production{classname}
{\token{identifier}}
\end{productionlist}
A class definition is an executable statement. It first evaluates the A class definition is an executable statement. It first evaluates the
inheritance list, if present. Each item in the inheritance list inheritance list, if present. Each item in the inheritance list
......
...@@ -49,9 +49,10 @@ program. ...@@ -49,9 +49,10 @@ program.
All input read from non-interactive files has the same form: All input read from non-interactive files has the same form:
\begin{verbatim} \begin{productionlist}
file_input: (NEWLINE | statement)* \production{file_input}
\end{verbatim} {(NEWLINE | \token{statement})*}
\end{productionlist}
This syntax is used in the following situations: This syntax is used in the following situations:
...@@ -70,9 +71,10 @@ This syntax is used in the following situations: ...@@ -70,9 +71,10 @@ This syntax is used in the following situations:
Input in interactive mode is parsed using the following grammar: Input in interactive mode is parsed using the following grammar:
\begin{verbatim} \begin{productionlist}
interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE \production{interactive_input}
\end{verbatim} {[\token{stmt_list}] NEWLINE | \token{compound_stmt} NEWLINE}
\end{productionlist}
Note that a (top-level) compound statement must be followed by a blank Note that a (top-level) compound statement must be followed by a blank
line in interactive mode; this is needed to help the parser detect the line in interactive mode; this is needed to help the parser detect the
...@@ -87,16 +89,18 @@ whitespace. ...@@ -87,16 +89,18 @@ whitespace.
The string argument to \function{eval()} must have the following form: The string argument to \function{eval()} must have the following form:
\bifuncindex{eval} \bifuncindex{eval}
\begin{verbatim} \begin{productionlist}
eval_input: expression_list NEWLINE* \production{eval_input}
\end{verbatim} {\token{expression_list} NEWLINE*}
\end{productionlist}
The input line read by \function{input()} must have the following form: The input line read by \function{input()} must have the following form:
\bifuncindex{input} \bifuncindex{input}
\begin{verbatim} \begin{productionlist}
input_input: expression_list NEWLINE \production{input_input}
\end{verbatim} {\token{expression_list} NEWLINE}
\end{productionlist}
Note: to read `raw' input line without interpretation, you can use the Note: to read `raw' input line without interpretation, you can use the
built-in function \function{raw_input()} or the \method{readline()} method built-in function \function{raw_input()} or the \method{readline()} method
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment