Lexical Structure Flashcards

Understand the use of Swift keywords

You may prefer our related Brainscape-certified flashcards:
1
Q

What are the rules for the characters allowed in an identifier?

A

Identifiers begin with an upper case or lower case letter A through Z, an underscore (_), a noncombining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area. After the first character, digits and combining Unicode characters are also allowed.

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

How can one use a reserved word as an identifier?

A

To use a reserved word as an identifier, put a backtick () before and after it. For example, class is not a valid identifier, but class ` is valid. The backticks are not considered part of the identifier; ` x ` and x have the same meaning.

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

Describe the rule for implicit identifiers within closures.

A

Inside a closure with no explicit parameter names, the parameters are implicitly named $0, $1, $2, and so on. These names are valid identifiers within the scope of the closure.

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

List all the keywords that can appear in declarations.

A

class, deinit, enum, extension, func, import, init, let, protocol, static, struct, subscript, typealias, and var.

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

List all the keywords that can appear in statements.

A

break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, where, and while.

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

List all the keywords that can appear in expressions and types.

A

as, dynamicType, is, new, super, self, Self, Type, __COLUMN__, __FILE__, __FUNCTION__, and __LINE__.

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

List all the keywords that are only reserved in particular contexts in the grammar.

A

associativity, didSet, get, infix, inout, left, mutating, none, nonmutating, operator, override, postfix, precedence, prefix, right, set, unowned, unowned(safe), unowned(unsafe), weak and willSet. Outside the context in which they appear in the grammar, these can be used as identifiers.

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

State the two uses of whitespace in Swift

A

Whitespace has two uses: to separate tokens in the source file and to help determine whether an operator is a prefix or postfix, but is otherwise ignored.

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

Which characters are considered whitespace?

A

The following characters are considered whitespace: space (U+0020), line feed (U+000A), carriage return (U+000D), horizontal tab (U+0009), vertical tab (U+000B), form feed (U+000C) and null (U+0000).

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

What rules govern comments?

A

Single line comments begin with // and continue until the end of the line. Multiline comments begin with /* and end with */. Nesting is allowed, but the comment markers must be balanced.

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

How can you use a Swift keyword as an identifier.

A

A keyword in Swift can be used as an identifier if it escaped with backticks. The backticks are not considered part of the identifier; x and x have the same meaning.

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

What are the kinds of literals described as being part of Swift’s lexical structure?

A

literal → integer-literal­ | floating-point-literal­ | string-literal­

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

What is an integer literal?

A

Integer literals represent integer values of unspecified precision. By default, integer literals are expressed in decimal. Decimal literals contain the digits 0 through 9. Negative integers literals are expressed by prepending a minus sign (-) to an integer literal, as in -42.

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

How do you specify an alternate base of an integer literal?

A

You can specify an alternate base using a prefix. Binary literals begin with 0b, octal literals begin with 0o, and hexadecimal literals begin with 0x.

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

What characters can appear in an integer literal?

A

Binary literals contain 0 and 1, octal literals contain 0 through 7, and hexadecimal literals contain 0 through 9 as well as A through F in upper- or lowercase. Underscores (_) are allowed between digits for readability, but are ignored and therefore don’t affect the value of the literal. Integer literals can begin with leading zeros (0), but are likewise ignored and don’t affect the base or value of the literal.

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

What is a floating point literal?

A

Floating-point literals represent floating-point values of unspecified precision. By default, floating-point literals are expressed in decimal (with no prefix), but they can also be expressed in hexadecimal (with a 0x prefix).

17
Q

How do you express a decimal floating point literal?

A

Decimal floating-point literals consist of a sequence of decimal digits followed by either a decimal fraction, a decimal exponent, or both. The decimal fraction consists of a decimal point (.) followed by a sequence of decimal digits. The exponent consists of an upper- or lowercase e prefix followed by sequence of decimal digits that indicates what power of 10 the value preceding the e is multiplied by.

18
Q

How do you express a hexidecimal floating point literal?

A

Hexadecimal floating-point literals consist of a 0x prefix, followed by an optional hexadecimal fraction, followed by a hexadecimal exponent. The hexadecimal fraction consists of a decimal point followed by a sequence of hexadecimal digits. The exponent consists of an upper- or lowercase p prefix followed by sequence of decimal digits that indicates what power of 2 the value preceding the p is multiplied by.

19
Q

What is the lexical rule for negative floating point literals?

A

Unlike with integer literals, negative floating-point numbers are expressed by applying the unary minus operator (-) to a floating-point literal, as in -42.0. The result is an expression, not a floating-point integer literal.

20
Q

What is the type of an integer literal?

A

Unless otherwise specified, the default type of an integer literal is the Swift standard library type Int. The Swift standard library also defines types for various sizes of signed and unsigned integers.

21
Q

What is the type of a floating point literal?

A

Unless otherwise specified, the default type of a floating-point literal is the Swift standard library type Double, which represents a 64-bit floating-point number. The Swift standard library also defines a Float type, which represents a 32-bit floating-point number.

22
Q

What is a string literal?

A

A string literal is a sequence of characters surrounded by double quotes, with the following form: “characters”

23
Q

What unescaped characters cannot be contained in a string literal?

A

String literals cannot contain an unescaped double quote (“), an unescaped backslash (), a carriage return, or a line feed.

24
Q

What special character escape sequences can appear in string literals?

A

Null Character (\0) Backslash (\) Horizontal Tab (\t) Line Feed (\n) Carriage Return (\r) Double Quote (") Single Quote (')

25
Q

How can you include unicode characters in a string literal?

A

Characters can also be expressed by \x followed by two hexadecimal digits, \u followed by four hexadecimal digits, or \U followed by eight hexadecimal digits. The digits in these escape sequences identify a Unicode codepoint.

26
Q

How can the value of an expression be inserted into a string literal?

A

The value of an expression can be inserted into a string literal by placing the expression in parentheses after a backslash ().

27
Q

What characters must an interpolated expression never contain?

A

The interpolated expression must not contain an unescaped double quote (“), an unescaped backslash (), a carriage return, or a line feed.

28
Q

What types can an interpolated expression evaluate to?

A

An interpolated expression must evaluate to a value of a type that the String class has an initializer for.

29
Q

What is the default type of a string literal?

A

The default type of a string literal is String. The characters that make up a string are of type Character.

30
Q

Which characters can be used in operators?

A

Operators are made up of one or more of the following characters: /, =, -, +, !, *, %, , &, |, ^, ~, and .

31
Q

Which tokens cannot be used as operators?

A

The tokens =, ->, //, /*, */, ., and the unary prefix operator & are reserved. These tokens can’t be overloaded, nor can they be used to define custom operators.

32
Q

What determines whether an operator is used as a prefix, postfix, or binary operator?

A

The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or a binary operator.

33
Q

What is special about the following tokens: =, ->, //, /*, */, ., and the unary prefix operator &

A

They are reserved, they cannot be overloaded, nor can they be used to define custom operators.

34
Q

When is an operator treated as a binary operator?

A

If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator.

35
Q

When is an operator treated as a prefix unary operator?

A

If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator.

36
Q

When is an operator treated as a postfix unary operator?

A

If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator. If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an example, the ++ operator in a++.b is treated as a postfix unary operator (a++ . b rather than a ++ .b).

37
Q

What additional characters are considered ‘whitespace’ in the context of the rules governing operator treatment?

A

In rules regarding the treatment of operators, the characters ( [and { before an operator, the characters )] and } after an operator, and the characters , ; and : are also considered whitespace.

38
Q

What are the caveats regarding the operator whitespace rules?

A

If the ! or ? operator has no whitespace on the left, it is treated as a postfix operator, regardless of whether it has whitespace on the right. To use the ? operator as syntactic sugar for the Optional type, it must not have whitespace on the left. To use it in the conditional (? :) operator, it must have whitespace around both sides.