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
......
This diff is collapsed.
This diff is collapsed.
...@@ -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