blob: 30bfcce022b957acb15a538ccfad73ebe64d57a9 [file] [log] [blame]
Guido van Rossum46f3e001992-08-14 09:11:01 +00001\chapter{Introduction}
2
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
Guido van Rossum16d6e711994-08-08 12:30:22 +00008lexical analysis. This should make the document more understandable
Guido van Rossum46f3e001992-08-14 09:11:01 +00009to 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.
16
17It is dangerous to add too many implementation details to a language
18reference document --- the implementation may change, and other
19implementations of the same language may work differently. On the
20other hand, there is currently only one Python implementation, and
21its particular quirks are sometimes worth being mentioned, especially
22where the implementation imposes additional limitations. Therefore,
23you'll find short ``implementation notes'' sprinkled throughout the
24text.
25
26Every Python implementation comes with a number of built-in and
27standard modules. These are not documented here, but in the separate
28{\em Python Library Reference} document. A few built-in modules are
29mentioned when they interact in a significant way with the language
30definition.
31
32\section{Notation}
33
34The descriptions of lexical analysis and syntax use a modified BNF
35grammar notation. This uses the following style of definition:
36\index{BNF}
37\index{grammar}
38\index{syntax}
39\index{notation}
40
41\begin{verbatim}
42name: lc_letter (lc_letter | "_")*
43lc_letter: "a"..."z"
44\end{verbatim}
45
Guido van Rossum6938f061994-08-01 12:22:53 +000046The first line says that a \verb@name@ is an \verb@lc_letter@ followed by
47a sequence of zero or more \verb@lc_letter@s and underscores. An
48\verb@lc_letter@ in turn is any of the single characters `a' through `z'.
Guido van Rossum46f3e001992-08-14 09:11:01 +000049(This rule is actually adhered to for the names defined in lexical and
50grammar rules in this document.)
51
52Each rule begins with a name (which is the name defined by the rule)
Guido van Rossum6938f061994-08-01 12:22:53 +000053and a colon. A vertical bar (\verb@|@) is used to separate
Guido van Rossum46f3e001992-08-14 09:11:01 +000054alternatives; it is the least binding operator in this notation. A
Guido van Rossum6938f061994-08-01 12:22:53 +000055star (\verb@*@) means zero or more repetitions of the preceding item;
56likewise, a plus (\verb@+@) means one or more repetitions, and a
57phrase enclosed in square brackets (\verb@[ ]@) means zero or one
Guido van Rossum46f3e001992-08-14 09:11:01 +000058occurrences (in other words, the enclosed phrase is optional). The
Guido van Rossum6938f061994-08-01 12:22:53 +000059\verb@*@ and \verb@+@ operators bind as tightly as possible;
Guido van Rossum46f3e001992-08-14 09:11:01 +000060parentheses are used for grouping. Literal strings are enclosed in
Guido van Rossum6938f061994-08-01 12:22:53 +000061quotes. White space is only meaningful to separate tokens.
Guido van Rossum46f3e001992-08-14 09:11:01 +000062Rules are normally contained on a single line; rules with many
63alternatives may be formatted alternatively with each line after the
64first beginning with a vertical bar.
65
66In lexical definitions (as the example above), two more conventions
67are used: Two literal characters separated by three dots mean a choice
Guido van Rossum47b4c0f1995-03-15 11:25:32 +000068of any single character in the given (inclusive) range of \ASCII{}
Guido van Rossum6938f061994-08-01 12:22:53 +000069characters. A phrase between angular brackets (\verb@<...>@) gives an
Guido van Rossum46f3e001992-08-14 09:11:01 +000070informal description of the symbol defined; e.g. this could be used
71to describe the notion of `control character' if needed.
72\index{lexical definitions}
73\index{ASCII}
74
75Even though the notation used is almost the same, there is a big
76difference between the meaning of lexical and syntactic definitions:
77a lexical definition operates on the individual characters of the
78input source, while a syntax definition operates on the stream of
79tokens generated by the lexical analysis. All uses of BNF in the next
80chapter (``Lexical Analysis'') are lexical definitions; uses in
81subsequent chapters are syntactic definitions.