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.
This commit is contained in:
Fred Drake 2001-07-06 22:49:53 +00:00
parent b2d1006272
commit cb4638a278
5 changed files with 410 additions and 261 deletions

View File

@ -237,13 +237,18 @@ lexical definitions:
\index{identifier}
\index{name}
\begin{verbatim}
identifier: (letter|"_") (letter|digit|"_")*
letter: lowercase | uppercase
lowercase: "a"..."z"
uppercase: "A"..."Z"
digit: "0"..."9"
\end{verbatim}
\begin{productionlist}
\production{identifier}
{(\token{letter}|"_") (\token{letter} | \token{digit} | "_")*}
\production{letter}
{\token{lowercase} | \token{uppercase}}
\production{lowercase}
{"a"..."z"}
\production{uppercase}
{"A"..."Z"}
\production{digit}
{"0"..."9"}
\end{productionlist}
Identifiers are unlimited in length. Case is significant.
@ -303,17 +308,27 @@ Literals are notations for constant values of some built-in types.
String literals are described by the following lexical definitions:
\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{}}
\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{Unicode Consortium}
@ -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
lexical definitions:
\begin{verbatim}
longinteger: integer ("l"|"L")
integer: decimalinteger | octinteger | hexinteger
decimalinteger: nonzerodigit digit* | "0"
octinteger: "0" octdigit+
hexinteger: "0" ("x"|"X") hexdigit+
nonzerodigit: "1"..."9"
octdigit: "0"..."7"
hexdigit: digit|"a"..."f"|"A"..."F"
\end{verbatim}
\begin{productionlist}
\production{longinteger}
{\token{integer} ("l" | "L")}
\production{integer}
{\token{decimalinteger} | \token{octinteger} | \token{hexinteger}}
\production{decimalinteger}
{\token{nonzerodigit} \token{digit}* | "0"}
\production{octinteger}
{"0" \token{octdigit}+}
\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
for long integers, it is strongly recommended to always use `L', since
@ -487,14 +510,21 @@ Some examples of plain and long integer literals:
Floating point literals are described by the following lexical
definitions:
\begin{verbatim}
floatnumber: pointfloat | exponentfloat
pointfloat: [intpart] fraction | intpart "."
exponentfloat: (nonzerodigit digit* | pointfloat) exponent
intpart: nonzerodigit digit* | "0"
fraction: "." digit+
exponent: ("e"|"E") ["+"|"-"] digit+
\end{verbatim}
\begin{productionlist}
\production{floatnumber}
{\token{pointfloat} | \token{exponentfloat}}
\production{pointfloat}
{[\token{intpart}] \token{fraction} | \token{intpart} "."}
\production{exponentfloat}
{(\token{nonzerodigit} \token{digit}* | \token{pointfloat})
\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
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
Imaginary literals are described by the following lexical definitions:
\begin{verbatim}
imagnumber: (floatnumber | intpart) ("j"|"J")
\end{verbatim}
\begin{productionlist}
\production{imagnumber}{(\token{floatnumber} | \token{intpart}) ("j" | "J")}
\end{productionlist}
An imaginary literal yields a complex number with a real part of
0.0. Complex numbers are represented as a pair of floating point

View File

@ -8,9 +8,9 @@ Python.
BNF\index{BNF} notation will be used to describe syntax, not lexical
analysis. When (one alternative of) a syntax rule has the form
\begin{verbatim}
name: othername
\end{verbatim}
\begin{productionlist}[*]
\production{name}{\token{othername}}
\end{productionlist}
and no semantics are given, the semantics of this form of \code{name}
are the same as for \code{othername}.
@ -50,10 +50,13 @@ are identifiers or literals. Forms enclosed in
reverse quotes or in parentheses, brackets or braces are also
categorized syntactically as atoms. The syntax for atoms is:
\begin{verbatim}
atom: identifier | literal | enclosure
enclosure: parenth_form|list_display|dict_display|string_conversion
\end{verbatim}
\begin{productionlist}
\production{atom}
{\token{identifier} | \token{literal} | \token{enclosure}}
\production{enclosure}
{\token{parenth_form} | \token{list_display}
| \token{dict_display} | \token{string_conversion}}
\end{productionlist}
\subsection{Identifiers (Names)\label{atom-identifiers}}
@ -107,9 +110,12 @@ consists only of underscores, no transformation is done.
Python supports string literals and various numeric literals:
\begin{verbatim}
literal: stringliteral | integer | longinteger | floatnumber | imagnumber
\end{verbatim}
\begin{productionlist}
\production{literal}
{\token{stringliteral} | \token{integer}
| \token{longinteger} | \token{floatnumber}
| \token{imagnumber}}
\end{productionlist}
Evaluation of a literal yields an object of the given type (string,
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.
A parenthesized form is an optional expression list enclosed in
parentheses:
\begin{verbatim}
parenth_form: "(" [expression_list] ")"
\end{verbatim}
\begin{productionlist}
\production{parenth_form}
{"(" [\token{expression_list}] ")"}
\end{productionlist}
A parenthesized expression list yields whatever that expression list
yields: if the list contains at least one comma, it yields a tuple;
@ -162,13 +169,20 @@ pass uncaught.
A list display is a possibly empty series of expressions enclosed in
square brackets:
\begin{verbatim}
list_display: "[" [listmaker] "]"
listmaker: expression ( list_for | ( "," expression)* [","] )
list_iter: list_for | list_if
list_for: "for" expression_list "in" testlist [list_iter]
list_if: "if" test [list_iter]
\end{verbatim}
\begin{productionlist}
\production{list_display}
{"[" [\token{listmaker}] "]"}
\production{listmaker}
{\token{expression} ( \token{list_for}
| ( "," \token{expression})* [","] )}
\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
by providing either a list of expressions or a list comprehension.
@ -196,11 +210,14 @@ enclosed in curly braces:
\index{datum}
\index{key/datum pair}
\begin{verbatim}
dict_display: "{" [key_datum_list] "}"
key_datum_list: key_datum ("," key_datum)* [","]
key_datum: expression ":" expression
\end{verbatim}
\begin{productionlist}
\production{dict_display}
{"{" [\token{key_datum_list}] "}"}
\production{key_datum_list}
{\token{key_datum} ("," \token{key_datum})* [","]}
\production{key_datum}
{\token{expression} ":" \token{expression}}
\end{productionlist}
A dictionary display yields a new dictionary object.
\obindex{dictionary}
@ -226,9 +243,10 @@ stored for a given key value prevails.
A string conversion is an expression list enclosed in reverse (a.k.a.
backward) quotes:
\begin{verbatim}
string_conversion: "`" expression_list "`"
\end{verbatim}
\begin{productionlist}
\production{string_conversion}
{"`" \token{expression_list} "`"}
\end{productionlist}
A string conversion evaluates the contained expression list and
converts the resulting object into a string according to rules
@ -263,9 +281,11 @@ similar but more user-friendly conversion.
Primaries represent the most tightly bound operations of the language.
Their syntax is:
\begin{verbatim}
primary: atom | attributeref | subscription | slicing | call
\end{verbatim}
\begin{productionlist}
\production{primary}
{\token{atom} | \token{attributeref}
| \token{subscription} | \token{slicing} | \token{call}}
\end{productionlist}
\subsection{Attribute references\label{attribute-references}}
@ -273,9 +293,10 @@ primary: atom | attributeref | subscription | slicing | call
An attribute reference is a primary followed by a period and a name:
\begin{verbatim}
attributeref: primary "." identifier
\end{verbatim}
\begin{productionlist}
\production{attributeref}
{\token{primary} "." \token{identifier}}
\end{productionlist}
The primary must evaluate to an object of a type that supports
attribute references, e.g., a module, list, or an instance. This
@ -302,9 +323,10 @@ or mapping (dictionary) object:
\obindex{dictionary}
\indexii{sequence}{item}
\begin{verbatim}
subscription: primary "[" expression_list "]"
\end{verbatim}
\begin{productionlist}
\production{subscription}
{\token{primary} "[" \token{expression_list} "]"}
\end{productionlist}
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:
\obindex{tuple}
\obindex{list}
\begin{verbatim}
slicing: simple_slicing | extended_slicing
simple_slicing: primary "[" short_slice "]"
extended_slicing: primary "[" slice_list "]"
slice_list: slice_item ("," slice_item)* [","]
slice_item: expression | proper_slice | ellipsis
proper_slice: short_slice | long_slice
short_slice: [lower_bound] ":" [upper_bound]
long_slice: short_slice ":" [stride]
lower_bound: expression
upper_bound: expression
stride: expression
ellipsis: "..."
\end{verbatim}
\begin{productionlist}
\production{slicing}
{\token{simple_slicing} | \token{extended_slicing}}
\production{simple_slicing}
{\token{primary} "[" \token{short_slice} "]"}
\production{extended_slicing}
{\token{primary} "[" \token{slice_list} "]" }
\production{slice_list}
{\token{slice_item} ("," \token{slice_item})* [","]}
\production{slice_item}
{\token{expression} | \token{proper_slice} | \token{ellipsis}}
\production{proper_slice}
{\token{short_slice} | \token{long_slice}}
\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
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
series of arguments:
\obindex{callable}
\begin{verbatim}
call: primary "(" [argument_list [","]] ")"
argument_list: positional_arguments ["," keyword_arguments]
| keyword_arguments
positional_arguments: expression ("," expression)*
keyword_arguments: keyword_item ("," keyword_item)*
keyword_item: identifier "=" expression
\end{verbatim}
\begin{productionlist}
\production{call}
{\token{primary} "(" [\token{argument_list} [","]] ")"}
\production{argument_list}
{\token{positional_arguments} ["," \token{keyword_arguments}]
| \token{keyword_arguments}}
\production{positional_arguments}
{\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
affect the semantics.
@ -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
syntax is:
\begin{verbatim}
power: primary ["**" u_expr]
\end{verbatim}
\begin{productionlist}
\production{power}
{\token{primary} ["**" \token{u_expr}]}
\end{productionlist}
Thus, in an unparenthesized sequence of power and unary operators, the
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
All unary arithmetic (and bit-wise) operations have the same priority:
\begin{verbatim}
u_expr: power | "-" u_expr | "+" u_expr | "~" u_expr
\end{verbatim}
\begin{productionlist}
\production{u_expr}
{\token{power} | "-" \token{u_expr}
| "+" \token{u_expr} | "~" \token{u_expr}}
\end{productionlist}
The unary \code{-} (minus) operator yields the negation of its
numeric argument.
@ -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
operators:
\begin{verbatim}
m_expr: u_expr | m_expr "*" u_expr
| m_expr "/" u_expr | m_expr "%" u_expr
a_expr: m_expr | aexpr "+" m_expr | aexpr "-" m_expr
\end{verbatim}
\begin{productionlist}
\production{m_expr}
{\token{u_expr} | \token{m_expr} "*" \token{u_expr}
| \token{m_expr} "/" \token{u_expr}
| \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
arguments. The arguments must either both be numbers, or one argument
@ -646,9 +692,11 @@ type.
The shifting operations have lower priority than the arithmetic
operations:
\begin{verbatim}
shift_expr: a_expr | shift_expr ( "<<" | ">>" ) a_expr
\end{verbatim}
\begin{productionlist}
\production{shift_expr}
{\token{a_expr}
| \token{shift_expr} ( "<<" | ">>" ) \token{a_expr}}
\end{productionlist}
These operators accept plain or long integers as arguments. The
arguments are converted to a common type. They shift the first
@ -670,11 +718,14 @@ exception.
Each of the three bitwise operations has a different priority level:
\begin{verbatim}
and_expr: shift_expr | and_expr "&" shift_expr
xor_expr: and_expr | xor_expr "^" and_expr
or_expr: xor_expr | or_expr "|" xor_expr
\end{verbatim}
\begin{productionlist}
\production{and_expr}
{\token{shift_expr} | \token{and_expr} "\&" \token{shift_expr}}
\production{xor_expr}
{\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
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
interpretation that is conventional in mathematics:
\indexii{C}{language}
\begin{verbatim}
comparison: or_expr (comp_operator or_expr)*
comp_operator: "<"|">"|"=="|">="|"<="|"<>"|"!="|"is" ["not"]|["not"] "in"
\end{verbatim}
\begin{productionlist}
\production{comparison}
{\token{or_expr} ( \token{comp_operator} \token{or_expr} )*}
\production{comp_operator}
{"<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"}
\end{productionlist}
Comparisons yield integer values: \code{1} for true, \code{0} for false.
@ -830,13 +884,18 @@ truth value.
Boolean operations have the lowest priority of all Python operations:
\begin{verbatim}
expression: or_test | lambda_form
or_test: and_test | or_test "or" and_test
and_test: not_test | and_test "and" not_test
not_test: comparison | "not" not_test
lambda_form: "lambda" [parameter_list]: expression
\end{verbatim}
\begin{productionlist}
\production{expression}
{\token{or_test} | \token{lambda_form}}
\production{or_test}
{\token{and_test} | \token{or_test} "or" \token{and_test}}
\production{and_test}
{\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
used by control flow statements, the following values are interpreted
@ -914,9 +973,10 @@ def make_incrementor(increment):
\section{Expression lists\label{exprlists}}
\indexii{expression}{list}
\begin{verbatim}
expression_list: expression ("," expression)* [","]
\end{verbatim}
\begin{productionlist}
\production{expression_list}
{\token{expression} ( "," \token{expression} )* [","]}
\end{productionlist}
An expression list containing at least one comma yields a
tuple. The length of the tuple is the number of expressions in the

View File

@ -5,22 +5,23 @@ Simple statements are comprised within a single logical line.
Several simple statements may occur on a single line separated
by semicolons. The syntax for simple statements is:
\begin{verbatim}
simple_stmt: expression_stmt
| assert_stmt
| assignment_stmt
| augmented_assignment_stmt
| pass_stmt
| del_stmt
| print_stmt
| return_stmt
| raise_stmt
| break_stmt
| continue_stmt
| import_stmt
| global_stmt
| exec_stmt
\end{verbatim}
\begin{productionlist}
\production{simple_stmt}
{\token{expression_stmt}
| \token{assert_stmt}
| \token{assignment_stmt}
| \token{augmented_assignment_stmt}
| \token{pass_stmt}
| \token{del_stmt}
| \token{print_stmt}
| \token{return_stmt}
| \token{raise_stmt}
| \token{break_stmt}
| \token{continue_stmt}
| \token{import_stmt}
| \token{global_stmt}
| \token{exec_stmt}}
\end{productionlist}
\section{Expression statements \label{exprstmts}}
@ -32,9 +33,10 @@ returns no meaningful result; in Python, procedures return the value
\code{None}). Other uses of expression statements are allowed and
occasionally useful. The syntax for an expression statement is:
\begin{verbatim}
expression_stmt: expression_list
\end{verbatim}
\begin{productionlist}
\production{expression_stmt}
{\token{expression_list}}
\end{productionlist}
An expression statement evaluates the expression list (which may be a
single expression).
@ -59,9 +61,10 @@ any output.)
Assert statements\stindex{assert} are a convenient way to insert
debugging assertions\indexii{debugging}{assertions} into a program:
\begin{verbatim}
assert_statement: "assert" expression ["," expression]
\end{verbatim}
\begin{productionlist}
\production{assert_statement}
{"assert" \token{expression} ["," \token{expression}]}
\end{productionlist}
The simple form, \samp{assert expression}, is equivalent to
@ -102,12 +105,19 @@ objects:
\obindex{mutable}
\indexii{attribute}{assignment}
\begin{verbatim}
assignment_stmt: (target_list "=")+ expression_list
target_list: target ("," target)* [","]
target: identifier | "(" target_list ")" | "[" target_list "]"
| attributeref | subscription | slicing
\end{verbatim}
\begin{productionlist}
\production{assignment_stmt}
{(\token{target_list} "=")+ \token{expression_list}}
\production{target_list}
{\token{target} ("," \token{target})* [","]}
\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
three symbols.)
@ -260,13 +270,20 @@ operation and an assignment statement:
\indexii{augmented}{assignment}
\index{statement!assignment, augmented}
\begin{verbatim}
augmented_assignment_stmt: target augop expression_list
augop: "+=" | "-=" | "*=" | "/=" | "%=" | "**="
| ">>=" | "<<=" | "&=" | "^=" | "|="
target: identifier | "(" target_list ")" | "[" target_list "]"
| attributeref | subscription | slicing
\end{verbatim}
\begin{productionlist}
\production{augmented_assignment_stmt}
{\token{target} \token{augop} \token{expression_list}}
\production{augop}
{"+=" | "-=" | "*=" | "/=" | "\%=" | "**="
| ">>=" | "<<=" | "\&=" | "\textasciicircum=" | "|="}
\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
three symbols.)
@ -294,9 +311,10 @@ augmented assignment is the same as the normal binary operations.
\section{The \keyword{pass} statement \label{pass}}
\stindex{pass}
\begin{verbatim}
pass_stmt: "pass"
\end{verbatim}
\begin{productionlist}
\production{pass_stmt}
{"pass"}
\end{productionlist}
\keyword{pass} is a null operation --- when it is executed, nothing
happens. It is useful as a placeholder when a statement is
@ -313,9 +331,10 @@ class C: pass # a class with no methods (yet)
\section{The \keyword{del} statement \label{del}}
\stindex{del}
\begin{verbatim}
del_stmt: "del" target_list
\end{verbatim}
\begin{productionlist}
\production{del_stmt}
{"del" \token{target_list}}
\end{productionlist}
Deletion is recursively defined very similar to the way assignment is
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).
\section{The \keyword{print} statement \label{print}}
\stindex{print}
\begin{verbatim}
print_stmt: "print" [ expression ("," expression)* [","] ]
\end{verbatim}
\begin{productionlist}
\production{print_stmt}
{"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
resulting object to standard output (see below). If an object is not
@ -376,13 +398,9 @@ exception is raised.
\withsubitem{(in module sys)}{\ttindex{stdout}}
\exindex{RuntimeError}
\keyword{print} also has an extended form, defined as
\index{extended print statement}
\begin{verbatim}
print_stmt: "print" ">>" expression [ ("," expression)+ [","] ]
\end{verbatim}
\keyword{print} also has an extended\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.''
In this form, the first expression after the \code{>}\code{>} must
evaluate to a ``file-like'' object, specifically an object that has a
\method{write()} method as described above. With this extended form,
@ -394,9 +412,10 @@ used as the file for output.
\section{The \keyword{return} statement \label{return}}
\stindex{return}
\begin{verbatim}
return_stmt: "return" [expression_list]
\end{verbatim}
\begin{productionlist}
\production{return_stmt}
{"return" [\token{expression_list}]}
\end{productionlist}
\keyword{return} may only occur syntactically nested in a function
definition, not within a nested class definition.
@ -418,9 +437,11 @@ before really leaving the function.
\section{The \keyword{raise} statement \label{raise}}
\stindex{raise}
\begin{verbatim}
raise_stmt: "raise" [expression ["," expression ["," expression]]]
\end{verbatim}
\begin{productionlist}
\production{raise_stmt}
{"raise" [\token{expression} ["," \token{expression}
["," \token{expression}]]]}
\end{productionlist}
If no expressions are present, \keyword{raise} re-raises the last
expression that was raised in the current scope.
@ -459,9 +480,10 @@ transparently in an except clause.
\section{The \keyword{break} statement \label{break}}
\stindex{break}
\begin{verbatim}
break_stmt: "break"
\end{verbatim}
\begin{productionlist}
\production{break_stmt}
{"break"}
\end{productionlist}
\keyword{break} may only occur syntactically nested in a \keyword{for}
or \keyword{while} loop, but not nested in a function or class definition
@ -487,9 +509,10 @@ before really leaving the loop.
\section{The \keyword{continue} statement \label{continue}}
\stindex{continue}
\begin{verbatim}
continue_stmt: "continue"
\end{verbatim}
\begin{productionlist}
\production{continue_stmt}
{"continue"}
\end{productionlist}
\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
@ -507,13 +530,17 @@ It continues with the next cycle of the nearest enclosing loop.
\section{The \keyword{import} statement \label{import}}
\stindex{import}
\begin{verbatim}
import_stmt: "import" module ["as" name] ("," module ["as" name] )*
| "from" module "import" identifier ["as" name]
("," identifier ["as" name] )*
| "from" module "import" "*"
module: (identifier ".")* identifier
\end{verbatim}
\begin{productionlist}
\production{import_stmt}
{"import" \token{module} ["as" \token{name}]
( "," \token{module} ["as" \token{name}] )*
| "from" \token{module} "import" \token{identifier}
["as" \token{name}]
( "," \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
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.]
\section{The \keyword{global} statement \label{global}}
\stindex{global}
\begin{verbatim}
global_stmt: "global" identifier ("," identifier)*
\end{verbatim}
\begin{productionlist}
\production{global_stmt}
{"global" \token{identifier} ("," \token{identifier})*}
\end{productionlist}
The \keyword{global} statement is a declaration which holds for the
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
\section{The \keyword{exec} statement \label{exec}}
\stindex{exec}
\begin{verbatim}
exec_stmt: "exec" expression ["in" expression ["," expression]]
\end{verbatim}
\begin{productionlist}
\production{exec_stmt}
{"exec" \token{expression}
["in" \token{expression} ["," \token{expression}]]}
\end{productionlist}
This statement supports dynamic execution of Python code. The first
expression should evaluate to either a string, an open file object, or

View File

@ -40,13 +40,18 @@ if x < y < z: print x; print y; print z
Summarizing:
\begin{verbatim}
compound_stmt: if_stmt | while_stmt | for_stmt
| try_stmt | funcdef | classdef
suite: stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement: stmt_list NEWLINE | compound_stmt
stmt_list: simple_stmt (";" simple_stmt)* [";"]
\end{verbatim}
\begin{productionlist}
\production{compound_stmt}
{\token{if_stmt} | \token{while_stmt} | \token{for_stmt}
| \token{try_stmt} | \token{funcdef} | \token{classdef}}
\production{suite}
{\token{stmt_list} NEWLINE
| 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
\code{NEWLINE}\index{NEWLINE token} possibly followed by a
@ -66,11 +71,12 @@ each clause on a separate line for clarity.
The \keyword{if} statement is used for conditional execution:
\begin{verbatim}
if_stmt: "if" expression ":" suite
("elif" expression ":" suite)*
["else" ":" suite]
\end{verbatim}
\begin{productionlist}
\production{if_stmt}
{"if" \token{expression} ":" \token{suite}
( "elif" \token{expression} ":" \token{suite} )*
["else" ":" \token{suite}]}
\end{productionlist}
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
@ -89,10 +95,11 @@ present, is executed.
The \keyword{while} statement is used for repeated execution as long
as an expression is true:
\begin{verbatim}
while_stmt: "while" expression ":" suite
["else" ":" suite]
\end{verbatim}
\begin{productionlist}
\production{while_stmt}
{"while" \token{expression} ":" \token{suite}
["else" ":" \token{suite}]}
\end{productionlist}
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
@ -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:
\obindex{sequence}
\begin{verbatim}
for_stmt: "for" target_list "in" expression_list ":" suite
["else" ":" suite]
\end{verbatim}
\begin{productionlist}
\production{for_stmt}
{"for" \token{target_list} "in" \token{expression_list}
":" \token{suite}
["else" ":" \token{suite}]}
\end{productionlist}
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
@ -179,14 +188,18 @@ for x in a[:]:
The \keyword{try} statement specifies exception handlers and/or cleanup
code for a group of statements:
\begin{verbatim}
try_stmt: try_exc_stmt | try_fin_stmt
try_exc_stmt: "try" ":" suite
("except" [expression ["," target]] ":" suite)+
["else" ":" suite]
try_fin_stmt: "try" ":" suite
"finally" ":" suite
\end{verbatim}
\begin{productionlist}
\production{try_stmt}
{\token{try_exc_stmt} | \token{try_fin_stmt}}
\production{try_exc_stmt}
{"try" ":" \token{suite}
("except" [\token{expression} ["," \token{target}]] ":"
\token{suite})+
["else" ":" \token{suite}]}
\production{try_fin_stmt}
{"try" ":" \token{suite}
"finally" ":" \token{suite}}
\end{productionlist}
There are two forms of \keyword{try} statement:
\keyword{try}...\keyword{except} and
@ -291,16 +304,24 @@ section \ref{types}):
\obindex{user-defined function}
\obindex{function}
\begin{verbatim}
funcdef: "def" funcname "(" [parameter_list] ")" ":" suite
parameter_list: (defparameter ",")* ("*" identifier [, "**" identifier]
| "**" identifier
| defparameter [","])
defparameter: parameter ["=" expression]
sublist: parameter ("," parameter)* [","]
parameter: identifier | "(" sublist ")"
funcname: identifier
\end{verbatim}
\begin{productionlist}
\production{funcdef}
{"def" \token{funcname} "(" [\token{parameter_list}] ")"
":" \token{suite}}
\production{parameter_list}
{(\token{defparameter} ",")*
("*" \token{identifier} [, "**" \token{identifier}]
| "**" \token{identifier}
| \token{defparameter} [","])}
\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
the function name in the current local namespace to a function object
@ -376,11 +397,15 @@ description of the new semantics.
A class definition defines a class object (see section \ref{types}):
\obindex{class}
\begin{verbatim}
classdef: "class" classname [inheritance] ":" suite
inheritance: "(" [expression_list] ")"
classname: identifier
\end{verbatim}
\begin{productionlist}
\production{classdef}
{"class" \token{classname} [\token{inheritance}] ":"
\token{suite}}
\production{inheritance}
{"(" [\token{expression_list}] ")"}
\production{classname}
{\token{identifier}}
\end{productionlist}
A class definition is an executable statement. It first evaluates the
inheritance list, if present. Each item in the inheritance list

View File

@ -49,9 +49,10 @@ program.
All input read from non-interactive files has the same form:
\begin{verbatim}
file_input: (NEWLINE | statement)*
\end{verbatim}
\begin{productionlist}
\production{file_input}
{(NEWLINE | \token{statement})*}
\end{productionlist}
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:
\begin{verbatim}
interactive_input: [stmt_list] NEWLINE | compound_stmt NEWLINE
\end{verbatim}
\begin{productionlist}
\production{interactive_input}
{[\token{stmt_list}] NEWLINE | \token{compound_stmt} NEWLINE}
\end{productionlist}
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
@ -87,16 +89,18 @@ whitespace.
The string argument to \function{eval()} must have the following form:
\bifuncindex{eval}
\begin{verbatim}
eval_input: expression_list NEWLINE*
\end{verbatim}
\begin{productionlist}
\production{eval_input}
{\token{expression_list} NEWLINE*}
\end{productionlist}
The input line read by \function{input()} must have the following form:
\bifuncindex{input}
\begin{verbatim}
input_input: expression_list NEWLINE
\end{verbatim}
\begin{productionlist}
\production{input_input}
{\token{expression_list} NEWLINE}
\end{productionlist}
Note: to read `raw' input line without interpretation, you can use the
built-in function \function{raw_input()} or the \method{readline()} method