Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

C# LANGUAGE SPECIFICATION

19.2.3 Grammar ambiguities

2The productions for simple-name (§14.5.2) and member-access (§14.5.4) can give rise to ambiguities in the

3grammar for expressions. [Example: The statement:

4F(G<A, B>(7));

5could be interpreted as a call to F with two arguments, G < A and B > (7). Alternatively, it could be

6interpreted as a call to F with one argument, which is a call to a generic method G with two type arguments

7and one regular argument. end example]

8If a sequence of tokens can be parsed (in context) as a simple-name (§14.5.2), member-access (§14.5.4), or

9pointer-member-access (§25.5.2) ending with a type-argument-list (§26.5.1), the token immediately

10following the closing > token is examined. If it is one of

11 ( ) ] : ; , . ? == !=

12then the type-argument-list is retained as part of the simple-name, member-access or pointer-member-access

13and any other possible parse of the sequence of tokens is discarded. Otherwise, the type-argument-list is not

14considered to be part of the simple-name, member-access or pointer-member-access, even if there is no other

15possible parse of the sequence of tokens. [Note: These rules are not applied when parsing a type-argument-

16list in a namespace-or-type-name (§10.8). end note] [Example: The statement:

17F(G<A, B>(7));

18will, according to this rule, be interpreted as a call to F with one argument, which is a call to a generic

19method G with two type arguments and one regular argument. The statements

20F(G<A, B>7);

21F(G<A, B>>7);

22will each be interpreted as a call to F with two arguments. The statement

23x = F<A> + y;

24will be interpreted as a less-than operater, greater-than operator and unary-plus operator, as if the statement

25 had been written x = (F < A) > (+y), instead of as a simple-name with a type-argument-list followed

26by a binary-plus operator. In the statement

27x = y is C<T> + z;

28the tokens C<T> are interpreted as a namespace-or-type-name with a type-argument-list. end example]

299.3 Lexical analysis

30The input production defines the lexical structure of a C# source file. Each source file in a C# program shall

31conform to this lexical grammar production.

32input::

33

input-sectionopt

34

input-section::

35

input-section-part

36

input-section input-section-part

37

input-section-part::

38

input-elementsopt new-line

39

pp-directive

40

input-elements::

41

input-element

42

input-elements input-element

43

input-element::

44

whitespace

45

comment

46

token

64

Chapter 9 Lexical structure

1Five basic elements make up the lexical structure of a C# source file: Line terminators (§9.3.1), white space

2(§9.3.3), comments (§9.3.2), tokens (§9.4), and pre-processing directives (§9.5). Of these basic elements,

3only tokens are significant in the syntactic grammar of a C# program (§9.2.2), except in the case of a > token

4being combined with another token to form a single operator (§9.4.5).

5The lexical processing of a C# source file consists of reducing the file into a sequence of tokens which

6becomes the input to the syntactic analysis. Line terminators, white space, and comments can serve to

7separate tokens, and pre-processing directives can cause sections of the source file to be skipped, but

8otherwise these lexical elements have no impact on the syntactic structure of a C# program.

9When several lexical grammar productions match a sequence of characters in a source file, the lexical

10processing always forms the longest possible lexical element. [Example: The character sequence // is

11processed as the beginning of a single-line comment because that lexical element is longer than a single /

12token. end example]

139.3.1 Line terminators

14Line terminators divide the characters of a C# source file into lines.

15new-line::

16

Carriage return character (U+000D)

17

Line feed character (U+000A)

18

Carriage return character (U+000D) followed by line feed character (U+000A)

19

Next line character (U+2085)

20

Line separator character (U+2028)

21

Paragraph separator character (U+2029)

22For compatibility with source code editing tools that add end-of-file markers, and to enable a source file to

23be viewed as a sequence of properly terminated lines, the following transformations are applied, in order, to

24every source file in a C# program:

25If the last character of the source file is a Control-Z character (U+001A), this character is deleted.

26A carriage-return character (U+000D) is added to the end of the source file if that source file is non-

27empty and if the last character of the source file is not a carriage return (U+000D), a line feed (U+000A),

28a next line character (U+2085), a line separator (U+2028), or a paragraph separator (U+2029). [Note:

29The additional carriage-return allows a program to end in a pp-directive (§9.5) that does not have a

30terminating new-line. end note]

319.3.2 Comments

32Two forms of comments are supported: delimited comments and single-line comments.

33A delimited comment begins with the characters /* and ends with the characters */. Delimited comments

34can occupy a portion of a line, a single line, or multiple lines. [Example: The example

35/* Hello, world program

36

This program writes “hello, world” to the console

37*/

38class Hello

39{

40

static void Main() {

41

System.Console.WriteLine("hello, world");

42}

43}

44includes a delimited comment. end example]

45A single-line comment begins with the characters // and extends to the end of the line. [Example: The

46example

65

C# LANGUAGE SPECIFICATION

1// Hello, world program

2// This program writes “hello, world” to the console

3//

4class Hello // any name will do for this class

5{

6

static void Main() { // this method

must be named "Main"

7

System.Console.WriteLine("hello,

world");

8}

9}

10shows several single-line comments. end example]

11comment::

12

single-line-comment

13

delimited-comment

14 single-line-comment::

15// input-charactersopt

16input-characters::

17

input-character

18

input-characters input-character

19

input-character::

20

Any Unicode character except a new-line-character

21

new-line-character::

22

Carriage return character (U+000D)

23

Line feed character (U+000A)

24

Next line character (U+0085)

25

Line separator character (U+2028)

26

Paragraph separator character (U+2029)

27

delimited-comment::

28

/* delimited-comment-textopt asterisks /

29

delimited-comment-text::

30

delimited-comment-section

31

delimited-comment-text delimited-comment-section

32

delimited-comment-section::

33

not-asterisk

34

asterisks

not-slash

35

asterisks::

 

36

*

 

37

asterisks

*

38

not-asterisk::

 

39

Any Unicode character except *

40

not-slash::

 

41

Any Unicode character except /

42Comments do not nest. The character sequences /* and */ have no special meaning within a single-line

43comment, and the character sequences // and /* have no special meaning within a delimited comment.

44Comments are not processed within character and string literals.

459.3.3 White space

46White space is defined as any character with Unicode class Zs (which includes the space character) as well

47as the horizontal tab character, the vertical tab character, and the form feed character.

66

Соседние файлы в предмете Электротехника