Tải bản đầy đủ (.pdf) (14 trang)

C# 2.0 Grammar

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (220.94 KB, 14 trang )

appendix
A
C# 2.0 Grammar
T
his appendix contains the grammatical summary of the C# 2.0 programming lan-
guage. Its syntax is described in a concise fashion using the EBNF notation as summarized
once again in Table A.1.
The grammar productions are logically grouped into two main grammars: lexical and
syntactic. The lexical grammar defines tokens extracted by the lexical analyzer or scanner,
and the syntactic grammar is used by the parser to specify how the tokens are organized
to produce valid C# programs.
Notation Meaning
A* Repetition—zero or more occurrences of A
A+ Repetition—one or more occurrences of A
A? Option—zero or one occurrence of A
AB Sequence—A followed by B
A|B Alternative—A or B
"0".."9" Alternative—one character between 0 and 9 inclusively
(AB) Grouping—of an ABsequence
Table A.1: Notation for Extended Backus–Naur Form.
A.1 Lexical Grammar
Input = InputSection? .
InputSection = InputSectionPart+ .
InputSectionPart = (InputElements? NewLine) | PpDirective .
InputElement = Whitespace | Comment | Token .
227
228
Appendix A: C# 2.0 Grammar

A.1.1 Line Terminators
All line terminators are represented by the Newline production. A Newline is either a


carriage return (CR) as the \u000D or ‘\r’ character, a line feed (LF) as the \u000A or
‘\n’ character, a (CR) followed by (LF), a line separator (LS) as the \u2028 character, or a
paragraph separator (PS) as the \u2029 character.
NewLine = CR | LF | CRLF | LS | PS .
A.1.2 White Space
A white space is any character with Unicode Class Zs, a horizontal tab (HT) as the \u0009
or ‘\t’ character, a vertical tab (VT) as the \u000B or ‘\v’ character, or a form feed (FF)
as the \u000C or ‘\f’ character.
Whitespace = AnyCharacterWithUnicodeClassZs | HT | VT | FF .
A.1.3 Comments
Comment = SingleLineComment | DelimitedComment .
SingleLineComment = "//" InputCharacters? .
InputCharacter = AnyUnicodeCharacterExceptANewLine .
DelimitedComment = "/*" DelimitedCommentCharacters? "*/" .
DelimitedCommentCharacter = NotAsterisk | ("*" NotSlash) .
NotAsterisk = AnyUnicodeCharacterExcept "*" .
NotSlash = AnyUnicodeCharacterExcept "/" .
A.1.4 Tokens
Token = Identifier | Keyword | Literal | OperatorOrPunctuator .
Note: null, true, and false are keywords as well as literals.
A.1.5 Unicode Character Escape Sequences
UnicodeEscapeSequence = ("\u" FourHexDigits) | ("\U" FourHexDigits FourHexDigits) .
FourHexDigits = HexDigit HexDigit HexDigit HexDigit .
A.1.6 Identifiers
Identifier = AvailableIdentifier | ("@" IdentifierOrKeyword) .
AvailableIdentifier = An IdentifierOrKeyword that is not a Keyword .
IdentifierOrKeyword = IdentifierStartCharacter IdentifierPartCharacters? .
IdentifierStartCharacter = LetterChar | "_" .
IdentifierPartCharacter = LetterChar | DecimalDigitChar | ConnectingChar
| CombiningChar | FormattingChar .


A.1 Lexical Grammar
229
A LetterChar is either a Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl; or a Unicode-
character-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl.
A CombiningChar is either a Unicode character of classes Mn or Mc; or a Unicode-character-
escape-sequence representing a character of classes Mn or Mc. A DecimalDigitChar
is either a Unicode character of the class Nd, or a Unicode-character-escape-sequence
representing a character of the class Nd. A ConnectingChar is either a Unicode char-
acter of the class Pc, or a Unicode-character-escape-sequence representing a character
of the class Pc. A FormattingChar is either a Unicode character of the class Cf, or a
Unicode-character-escape-sequence representing a character of the class Cf.
A.1.7 Keywords
abstract as base bool break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach goto
if implicit in int interface
internal is lock long namespace
new null object operator out
override params private protected public
readonly ref return sbyte sealed
short sizeof stackalloc static string
struct switch this throw true
try typeof uint ulong unchecked
unsafe ushort using virtual void
volatile while
A.1.8 Literals

Literal = BooleanLiteral | IntegerLiteral | RealLiteral
| CharacterLiteral | StringLiteral | NullLiteral .
BooleanLiteral = "true" | "false" .
IntegerLiteral = DecimalIntLiteral | HexIntLiteral .
DecimalIntLiteral = DecimalDigits IntegerTypeSuffix? .
DecimalDigit = "0".."9" .
IntegerTypeSuffix = "U" | "u" | "L" | "l" | "Ul" | "ul" | "Lu" | "lu" | "UL" | "uL" | "LU" | "lU" .
HexIntegerLiteral = ("0x" | "0X") HexDigits IntegerTypeSuffix? .
HexDigit = "0..9" | "A".."F" | "a".."f" .
RealLiteral = ( DecimalDigits "." DecimalDigits ExponentPart? RealTypeSuffix? )
| ( "." DecimalDigits ExponentPart? RealTypeSuffix? )
| ( DecimalDigits ExponentPart RealTypeSuffix? )
| ( DecimalDigits RealTypeSuffix ) .
230
Appendix A: C# 2.0 Grammar

