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