Lexical analysis Flashcards

1
Q

Logical lines

A

The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Physical lines

A

A physical line is a sequence of characters terminated by an end-of-line sequence. In source files and strings, any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of platform. The end of input also serves as an implicit terminator for the final physical line.

When embedding Python, source code strings should be passed to Python APIs using the standard C conventions for newline characters (the \n character, representing ASCII LF, is the line terminator).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explicit line joining

A

Two or more physical lines may be joined into logical lines using backslash characters (), as follows: when a physical line ends in a backslash that is not part of a string literal or comment, it is joined with the following forming a single logical line, deleting the backslash and the following end-of-line character. For example:

A line ending in a backslash cannot carry a comment. A backslash does not continue a comment. A backslash does not continue a token except for string literals (i.e., tokens other than string literals cannot be split across physical lines using a backslash). A backslash is illegal elsewhere on a line outside a string literal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Implicit line joining

A

Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. For example:

Implicitly continued lines can carry comments. The indentation of the continuation lines is not important. Blank continuation lines are allowed. There is no NEWLINE token between implicit continuation lines. Implicitly continued lines can also occur within triple-quoted strings (see below); in that case they cannot carry comments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Indentation

A

The indentation levels of consecutive lines are used to generate INDENT and DEDENT tokens, using a stack, as follows.

Before the first line of the file is read, a single zero is pushed on the stack; this will never be popped off again. The numbers pushed on the stack will always be strictly increasing from bottom to top. At the beginning of each logical line, the line’s indentation level is compared to the top of the stack. If it is equal, nothing happens. If it is larger, it is pushed on the stack, and one INDENT token is generated. If it is smaller, it must be one of the numbers occurring on the stack; all numbers on the stack that are larger are popped off, and for each number popped off a DEDENT token is generated. At the end of the file, a DEDENT token is generated for each number remaining on the stack that is larger than zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Keywords

A

The following identifiers are used as reserved words, or keywords of the language, and cannot be used as ordinary identifiers. They must be spelled exactly as written here:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

String literal concatenation

A

Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, “hello”‘world’ is equivalent to “helloworld”. This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings, for example:

Note that this feature is defined at the syntactical level, but implemented at compile time. The ‘+’ operator must be used to concatenate string expressions at run time. Also note that literal concatenation can use different quoting styles for each component (even mixing raw strings and triple quoted strings), and formatted string literals may be concatenated with plain string literals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Formatted string literals

A

A formatted string literal or f-string is a string literal that is prefixed with ‘f’ or ‘F’. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.

The parts of the string outside curly braces are treated literally, except that any doubled curly braces ‘{{‘ or ‘}}’ are replaced with the corresponding single curly brace. A single opening curly bracket ‘{‘ marks a replacement field, which starts with a Python expression. After the expression, there may be a conversion field, introduced by an exclamation point ‘!’. A format specifier may also be appended, introduced by a colon ‘:’. A replacement field ends with a closing curly bracket ‘}’.

Expressions in formatted string literals are treated like regular Python expressions surrounded by parentheses, with a few exceptions. An empty expression is not allowed, and a lambda expression must be surrounded by explicit parentheses. Replacement expressions can contain line breaks (e.g. in triple-quoted strings), but they cannot contain comments. Each expression is evaluated in the context where the formatted string literal appears, in order from left to right.

If a conversion is specified, the result of evaluating the expression is converted before formatting. Conversion ‘!s’ calls str() on the result, ‘!r’ calls repr(), and ‘!a’ calls ascii().

Formatted string literals may be concatenated, but replacement fields cannot be split across literals.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Numeric literals

A

There are three types of numeric literals: integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number).

Note that numeric literals do not include a sign; a phrase like -1 is actually an expression composed of the unary operator ‘-‘ and the literal 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Integer literals

A

Integer literals are described by the following lexical definitions:

There is no limit for the length of integer literals apart from what can be stored in available memory.

Underscores are ignored for determining the numeric value of the literal. They can be used to group digits for enhanced readability. One underscore can occur between digits, and after base specifiers like 0x.

Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Floating point literals

A

loating point literals are described by the following lexical definitions:

Note that the integer and exponent parts are always interpreted using radix 10. For example, 077e010is legal, and denotes the same number as 77e10. The allowed range of floating point literals is implementation-dependent. As in integer literals, underscores are supported for digit grouping.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Imaginary literals

A

Imaginary literals are described by the following lexical definitions:

An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., (3+4j). Some examples of imaginary literals:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Operators

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Delimiters

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly