blob: 23bc05d3771a34da9a2b0f18d91da17dfede7f4d [file] [log] [blame]
Fred Drake61c77281998-07-28 19:34:22 +00001\chapter{Introduction\label{introduction}}
Fred Drakef6669171998-05-06 19:52:49 +00002
3This reference manual describes the Python programming language.
4It is not intended as a tutorial.
5
6While I am trying to be as precise as possible, I chose to use English
7rather than formal specifications for everything except syntax and
8lexical analysis. This should make the document more understandable
9to the average reader, but will leave room for ambiguities.
10Consequently, if you were coming from Mars and tried to re-implement
11Python from this document alone, you might have to guess things and in
12fact you would probably end up implementing quite a different language.
13On the other hand, if you are using
14Python and wonder what the precise rules about a particular area of
15the language are, you should definitely be able to find them here.
Guido van Rossum0bd37951998-06-15 16:27:37 +000016If you would like to see a more formal definitition of the language,
17maybe you could volunteer your time --- or invent a cloning machine
18:-).
Fred Drakef6669171998-05-06 19:52:49 +000019
20It is dangerous to add too many implementation details to a language
21reference document --- the implementation may change, and other
22implementations of the same language may work differently. On the
Guido van Rossum0bd37951998-06-15 16:27:37 +000023other hand, there is currently only one Python implementation in
24widespread use (although a second one now exists!), and
Fred Drakef6669171998-05-06 19:52:49 +000025its particular quirks are sometimes worth being mentioned, especially
26where the implementation imposes additional limitations. Therefore,
27you'll find short ``implementation notes'' sprinkled throughout the
28text.
29
30Every Python implementation comes with a number of built-in and
31standard modules. These are not documented here, but in the separate
Fred Drake61c77281998-07-28 19:34:22 +000032\emph{Python Library Reference} document. A few built-in modules are
Fred Drakef6669171998-05-06 19:52:49 +000033mentioned when they interact in a significant way with the language
34definition.
35
Fred Drake61c77281998-07-28 19:34:22 +000036\section{Notation\label{notation}}
Fred Drakef6669171998-05-06 19:52:49 +000037
38The descriptions of lexical analysis and syntax use a modified BNF
39grammar notation. This uses the following style of definition:
40\index{BNF}
41\index{grammar}
42\index{syntax}
43\index{notation}
44
45\begin{verbatim}
46name: lc_letter (lc_letter | "_")*
47lc_letter: "a"..."z"
48\end{verbatim}
49
Fred Drake5c07d9b1998-05-14 19:37:06 +000050The first line says that a \code{name} is an \code{lc_letter} followed by
51a sequence of zero or more \code{lc_letter}s and underscores. An
52\code{lc_letter} in turn is any of the single characters \character{a}
53through \character{z}. (This rule is actually adhered to for the
54names defined in lexical and grammar rules in this document.)
Fred Drakef6669171998-05-06 19:52:49 +000055
56Each rule begins with a name (which is the name defined by the rule)
Fred Drake5c07d9b1998-05-14 19:37:06 +000057and a colon. A vertical bar (\code{|}) is used to separate
Fred Drakef6669171998-05-06 19:52:49 +000058alternatives; it is the least binding operator in this notation. A
Fred Drake5c07d9b1998-05-14 19:37:06 +000059star (\code{*}) means zero or more repetitions of the preceding item;
60likewise, a plus (\code{+}) means one or more repetitions, and a
61phrase enclosed in square brackets (\code{[ ]}) means zero or one
Fred Drakef6669171998-05-06 19:52:49 +000062occurrences (in other words, the enclosed phrase is optional). The
Fred Drake5c07d9b1998-05-14 19:37:06 +000063\code{*} and \code{+} operators bind as tightly as possible;
Fred Drakef6669171998-05-06 19:52:49 +000064parentheses are used for grouping. Literal strings are enclosed in
65quotes. White space is only meaningful to separate tokens.
66Rules are normally contained on a single line; rules with many
67alternatives may be formatted alternatively with each line after the
68first beginning with a vertical bar.
69
70In lexical definitions (as the example above), two more conventions
71are used: Two literal characters separated by three dots mean a choice
72of any single character in the given (inclusive) range of \ASCII{}
Fred Drake5c07d9b1998-05-14 19:37:06 +000073characters. A phrase between angular brackets (\code{<...>}) gives an
Guido van Rossum7c0240f1998-07-24 15:36:43 +000074informal description of the symbol defined; e.g., this could be used
Fred Drakef6669171998-05-06 19:52:49 +000075to describe the notion of `control character' if needed.
76\index{lexical definitions}
Fred Drake5c07d9b1998-05-14 19:37:06 +000077\index{ASCII@\ASCII{}}
Fred Drakef6669171998-05-06 19:52:49 +000078
79Even though the notation used is almost the same, there is a big
80difference between the meaning of lexical and syntactic definitions:
81a lexical definition operates on the individual characters of the
82input source, while a syntax definition operates on the stream of
83tokens generated by the lexical analysis. All uses of BNF in the next
84chapter (``Lexical Analysis'') are lexical definitions; uses in
85subsequent chapters are syntactic definitions.