[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [Samples] / c++.syntax
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Tue Sep 18 15:25:35 PDT 2007

Contents


    The Syntax of C++

      .Road_works_ahead This page defines the form of C++ described in the Annotated C++ Manual of Ellis and Stroustroup. It is not unlike the C++ implemented by most compilers. However, C++ is still evolving and the ANSI/ISO standard version of C++ will be significantly different to the origonal C with classes defined by Stroustroup.

      For uptodate syntax try the syntax for the draft ANSI/ISO C++ standard: [ syntax.html ] and [ gram.html ]

      See Also


        [ c++.glossary.html ]

        [ comp.text.ASCII.html ]

        [ objects.glossary.html ]

        [ math.syntax.html ]


      The idea of C++ was to include most of C as a subset. The following lines formalize this intention:
    1. C::documentation=http://www.csci.csusb.edu/dick/samples/c.syntax.html, [ c.syntax.html ]

      C.

      Note

        The Standard is no longer guaranteeing upward compatabilty between C and C++. Many good ANSI standard C programs are now longer standard (or even compilable) C++. [ http://www.csci.csusb.edu/dick/c++std/ ]

      . . . . . . . . . ( end of section Note) <<Contents | End>>

      The above means that this file does not have to repeat any of the definition in C when the same name is used for different syntaxes in C and C++ and it is necesary to refer to the C form in this grammar then it is written like this example:

    2. C.comment

      Otherwise the old name can be used as if had been included above.

      The C++ version is written without a prefix and defined in this file. For example C++ uses both the old C comment and a new one:

    3. comment::=C.comment | inline_comment.
    4. inline_comment::="//" #(non(eoln))eoln,

      Lexemes

    5. pre_processor_tokens::= [ preprocessor_token in c++.glossary ]
    6. keyword::= [ keyword in c++.glossary ]
    7. punctuation::= [ punctuation in c++.glossary ]
    8. operators::= [ operators in c++.glossary ]
    9. identifier::= [ identifier in c++.glossary ]

      Declarations

    10. declaration::= O(declaration_specifiers ) O(declarator_list ) | function_declaration | linkage_specification,

    11. declaration_specifiers::= N(declaration_specifier) ,

    12. declaration_specifier::= storage_class_specifier | type_specifier | function_specifier | "friend" | "typedef",

    13. storage_class_specifier::= "auto" | "register" | "static" | "extern",

    14. function_specifier::= "inline" | "virtual",

    15. type_specifier::= simple_type_name | class_specifier | enumeration_specifier | elaborated_type_specifier | "const" | "volatile",

    16. simple_type_name::= class_name | "typedef" name | "char" | "short" | "int" | "long" | "signed" | "unsigned" | "float" | "double" | "void",

    17. elaborated_type_specifier::= class_key identifier | class_key class_name | "enum" enumeration_name,

    18. class_key::= "class" | "struct" | "union",

    19. enumeration_specifier::= C.enumeration_specifier,


    20. |-C.enumeration_specifier=enumeration_identifier | "enum" identifier "{"enumeration_list"}",

    21. linkage_specification::= "extern" string "{" O(declaration_list ) "}" | "extern" string_declaration,

    22. declaration_list::= declaration #(";" declaration),

    23. class_specifier::= class_head "{" O(member_list ) "}",
    24. class_head::= class_key O(identifier ) O(base_specifier ) | class_key class_name O(base_specification ),

    25. member_list::= #( O(access_specifier colon ) #member_declaration ),

    26. member_declaration::= O(declaration_specifiers) O(member_declarator_list ) ";" | function_definition O(";") | qualified_name ";",
    27. type_description::= class | struct | union,

    28. simple_class::="class" identifier "{" private_part explicit_access_part"}",
    29. structure::=struct,
    30. simple_struct::="struct" identifier "{" public_part "private:" explicit_access_part "}"
    31. union::="union" O(identifier) "{" public_part "}",
    32. explicit_access_part::= "private" colon private_part | "public" colon public_part | "protected" colon protected_part,
    33. private_part::=#member_declaration,
    34. public_part::=#member_declaration,
    35. protected_part::=#member_declaration.

    36. member_declarator_list::= List( member_declarator),

    37. member_declarator::= declarator O(pure_specifier ) | O(identifier ) colon constant_expression,

    38. pure_specifier::= "=" "0",

    39. base_specification::= colon List(base_specifier),

    40. base_specifier::= class_name | "virtual" O(access_specifier ) class_name | access_specifier O("virtual" ) class_name,

    41. access_specifier::= "private" | "protected" | "public",

    42. conversion_function_name::= "operator" conversion_type_name,

    43. constructor_declarations::= ...

    44. destructor_declarations::= ...

    45. constructor_initializer::= colon memory_initializer_list,

    46. memory_initializer_list::= List( memory_initializer),

    47. memory_initializer::= class_name "(" O(argument_list ) ")" | identifier "(" O(argument_list ) ")",

      Operators and Variables

    48. operator_function_name::= "operator" operator,

    49. scope_resolution_operator::lexeme= "::".
    50. double_colon::=scope_resolution_operator.
    51. DC::abbreviation=double_colon.
    52. variable::=C.variable | class_name DC static_data_member.

      Expressions

    53. expression::=C.expression | multiplicative_expression.

    54. multiplicative_expression::=pm_expression #(multiplicative_operator pm_expression).

    55. pm_expression::=cast_expression #(cast_operator cast_expression).

    56. cast_operator::= ".*" | "->*".

    57. unary_expression::= C.unary_expression | allocation_expression | deallocation_expression.

    58. allocation_expression::= O(DC) "new" O(placement) new_type_name O(new_initializer) | O(DC) "new" O(placement) (type_name) O(new_initializer)
    59. deallocation_expression::= O(DC) "delete" O("[]") cast_expression.

    60. placement::="(" expression_list ")".

    61. new_type_name::= N(type_specifier) new_declarator.
    62. new_declarator::= O(complete_class_name DC )"*" const_or_volatile) new_delarator | O(new-declarator) "[" expression "]".

    63. asm_declaration::= "asm" "{" string_literal "}" ";" .

      Declarators

    64. declarator_list::=List(declarator O(initializer)).

    65. declarator::= dname | ptr_operator declarator | declarator ( "(" argument_declaration_list")" const_or_volatile | "[" O(constant_expression)"]" | "(" declarator ")".

    66. ptr_operator::= ("*" | "&"| complete_class_name DC "*") const_or_volatile.

    67. const_or_volatile::= "const" | O("volatile").

    68. dname::= name| O("~") class_name | typedef_name | qualified_type_name.

    69. type_name::= N(type_specifier) O(abstract_declarator).

    70. abstract_declarator::=ptr_operator O(abstract_declarator)| O(abstract_declarator) ( "(" argument_declaration_list")" const_or_volatile | "[" O(constant_expression)"]" | "(" abstract_declarator ")".

    71. argument_declaration_list::=O(arg_declaration_list) (O("...") | O( "," "...") ).

    72. arg_decalration_list::=List(argument_declaration).

    73. arg_decalarion::= decl_specifiers ( declarator O("=" expression) | O(abstract_declarator)O("=" expression)).

    74. fct_body::=compound_statement.

    75. initializer::= "=" ( assignemnt_expression | "{" initializer_list O(comma) "}") "(" expression_list ")".

    76. initializer_list::= List(assignment_expression) | "{" initializer_list O(comma) "}".

      Class Declarations

      class_specifier::=class_head "{" member_list "}".

      member_declaration::= qualified_name ";" | function_definition O(";") | O(decl_specifiers) O(List(member_declarator)).

    77. base_spec::= colon List(base_specifier).

    78. conversion_type_name::=N(type_specifier) O(ptr-operator).
    79. type_specifier_list::=N(type_specifier).

    80. ctor::=constructor.
    81. mem::=member.
    82. ctor_initializer::=constructor_initializer::=colon member_initializer_list.
    83. mem_initializer_list::=member_initializer_list::=List(member_initializer).
    84. mem_initializer::=member_initializer::=(complete_class_name | identifier) "(" O(List(expression)) ")".

    85. operator::lexeme= "new" | "delete" | "+" | "-" | "*" | "/" | "%" | "^" | "&" | "~" | "!" | "=" | "<" | ">" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" | "|=" | "<<" | ">>" | "<<=" | ">>=" | "==" | "++" | "--" | "," | "->*" | "!=" | "<=" | ">=" |"->" | "()" | "[]" .

      Statements

    86. statement::= C.statement | declaration_statement | try_block.
    87. if_statement::=C.if_statement.

    88. compound_statement::="{" #statement "}".

    89. block::=compound_statement & with at least one declaration statemnt.

    90. declaration_statement::= declaration.

    91. for_init_statement::= expression_statement | declaration_statement.

    92. iteration_statement::="while" "(" expression ")" statement | "do" statement "while" "(" expression ")" ";" | for_statement.

    93. for_statement::="for" "(" for_init_statement O(expression) ";" O(expression) ")" statement. -- thanks to Ben Evans for pointing out a typographical error here.

      Templates

    94. template_declaration::= "template" "<" List(template_argument) ">" declaration.

    95. template_argument::=type_argument | argument_deeclaration.

    96. type_argument::="class" identifier.

    97. template_clase_name::= template_name "<" List(template_arg) ">".

    98. template_arg::=expression| type_name.

      .Exception Handling

    99. try_block::= "try" compound_statement #handler.

    100. handler::= "catch" "(" exception_declaration ")" compound_statement.

    101. exception_declaration::= "..." | N(type_specifier) O(declarator | abstarct_declarator).

    102. throw_expression::= "throw" O(assignment_expression).

    103. exception_specification::= "throw" "(" List(type) ")".

      ...

      Namespaces:

      //ftp.std.com/AW/strousetroup2e/iso.ps A mechanism, called namespaces, was introduced for controlling the scope of otherwise global names.

      The grammar for the namespace constructs is:

    104. namespace_definition::="namespace" identifier "{" O( declaration_list) "}".

    105. namespace_alias_definition::= "namespace" identifier "=" namespace_name ";".

    106. using_declaration::= "using" namespace_name "::" identifier ";".

    107. using_directive::= "using" "namespace" namespace_name ";".

    108. namespace_name::= O("::") #( identifier "::" ) identifier.

      An identifier in a namespace_name must have been specified to refer to a namespace by being the identifier in a namespace_definition or a namespace_alias_definition.

    . . . . . . . . . ( end of section The Syntax of C++) <<Contents | End>>

End