______________________________________________________________________ Annex A (informative) Grammar summary [gram] ______________________________________________________________________ 1 This summary of C++ syntax is intended to be an aid to comprehension. It is not an exact statement of the language. In particular, the grammar described here accepts a superset of valid C++ constructs. Disambiguation rules (_stmt.ambig_, _dcl.spec_, _class.ambig_) must be applied to distinguish expressions from declarations. Further, access control, ambiguity, and type rules must be used to weed out syntacti cally valid but meaningless constructs. A.1 Keywords [gram.key] 1 New context-dependent keywords are introduced into a program by type def (_dcl.typedef_), namespace (_namespace.def_), class (_class_), enumeration (_dcl.enum_), and template (_temp_) declarations. typedef-name: identifier namespace-name: original-namespace-name namespace-alias original-namespace-name: identifier namespace-alias: identifier class-name: identifier template-class-id enum-name: identifier template-name: identifier Note that a typedef-name naming a class is also a class-name (_class.name_). A.2 Lexical conventions [gram.lex] preprocessing-token: header-name identifier pp-number character-constant string-literal operator digraph punctuator each non-white-space character that cannot be one of the above digraph: <% %> <: :> %: token: identifier keyword literal operator punctuator identifier: nondigit identifier nondigit identifier digit nondigit: one of _ a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z digit: one of 0 1 2 3 4 5 6 7 8 9 literal: integer-literal character-literal floating-literal string-literal boolean-literal integer-literal: decimal-literal integer-suffixopt octal-literal integer-suffixopt hexadecimal-literal integer-suffixopt decimal-literal: nonzero-digit decimal-literal digit octal-literal: 0 octal-literal octal-digit hexadecimal-literal: 0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-literal hexadecimal-digit nonzero-digit: one of 1 2 3 4 5 6 7 8 9 octal-digit: one of 0 1 2 3 4 5 6 7 hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F integer-suffix: unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt unsigned-suffix: one of u U long-suffix: one of l L character-literal: 'c-char-sequence' L'c-char-sequence' c-char-sequence: c-char c-char-sequence c-char c-char: any member of the source character set except the single-quote ', backslash \, or new-line character escape-sequence escape-sequence: simple-escape-sequence octal-escape-sequence hexadecimal-escape-sequence simple-escape-sequence: one of \' \" \? \\ \a \b \f \n \r \t \v octal-escape-sequence: \ octal-digit octal-escape-sequence octal-digit hexadecimal-escape-sequence: \x hexadecimal-digit hexadecimal-escape-sequence hexadecimal-digit floating-constant: fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt fractional-constant: digit-sequenceopt . digit-sequence digit-sequence . exponent-part: e signopt digit-sequence E signopt digit-sequence sign: one of + - digit-sequence: digit digit-sequence digit floating-suffix: one of f l F L string-literal: "s-char-sequenceopt" L"s-char-sequenceopt" s-char-sequence: s-char s-char-sequence s-char s-char: any member of the source character set except the double-quote ", backslash \, or new-line character escape-sequence boolean-literal: false true A.3 Basic concepts [gram.basic] translation unit: declaration-seqopt A.4 Expressions [gram.expr] primary-expression: literal this :: identifier :: operator-function-id :: qualified-id ( expression ) id-expression id-expression: unqualified-id qualified-id unqualified-id: identifier operator-function-id conversion-function-id ~ class-name qualified-id: nested-name-specifier unqualified-id postfix-expression: primary-expression postfix-expression [ expression ] postfix-expression ( expression-listopt ) simple-type-specifier ( expression-listopt ) postfix-expression . id-expression postfix-expression -> id-expression postfix-expression ++ postfix-expression -- dynamic_cast < type-id > ( expression ) static_cast < type-id > ( expression ) reinterpret_cast < type-id > ( expression ) const_cast < type-id > ( expression ) typeid ( expression ) typeid ( type-id ) expression-list: assignment-expression expression-list , assignment-expression unary-expression: postfix-expression ++ unary-expression -- unary-expression unary-operator cast-expression sizeof unary-expression sizeof ( type-id ) new-expression delete-expression unary-operator: one of * & + - ! ~ new-expression: ::opt new new-placementopt new-type-id new-initializeropt ::opt new new-placementopt ( type-id ) new-initializeropt new-placement: ( expression-list ) new-type-id: type-specifier-seq new-declaratoropt new-declarator: * cv-qualifier-seqopt new-declaratoropt ::opt nested-name-specifier * cv-qualifier-seqopt new-declaratoropt direct-new-declarator direct-new-declarator: [ expression ] direct-new-declarator [ constant-expression ] new-initializer: ( expression-listopt ) delete-expression: ::opt delete cast-expression ::opt delete [ ] cast-expression cast-expression: unary-expression ( type-id ) cast-expression pm-expression: cast-expression pm-expression .* cast-expression pm-expression ->* cast-expression multiplicative-expression: pm-expression multiplicative-expression * pm-expression multiplicative-expression / pm-expression multiplicative-expression % pm-expression additive-expression: multiplicative-expression additive-expression + multiplicative-expression additive-expression - multiplicative-expression shift-expression: additive-expression shift-expression << additive-expression shift-expression >> additive-expression relational-expression: shift-expression relational-expression < shift-expression relational-expression > shift-expression relational-expression <= shift-expression relational-expression >= shift-expression equality-expression: relational-expression equality-expression == relational-expression equality-expression != relational-expression and-expression: equality-expression and-expression & equality-expression exclusive-or-expression: and-expression exclusive-or-expression ^ and-expression inclusive-or-expression: exclusive-or-expression inclusive-or-expression | exclusive-or-expression logical-and-expression: inclusive-or-expression logical-and-expression && inclusive-or-expression logical-or-expression: logical-and-expression logical-or-expression || logical-and-expression conditional-expression: logical-or-expression logical-or-expression ? expression : assignment-expression assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression throw-expression assignment-operator: one of = *= /= %= += -= >>= <<= &= ^= |= expression: assignment-expression expression , assignment-expression constant-expression: conditional-expression A.5 Statements [gram.stmt.stmt] statement: labeled-statement expression-statement compound-statement selection-statement iteration-statement jump-statement declaration-statement try-block labeled-statement: identifier : statement case constant-expression : statement default : statement expression-statement: expressionopt ; compound-statement: { statement-seqopt } statement-seq: statement statement-seq statement selection-statement: if ( condition ) statement if ( condition ) statement else statement switch ( condition ) statement condition: expression type-specifier-seq declarator = assignment-expression iteration-statement: while ( condition ) statement do statement while ( expression ) ; for ( for-init-statement conditionopt ; expressionopt ) statement for-init-statement: expression-statement declaration-statement jump-statement: break ; continue ; return expressionopt ; goto identifier ; declaration-statement: declaration A.6 Declarations [gram.dcl.dcl] declaration: decl-specifier-seqopt init-declarator-listopt ; function-definition template-declaration asm-definition linkage-specification namespace-definition namespace-alias-definition using-declaration using-directive decl-specifier-seqopt init-declarator-listopt ; decl-specifier: storage-class-specifier type-specifier function-specifier friend typedef decl-specifier-seq: decl-specifier-seqopt decl-specifier storage-class-specifier: auto register static extern mutable function-specifier: inline virtual typedef-name: identifier type-specifier: simple-type-specifier class-specifier enum-specifier elaborated-type-specifier cv-qualifier simple-type-specifier: ::opt nested-name-specifieropt type-name char wchar_t bool short int long signed unsigned float double void type-name: class-name enum-name typedef-name elaborated-type-specifier: class-key ::opt nested-name-specifieropt identifier enum ::opt nested-name-specifieropt identifier class-key: class struct union enum-name: identifier enum-specifier: enum identifieropt { enumerator-listopt } enumerator-list: enumerator-definition enumerator-list , enumerator-definition enumerator-definition: enumerator enumerator = constant-expression enumerator: identifier original-namespace-name: identifier namespace-definition: original-namespace-definition extension-namespace-definition unnamed-namespace-definition original-namespace-definition: namespace identifier { namespace-body } extension-namespace-definition: namespace original-namespace-name { namespace-body } unnamed-namespace-definition: namespace { namespace-body } namespace-body: declaration-seqopt id-expression: unqualified-id qualified-id nested-name-specifier: class-or-namespace-name :: nested-name-specifieropt class-or-namespace-name: class-name namespace-name namespace-name: original-namespace-name namespace-alias namespace-alias: identifier namespace-alias-definition: namespace identifier = qualified-namespace-specifier ; qualified-namespace-specifier: ::opt nested-name-specifieropt class-or-namespace-name using-declaration: using ::opt nested-name-specifier unqualified-id ; using :: unqualified-id ; using-directive: using namespace ::opt nested-name-specifieropt namespace-name ; asm-definition: asm ( string-literal ) ; linkage-specification: extern string-literal { declaration-seqopt } extern string-literal declaration declaration-seq: declaration declaration-seq declaration A.7 Declarators [gram.dcl.decl] init-declarator-list: init-declarator init-declarator-list , init-declarator init-declarator: declarator initializeropt declarator: direct-declarator ptr-operator declarator direct-declarator: declarator-id direct-declarator ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationopt direct-declarator [ constant-expressionopt ] ( declarator ) ptr-operator: * cv-qualifier-seqopt & ::opt nested-name-specifier * cv-qualifier-seqopt cv-qualifier-seq: cv-qualifier cv-qualifier-seqopt cv-qualifier: const volatile declarator-id: id-expression nested-name-specifieropt type-name type-id: type-specifier-seq abstract-declaratoropt type-specifier-seq: type-specifier type-specifier-seqopt abstract-declarator: ptr-operator abstract-declaratoropt direct-abstract-declarator direct-abstract-declarator: direct-abstract-declaratoropt ( parameter-declaration-clause ) cv-qualifier-seqopt exception-specificationopt direct-abstract-declaratoropt [ constant-expressionopt ] ( abstract-declarator ) parameter-declaration-clause: parameter-declaration-listopt ...opt parameter-declaration-list , ... parameter-declaration-list: parameter-declaration parameter-declaration-list , parameter-declaration parameter-declaration: decl-specifier-seq declarator decl-specifier-seq declarator = assignment-expression decl-specifier-seq abstract-declaratoropt decl-specifier-seq abstract-declaratoropt = assignment-expression function-definition: decl-specifier-seqopt declarator ctor-initializeropt function-body function-body: compound-statement initializer: = initializer-clause ( expression-list ) initializer-clause: assignment-expression { initializer-list ,opt } { } initializer-list: initializer-clause initializer-list , initializer-clause A.8 Classes [gram.class] class-name: identifier template-id class-specifier: class-head { member-specificationopt } class-head: class-key identifieropt base-clauseopt class-key nested-name-specifier identifier base-clauseopt class-key: class struct union member-specification: member-declaration member-specificationopt access-specifier : member-specificationopt member-declaration: decl-specifier-seqopt member-declarator-listopt ; function-definition ;opt qualified-id ; using-declaration member-declarator-list: member-declarator member-declarator-list , member-declarator member-declarator: declarator pure-specifieropt declarator constant-initializeropt identifieropt : constant-expression pure-specifier: = 0 constant-initializer: = constant-expression A.9 Derived classes [gram.class.derived] base-clause: : base-specifier-list base-specifier-list: base-specifier base-specifier-list , base-specifier base-specifier: ::opt nested-name-specifieropt class-name virtual access-specifieropt ::opt nested-name-specifieropt class-name access-specifier virtualopt ::opt nested-name-specifieropt class-name access-specifier: private protected public A.10 Special member functions [gram.special] class-name ( expression-listopt ) conversion-function-id: operator conversion-type-id conversion-type-id: type-specifier-seq conversion-declaratoropt conversion-declarator: ptr-operator conversion-declaratoropt ctor-initializer: : mem-initializer-list mem-initializer-list: mem-initializer mem-initializer , mem-initializer-list mem-initializer: ::opt nested-name-specifieropt class-name ( expression-listopt ) identifier ( expression-listopt ) A.11 Overloading [gram.over] operator-function-id: operator operator operator: one of new delete new[] delete[] + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- , ->* -> () [] A.12 Templates [gram.temp] template-declaration: template < template-parameter-list > declaration template-parameter-list: template-parameter template-parameter-list , template-parameter template-id: template-name < template-argument-list > template-name: identifier template-argument-list: template-argument template-argument-list , template-argument template-argument: assignment-expression type-id template-name type-name-declaration: typedef qualified-name ; explicit-instantiation: template inst ; inst: class-key template-id type-specifier-seq template-id ( parameter-declaration-clause ) specialization: declaration template-parameter: type-parameter parameter-declaration type-parameter: class identifieropt class identifieropt = type-id typedef identifieropt typedef identifieropt = type-name template < template-parameter-list > class identifieropt template < template-parameter-list > class identifieropt = template-name A.13 Exception handling [gram.except] try-block: try compound-statement handler-seq handler-seq: handler handler-seqopt handler: catch ( exception-declaration ) compound-statement exception-declaration: type-specifier-seq declarator type-specifier-seq abstract-declarator type-specifier-seq ... throw-expression: throw assignment-expressionopt exception-specification: throw ( type-id-listopt ) type-id-list: type-id type-id-list , type-id