1 \chapter{Lexical analysis
\label{lexical
}}
3 A Python program is read by a
\emph{parser
}. Input to the parser is a
4 stream of
\emph{tokens
}, generated by the
\emph{lexical analyzer
}. This
5 chapter describes how the lexical analyzer breaks a file into tokens.
6 \index{lexical analysis
}
10 Python uses the
7-bit
\ASCII{} character set for program text and string
11 literals.
8-bit characters may be used in string literals and comments
12 but their interpretation is platform dependent; the proper way to
13 insert
8-bit characters in string literals is by using octal or
14 hexadecimal escape sequences.
16 The run-time character set depends on the I/O devices connected to the
17 program but is generally a superset of
\ASCII.
19 \strong{Future compatibility note:
} It may be tempting to assume that the
20 character set for
8-bit characters is ISO Latin-
1 (an
\ASCII{}
21 superset that covers most western languages that use the Latin
22 alphabet), but it is possible that in the future Unicode text editors
23 will become common. These generally use the UTF-
8 encoding, which is
24 also an
\ASCII{} superset, but with very different use for the
25 characters with ordinals
128-
255. While there is no consensus on this
26 subject yet, it is unwise to assume either Latin-
1 or UTF-
8, even
27 though the current implementation appears to favor Latin-
1. This
28 applies both to the source character set and the run-time character
32 \section{Line structure
\label{line-structure
}}
34 A Python program is divided into a number of
\emph{logical lines
}.
35 \index{line structure
}
38 \subsection{Logical lines
\label{logical
}}
41 a logical line is represented by the token NEWLINE. Statements cannot
42 cross logical line boundaries except where NEWLINE is allowed by the
43 syntax (e.g., between statements in compound statements).
44 A logical line is constructed from one or more
\emph{physical lines
}
45 by following the explicit or implicit
\emph{line joining
} rules.
52 \subsection{Physical lines
\label{physical
}}
54 A physical line ends in whatever the current platform's convention is
55 for terminating lines. On
\UNIX, this is the
\ASCII{} LF (linefeed)
56 character. On DOS/Windows, it is the
\ASCII{} sequence CR LF (return
57 followed by linefeed). On Macintosh, it is the
\ASCII{} CR (return)
61 \subsection{Comments
\label{comments
}}
63 A comment starts with a hash character (
\code{\#
}) that is not part of
64 a string literal, and ends at the end of the physical line. A comment
65 signifies the end of the logical line unless the implicit line joining
67 Comments are ignored by the syntax; they are not tokens.
69 \index{hash character
}
72 \subsection{Explicit line joining
\label{explicit-joining
}}
74 Two or more physical lines may be joined into logical lines using
75 backslash characters (
\code{\e}), as follows: when a physical line ends
76 in a backslash that is not part of a string literal or comment, it is
77 joined with the following forming a single logical line, deleting the
78 backslash and the following end-of-line character. For example:
81 \index{line continuation
}
82 \index{backslash character
}
85 if
1900 < year <
2100 and
1 <= month <=
12 \
86 and
1 <= day <=
31 and
0 <= hour <
24 \
87 and
0 <= minute <
60 and
0 <= second <
60: # Looks like a valid date
91 A line ending in a backslash cannot carry a comment. A backslash does
92 not continue a comment. A backslash does not continue a token except
93 for string literals (i.e., tokens other than string literals cannot be
94 split across physical lines using a backslash). A backslash is
95 illegal elsewhere on a line outside a string literal.
98 \subsection{Implicit line joining
\label{implicit-joining
}}
100 Expressions in parentheses, square brackets or curly braces can be
101 split over more than one physical line without using backslashes.
105 month_names =
['Januari', 'Februari', 'Maart', # These are the
106 'April', 'Mei', 'Juni', # Dutch names
107 'Juli', 'Augustus', 'September', # for the months
108 'Oktober', 'November', 'December'
] # of the year
111 Implicitly continued lines can carry comments. The indentation of the
112 continuation lines is not important. Blank continuation lines are
113 allowed. There is no NEWLINE token between implicit continuation
114 lines. Implicitly continued lines can also occur within triple-quoted
115 strings (see below); in that case they cannot carry comments.
118 \subsection{Blank lines
\index{blank line
}\label{blank-lines
}}
120 A logical line that contains only spaces, tabs, formfeeds and possibly
121 a comment, is ignored (i.e., no NEWLINE token is generated). During
122 interactive input of statements, handling of a blank line may differ
123 depending on the implementation of the read-eval-print loop. In the
124 standard implementation, an entirely blank logical line (i.e.\ one
125 containing not even whitespace or a comment) terminates a multi-line
129 \subsection{Indentation
\label{indentation
}}
131 Leading whitespace (spaces and tabs) at the beginning of a logical
132 line is used to compute the indentation level of the line, which in
133 turn is used to determine the grouping of statements.
136 \index{leading whitespace
}
140 \index{statement grouping
}
142 First, tabs are replaced (from left to right) by one to eight spaces
143 such that the total number of characters up to and including the
144 replacement is a multiple of
145 eight (this is intended to be the same rule as used by
\UNIX). The
146 total number of spaces preceding the first non-blank character then
147 determines the line's indentation. Indentation cannot be split over
148 multiple physical lines using backslashes; the whitespace up to the
149 first backslash determines the indentation.
151 \strong{Cross-platform compatibility note:
} because of the nature of
152 text editors on non-UNIX platforms, it is unwise to use a mixture of
153 spaces and tabs for the indentation in a single source file.
155 A formfeed character may be present at the start of the line; it will
156 be ignored for the indentation calculations above. Formfeed
157 characters occurring elsewhere in the leading whitespace have an
158 undefined effect (for instance, they may reset the space count to
161 The indentation levels of consecutive lines are used to generate
162 INDENT and DEDENT tokens, using a stack, as follows.
166 Before the first line of the file is read, a single zero is pushed on
167 the stack; this will never be popped off again. The numbers pushed on
168 the stack will always be strictly increasing from bottom to top. At
169 the beginning of each logical line, the line's indentation level is
170 compared to the top of the stack. If it is equal, nothing happens.
171 If it is larger, it is pushed on the stack, and one INDENT token is
172 generated. If it is smaller, it
\emph{must
} be one of the numbers
173 occurring on the stack; all numbers on the stack that are larger are
174 popped off, and for each number popped off a DEDENT token is
175 generated. At the end of the file, a DEDENT token is generated for
176 each number remaining on the stack that is larger than zero.
178 Here is an example of a correctly (though confusingly) indented piece
183 # Compute the list of all permutations of l
187 for i in range(len(l)):
191 r.append(l
[i:i+
1] + x)
195 The following example shows various indentation errors:
198 def perm(l): # error: first line indented
199 for i in range(len(l)): # error: not indented
201 p = perm(l
[:i
] + l
[i+
1:
]) # error: unexpected indent
203 r.append(l
[i:i+
1] + x)
204 return r # error: inconsistent dedent
207 (Actually, the first three errors are detected by the parser; only the
208 last error is found by the lexical analyzer --- the indentation of
209 \code{return r
} does not match a level popped off the stack.)
212 \subsection{Whitespace between tokens
\label{whitespace
}}
214 Except at the beginning of a logical line or in string literals, the
215 whitespace characters space, tab and formfeed can be used
216 interchangeably to separate tokens. Whitespace is needed between two
217 tokens only if their concatenation could otherwise be interpreted as a
218 different token (e.g., ab is one token, but a b is two tokens).
221 \section{Other tokens
\label{other-tokens
}}
223 Besides NEWLINE, INDENT and DEDENT, the following categories of tokens
224 exist:
\emph{identifiers
},
\emph{keywords
},
\emph{literals
},
225 \emph{operators
}, and
\emph{delimiters
}.
226 Whitespace characters (other than line terminators, discussed earlier)
227 are not tokens, but serve to delimit tokens.
229 ambiguity exists, a token comprises the longest possible string that
230 forms a legal token, when read from left to right.
233 \section{Identifiers and keywords
\label{identifiers
}}
235 Identifiers (also referred to as
\emph{names
}) are described by the following
240 \begin{productionlist
}
241 \production{identifier
}
242 {(
\token{letter
}|"_") (
\token{letter
} |
\token{digit
} | "_")*
}
244 {\token{lowercase
} |
\token{uppercase
}}
245 \production{lowercase
}
247 \production{uppercase
}
253 Identifiers are unlimited in length. Case is significant.
256 \subsection{Keywords
\label{keywords
}}
258 The following identifiers are used as reserved words, or
259 \emph{keywords
} of the language, and cannot be used as ordinary
260 identifiers. They must be spelled exactly as written here:
%
262 \index{reserved word
}
266 assert elif from lambda return
267 break else global not try
268 class except if or while
269 continue exec import pass yield
273 % When adding keywords, use reswords.py for reformatting
276 \subsection{Reserved classes of identifiers
\label{id-classes
}}
278 Certain classes of identifiers (besides keywords) have special
281 \begin{tableiii
}{l|l|l
}{code
}{Form
}{Meaning
}{Notes
}
282 \lineiii{_*
}{Not imported by
\samp{from
\var{module
} import *
}}{(
1)
}
283 \lineiii{__*__
}{System-defined name
}{}
284 \lineiii{__*
}{Class-private name mangling
}{}
287 (XXX need section references here.)
292 \item[(
1)
] The special identifier
\samp{_
} is used in the interactive
293 interpreter to store the result of the last evaluation; it is stored
294 in the
\module{__builtin__
} module. When not in interactive mode,
295 \samp{_
} has no special meaning and is not defined.
299 \section{Literals
\label{literals
}}
301 Literals are notations for constant values of some built-in types.
306 \subsection{String literals
\label{strings
}}
308 String literals are described by the following lexical definitions:
309 \index{string literal
}
312 \begin{productionlist
}
313 \production{stringliteral
}
314 {[\token{stringprefix
}](
\token{shortstring
} |
\token{longstring
})
}
315 \production{stringprefix
}
316 {"r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR"
}
317 \production{shortstring
}
318 {"'"
\token{shortstringitem
}* "'"
319 | '"'
\token{shortstringitem
}* '"'
}
320 \production{longstring
}
321 {"'''"
\token{longstringitem
}* "'''"
322 | '"""'
\token{longstringitem
}* '"""'
}
323 \production{shortstringitem
}
324 {\token{shortstringchar
} |
\token{escapeseq
}}
325 \production{longstringitem
}
326 {\token{longstringchar
} |
\token{escapeseq
}}
327 \production{shortstringchar
}
328 {<any ASCII character except "
\e" or newline or the quote>
}
329 \production{longstringchar
}
330 {<any ASCII characteru except "
\e">
}
331 \production{escapeseq
}
332 {"
\e" <any ASCII character>
}
335 One syntactic restriction not indicated by these productions is that
336 whitespace is not allowed between the
\grammartoken{stringprefix
} and
337 the rest of the string literal.
339 \index{triple-quoted string
}
340 \index{Unicode Consortium
}
341 \index{string!Unicode
}
342 In plain English: String literals can be enclosed in matching single
343 quotes (
\code{'
}) or double quotes (
\code{"
}). They can also be
344 enclosed in matching groups of three single or double quotes (these
345 are generally referred to as
\emph{triple-quoted strings
}). The
346 backslash (
\code{\e}) character is used to escape characters that
347 otherwise have a special meaning, such as newline, backslash itself,
348 or the quote character. String literals may optionally be prefixed
349 with a letter `r' or `R'; such strings are called
\dfn{raw
350 strings
}\index{raw string
} and use different rules for interpreting
351 backslash escape sequences. A prefix of 'u' or 'U' makes the string
352 a Unicode string. Unicode strings use the Unicode character set as
353 defined by the Unicode Consortium and ISO~
10646. Some additional
354 escape sequences, described below, are available in Unicode strings.
355 The two prefix characters may be combined; in this case, `u' must
358 In triple-quoted strings,
359 unescaped newlines and quotes are allowed (and are retained), except
360 that three unescaped quotes in a row terminate the string. (A
361 ``quote'' is the character used to open the string, i.e. either
362 \code{'
} or
\code{"
}.)
364 Unless an `r' or `R' prefix is present, escape sequences in strings
365 are interpreted according to rules similar
366 to those used by Standard C. The recognized escape sequences are:
367 \index{physical line
}
368 \index{escape sequence
}
372 \begin{tableii
}{l|l
}{code
}{Escape Sequence
}{Meaning
}
373 \lineii{\e\var{newline
}} {Ignored
}
374 \lineii{\e\e} {Backslash (
\code{\e})
}
375 \lineii{\e'
} {Single quote (
\code{'
})
}
376 \lineii{\e"
} {Double quote (
\code{"
})
}
377 \lineii{\e a
} {\ASCII{} Bell (BEL)
}
378 \lineii{\e b
} {\ASCII{} Backspace (BS)
}
379 \lineii{\e f
} {\ASCII{} Formfeed (FF)
}
380 \lineii{\e n
} {\ASCII{} Linefeed (LF)
}
381 \lineii{\e N\
{\var{name
}\
}}
382 {Character named
\var{name
} in the Unicode database (Unicode only)
}
383 \lineii{\e r
} {\ASCII{} Carriage Return (CR)
}
384 \lineii{\e t
} {\ASCII{} Horizontal Tab (TAB)
}
385 \lineii{\e u
\var{xxxx
}} {Character with
16-bit hex value
\var{xxxx
} (Unicode only)
}
386 \lineii{\e U
\var{xxxxxxxx
}}{Character with
32-bit hex value
\var{xxxxxxxx
} (Unicode only)
}
387 \lineii{\e v
} {\ASCII{} Vertical Tab (VT)
}
388 \lineii{\e\var{ooo
}} {\ASCII{} character with octal value
\var{ooo
}}
389 \lineii{\e x
\var{hh
}} {\ASCII{} character with hex value
\var{hh
}}
393 As in Standard C, up to three octal digits are accepted. However,
394 exactly two hex digits are taken in hex escapes.
396 Unlike Standard
\index{unrecognized escape sequence
}C,
397 all unrecognized escape sequences are left in the string unchanged,
398 i.e.,
\emph{the backslash is left in the string
}. (This behavior is
399 useful when debugging: if an escape sequence is mistyped, the
400 resulting output is more easily recognized as broken.) It is also
401 important to note that the escape sequences marked as ``(Unicode
402 only)'' in the table above fall into the category of unrecognized
403 escapes for non-Unicode string literals.
405 When an `r' or `R' prefix is present, a character following a
406 backslash is included in the string without change, and
\emph{all
407 backslashes are left in the string
}. For example, the string literal
408 \code{r"
\e n"
} consists of two characters: a backslash and a lowercase
409 `n'. String quotes can be escaped with a backslash, but the backslash
410 remains in the string; for example,
\code{r"
\e""
} is a valid string
411 literal consisting of two characters: a backslash and a double quote;
412 \code{r"
\e"
} is not a valid string literal (even a raw string cannot
413 end in an odd number of backslashes). Specifically,
\emph{a raw
414 string cannot end in a single backslash
} (since the backslash would
415 escape the following quote character). Note also that a single
416 backslash followed by a newline is interpreted as those two characters
417 as part of the string,
\emph{not
} as a line continuation.
420 \subsection{String literal concatenation
\label{string-catenation
}}
422 Multiple adjacent string literals (delimited by whitespace), possibly
423 using different quoting conventions, are allowed, and their meaning is
424 the same as their concatenation. Thus,
\code{"hello" 'world'
} is
425 equivalent to
\code{"helloworld"
}. This feature can be used to reduce
426 the number of backslashes needed, to split long strings conveniently
427 across long lines, or even to add comments to parts of strings, for
431 re.compile("
[A-Za-z_
]" # letter or underscore
432 "
[A-Za-z0-
9_
]*" # letter, digit or underscore
436 Note that this feature is defined at the syntactical level, but
437 implemented at compile time. The `+' operator must be used to
438 concatenate string expressions at run time. Also note that literal
439 concatenation can use different quoting styles for each component
440 (even mixing raw strings and triple quoted strings).
443 \subsection{Numeric literals
\label{numbers
}}
445 There are four types of numeric literals: plain integers, long
446 integers, floating point numbers, and imaginary numbers. There are no
447 complex literals (complex numbers can be formed by adding a real
448 number and an imaginary number).
450 \index{numeric literal
}
451 \index{integer literal
}
452 \index{plain integer literal
}
453 \index{long integer literal
}
454 \index{floating point literal
}
455 \index{hexadecimal literal
}
456 \index{octal literal
}
457 \index{decimal literal
}
458 \index{imaginary literal
}
459 \index{complex literal
}
461 Note that numeric literals do not include a sign; a phrase like
462 \code{-
1} is actually an expression composed of the unary operator
463 `
\code{-
}' and the literal
\code{1}.
466 \subsection{Integer and long integer literals
\label{integers
}}
468 Integer and long integer literals are described by the following
471 \begin{productionlist
}
472 \production{longinteger
}
473 {\token{integer
} ("l" | "L")
}
475 {\token{decimalinteger
} |
\token{octinteger
} |
\token{hexinteger
}}
476 \production{decimalinteger
}
477 {\token{nonzerodigit
} \token{digit
}* | "
0"
}
478 \production{octinteger
}
479 {"
0"
\token{octdigit
}+
}
480 \production{hexinteger
}
481 {"
0" ("x" | "X")
\token{hexdigit
}+
}
482 \production{nonzerodigit
}
484 \production{octdigit
}
486 \production{hexdigit
}
487 {\token{digit
} | "a"..."f" | "A"..."F"
}
490 Although both lower case `l' and upper case `L' are allowed as suffix
491 for long integers, it is strongly recommended to always use `L', since
492 the letter `l' looks too much like the digit `
1'.
494 Plain integer decimal literals must be at most
2147483647 (i.e., the
495 largest positive integer, using
32-bit arithmetic). Plain octal and
496 hexadecimal literals may be as large as
4294967295, but values larger
497 than
2147483647 are converted to a negative value by subtracting
498 4294967296. There is no limit for long integer literals apart from
499 what can be stored in available memory.
501 Some examples of plain and long integer literals:
504 7 2147483647 0177 0x80000000
505 3L 79228162514264337593543950336L 0377L 0x100000000L
509 \subsection{Floating point literals
\label{floating
}}
511 Floating point literals are described by the following lexical
514 \begin{productionlist
}
515 \production{floatnumber
}
516 {\token{pointfloat
} |
\token{exponentfloat
}}
517 \production{pointfloat
}
518 {[\token{intpart
}] \token{fraction
} |
\token{intpart
} "."
}
519 \production{exponentfloat
}
520 {(
\token{intpart
} |
\token{pointfloat
})
524 \production{fraction
}
526 \production{exponent
}
527 {("e" | "E")
["+" | "-"
] \token{digit
}+
}
530 Note that the integer and exponent parts of floating point numbers
531 can look like octal integers, but are interpreted using radix
10. For
532 example,
\samp{077e010
} is legal, and denotes the same number
534 The allowed range of floating point literals is
535 implementation-dependent.
536 Some examples of floating point literals:
539 3.14 10.
.001 1e100
3.14e-10 0e0
542 Note that numeric literals do not include a sign; a phrase like
543 \code{-
1} is actually an expression composed of the operator
544 \code{-
} and the literal
\code{1}.
547 \subsection{Imaginary literals
\label{imaginary
}}
549 Imaginary literals are described by the following lexical definitions:
551 \begin{productionlist
}
552 \production{imagnumber
}{(
\token{floatnumber
} |
\token{intpart
}) ("j" | "J")
}
555 An imaginary literal yields a complex number with a real part of
556 0.0. Complex numbers are represented as a pair of floating point
557 numbers and have the same restrictions on their range. To create a
558 complex number with a nonzero real part, add a floating point number
559 to it, e.g.,
\code{(
3+
4j)
}. Some examples of imaginary literals:
562 3.14j
10.j
10j
.001j
1e100j
3.14e-10j
566 \section{Operators
\label{operators
}}
568 The following tokens are operators:
577 The comparison operators
\code{<>
} and
\code{!=
} are alternate
578 spellings of the same operator.
\code{!=
} is the preferred spelling;
579 \code{<>
} is obsolescent.
582 \section{Delimiters
\label{delimiters
}}
584 The following tokens serve as delimiters in the grammar:
594 The period can also occur in floating-point and imaginary literals. A
595 sequence of three periods has a special meaning as an ellipsis in slices.
596 The second half of the list, the augmented assignment operators, serve
597 lexically as delimiters, but also perform an operation.
599 The following printing
\ASCII{} characters have special meaning as part
600 of other tokens or are otherwise significant to the lexical analyzer:
606 The following printing
\ASCII{} characters are not used in Python. Their
607 occurrence outside string literals and comments is an unconditional