ExponentPart = ("e" | "E") Sign? DecimalDigits .
Sign = "+" | "-" .
RealTypeSuffix = "F" | "f" | "D" | "d" | "M" | "m" .
CharacterLiteral = "’" Character "’" .
Character = SingleCharacter | SimpleEscapeSequence | HexEscapeSequence | UnicodeEscapeSequence .
SingleCharacter = Any Character Except Quote, Escape, and NewLine .
SimpleEscapeSequence = "\’" | "\\" | "\0" | "\a" | "\b" | "\f" | "\n" | "\r" | "\t" | "\v" | DQuote .
DQuote = "\"" (\u0022) .
Quote = "’" (\u0027) .
Escape = "\\" (\u005C) .
HexEscapeSequence = "\x" HexDigit HexDigit? HexDigit? HexDigit? .
StringLiteral = RegularStringLiteral | VerbatimStringLiteral .
RegularStringLiteral = " RegularStringLiteralCharacters? " .
RegularStringLiteralCharacter = SingleRegularStringLiteralCharacter | SimpleEscapeSequence

| HexadecimalEscapeSequence | UnicodeEscapeSequence .
SingleRegularStringLiteralCharacter = Any Character Except DQuote, Escape, and NewLine .
VerbatimStringLiteral = "@" DQuote VerbatimStringLiteralCharacters? DQuote .
VerbatimStringLiteralCharacter = SingleVerbatimStringLiteralCharacter | QuoteEscapeSequence .
SingleVerbatimStringLiteralCharacter = Any Character Except DQuote .
QuoteEscapeSequence = "\’" .
NullLiteral = "null" .
A.1.9 Operators and Punctuators
{}[]().,=;
+-*/%&|ˆ!˜
=<>?::++--&&||->
== != <= >= += -= *= /= %= &=
|= ˆ= <<= << > > > >=
A.1.10 Preprocessing Directives
PpDirective = PpDeclaration | PpConditional | PpLine | PpDiagnostic | PpRegion | PpPragma .
PpNewLine = Whitespace? SingleLineComment? NewLine .
ConditionalSymbol = Any IdentifierOrKeyword Except "true" or "false" .
PpExpr = Whitespace? PpOrExpr Whitespace? .
PpOrExpr = PpAndExpr (Whitespace? "||" Whitespace? PpAndExpr)* .
PpAndExpr = PpEqualityExpr (Whitespace? "&&" Whitespace? PpEqualityExpr)* .
PpEqualityExpr = PpUnaryExpr (Whitespace? ("==" | "!=") Whitespace? PpUnaryExpr)* .
PpUnaryExpr = ("!" Whitespace? PpPrimaryExpr)* .
PpPrimaryExpr = "true" | "false" | ConditionalSymbol | "(" Whitespace? PpExpr Whitespace? ")" .
PpDeclaration = Whitespace? "#" Whitespace? ("define"|"undef") Whitespace ConditionalSymbol PpNewLine .
PpConditional = PpIfSection PpElifSections? PpElseSection? PpEndif .

A.2 Syntactic Grammar
231
PpIfSection = Whitespace? "#" Whitespace? "if" Whitespace PpExpr PpNewLine ConditionalSection? .
PpElifSection = Whitespace? "#" Whitespace? "elif" Whitespace PpExpr PpNewLine ConditionalSection? .

PpElseSection = Whitespace? "#" Whitespace? "else" PpNewLine ConditionalSection? .
PpEndifLine = Whitespace? "#" Whitespace? "endif" PpNewLine .
ConditionalSection = InputSection | SkippedSection .
SkippedSection = SkippedSectionPart+ .
SkippedSectionPart = (SkippedCharacters? NewLine) | PpDirective .
SkippedCharacters = Whitespace? NotNumberSign InputCharacters? .
NotNumberSign = Any InputCharacter Except "#" .
PpLine = Whitespace? "#" Whitespace? "line" Whitespace LineIndicator PpNewLine .
LineIndicator = (DecimalDigits Whitespace FileName) | DecimalDigits | "default" .
FileName = "\"" FileNameCharacters "\"" .
FileNameCharacter = Any InputCharacter Except "\"" .
PpDiagnostic = Whitespace? "#" Whitespace? ("error" | "warning") PpMessage .
PpMessage = NewLine | (Whitespace InputCharacters? NewLine) .
PpRegion = PpStartRegion ConditionalSection? PpEndRegion .
PpStartRegion = Whitespace? "#" Whitespace? "region" PpMessage .
PpEndRegion = Whitespace? "#" Whitespace? "endregion" PpMessage .
PpPragma = Whitespace? "#" Whitespace? "pragma" PragmaBody PpNewLine .
PragmaBody = PragmaWarningBody .
PragmaWarningBody = "warning" Whitespace WarningAction ( Whitespace WarningList )? .
WarningAction = "disable" | "restore" .
WarningList = DecimalDigits ( Whitespace? "," Whitespace? DecimalDigits )* .
A.2 Syntactic Grammar
A.2.1 Namespace, Type, and Simple Names
NamespaceName = NamespaceOrTypeName .
TypeName = NamespaceOrTypeName .
NamespaceOrTypeName = ( Identifier TypeArgumentList? )
| ( "." Identifier TypeArgumentList? )*
| QualifiedAliasMember .
SimpleName = Identifier TypeArgumentList? .
QualifiedAliasMember = Identifier "::" Identifier TypeArgumentList? .

A.2.2 Types
Type = ValueType | ReferenceType | TypeParameter .
ValueType = StructType | EnumType | NullableType .
StructType = TypeName | SimpleType .

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×