.Open 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: .See http://www/dick/c++std/syntax.html and .See http://www/dick/c++std/cd2/gram.html . See Also .Box .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html .See http://www.csci.csusb.edu/dick/samples/comp.text.ASCII.html .See http://www.csci.csusb.edu/dick/samples/objects.glossary.html .See http://www.csci.csusb.edu/dick/samples/math.syntax.html .Close.Box The idea of C++ was to include most of $C as a subset. The following lines formalize this intention: C::documentation=`http://www.csci.csusb.edu/dick/samples/c.syntax.html`, .See http://www.csci.csusb.edu/dick/samples/c.syntax.html $C. .Open 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++. .See http://www.csci.csusb.edu/dick/c++std/ .Close Note 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: $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: comment::=$C.$comment | $inline_comment. inline_comment::="//" #(non(eoln))eoln, . Lexemes pre_processor_tokens::= .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html#preprocessor_token keyword::= .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html#keyword punctuation::= .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html#punctuation operators::= .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html#operators identifier::= .See http://www.csci.csusb.edu/dick/samples/c++.glossary.html#identifier . Declarations declaration::= O($declaration_specifiers ) O($declarator_list ) | function_declaration | $linkage_specification, declaration_specifiers::= N($declaration_specifier) , declaration_specifier::= $storage_class_specifier | $type_specifier | $function_specifier | "friend" | "typedef", storage_class_specifier::= "auto" | "register" | "static" | "extern", function_specifier::= "inline" | "virtual", type_specifier::= $simple_type_name | $class_specifier | $enumeration_specifier | $elaborated_type_specifier | "const" | "volatile", simple_type_name::= class_name | "typedef" name | "char" | "short" | "int" | "long" | "signed" | "unsigned" | "float" | "double" | "void", elaborated_type_specifier::= $class_key $identifier | $class_key class_name | "enum" enumeration_name, class_key::= "class" | "struct" | "$union", enumeration_specifier::= C.enumeration_specifier, |- C.enumeration_specifier=enumeration_identifier | "enum" $identifier "{"enumeration_list"}", linkage_specification::= "extern" string "{" O($declaration_list ) "}" | "extern" string_declaration, declaration_list::= $declaration #(";" $declaration), class_specifier::= $class_head "{" O($member_list ) "}", class_head::= $class_key O($identifier ) O($base_specifier ) | $class_key class_name O($base_specification ), member_list::= #( O($access_specifier colon ) #$member_declaration ), member_declaration::= O($declaration_specifiers) O($member_declarator_list ) ";" | function_definition O(";") | qualified_name ";", type_description::= class | struct | $union, simple_class::="class" $identifier "{" $private_part $explicit_access_part"}", structure::=struct, simple_struct::="struct" $identifier "{" $public_part "private:" $explicit_access_part "}" union::="$union" O($identifier) "{" $public_part "}", explicit_access_part::= "private" colon $private_part | "public" colon $public_part | "protected" colon $protected_part, private_part::=#$member_declaration, public_part::=#$member_declaration, protected_part::=#$member_declaration. member_declarator_list::= List( $member_declarator), member_declarator::= $declarator O($pure_specifier ) | O($identifier ) colon constant_expression, pure_specifier::= "=" "0", base_specification::= colon List($base_specifier), base_specifier::= class_name | "virtual" O($access_specifier ) class_name | $access_specifier O("virtual" ) class_name, access_specifier::= "private" | "protected" | "public", conversion_function_name::= "$operator" $conversion_type_name, constructor_declarations::= ... destructor_declarations::= ... constructor_initializer::= colon $memory_initializer_list, memory_initializer_list::= List( $memory_initializer), memory_initializer::= class_name "(" O(argument_list ) ")" | $identifier "(" O(argument_list ) ")", . Operators and Variables operator_function_name::= "$operator" $operator, scope_resolution_operator::lexeme="::". double_colon::=scope_resolution_operator. DC::abbreviation=double_colon. variable::=$C.$variable | class_name $DC static_data_member. . Expressions expression::=$C.expression | $multiplicative_expression. multiplicative_expression::=$pm_expression #(multiplicative_operator $pm_expression). pm_expression::=cast_expression #($cast_operator cast_expression). cast_operator::= ".*" | "->*". unary_expression::= $C.$unary_expression | $allocation_expression | $deallocation_expression. allocation_expression::= O($DC) "new" O($placement) $new_type_name O(new_initializer) | O($DC) "new" O($placement) ($type_name) O(new_initializer) deallocation_expression::= O($DC) "delete" O("[]") cast_expression. placement::="(" expression_list ")". new_type_name::= N($type_specifier) $new_declarator. new_declarator::= O(complete_class_name $DC )"*" $const_or_volatile) new_delarator | O(new-$declarator) "[" expression "]". asm_declaration::= "asm" "{" string_literal "}" ";" . . Declarators declarator_list::=List($declarator O($initializer)). declarator::= $dname | $ptr_operator $declarator | $declarator ( "(" $argument_declaration_list")" $const_or_volatile | "[" O(constant_expression)"]" | "(" $declarator ")". ptr_operator::= ("*" | "&"| complete_class_name $DC "*") $const_or_volatile. const_or_volatile::= "const" | O("volatile"). dname::= name| O("~") class_name | typedef_name | qualified_type_name. type_name::= N($type_specifier) O($abstract_declarator). abstract_declarator::=$ptr_operator O($abstract_declarator)| O($abstract_declarator) ( "(" $argument_declaration_list")" $const_or_volatile | "[" O(constant_expression)"]" | "(" $abstract_declarator ")". argument_declaration_list::=O(arg_declaration_list) (O("...") | O( "," "...") ). arg_decalration_list::=List(argument_declaration). arg_decalarion::= decl_specifiers ( $declarator O("=" expression) | O($abstract_declarator)O("=" expression)). fct_body::=$compound_statement. initializer::= "=" ( assignemnt_expression | "{" $initializer_list O(comma) "}") "(" expression_list ")". 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)). base_spec::= colon List($base_specifier). conversion_type_name::=N($type_specifier) O(ptr-$operator). type_specifier_list::=N($type_specifier). ctor::=constructor. mem::=member. ctor_initializer::=$constructor_initializer::=colon member_initializer_list. mem_initializer_list::=member_initializer_list::=List(member_initializer). mem_initializer::=member_initializer::=(complete_class_name | $identifier) "(" O(List($expression)) ")". operator::lexeme= "new" | "delete" | "+" | "-" | "*" | "/" | "%" | "^" | "&" | "~" | "!" | "=" | "<" | ">" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" | "|=" | "<<" | ">>" | "<<=" | ">>=" | "==" | "++" | "--" | "," | "->*" | "!=" | "<=" | ">=" |"->" | "()" | "[]" . . Statements statement::= $C.$statement | $declaration_statement | $try_block. if_statement::=$C.if_statement. compound_statement::="{" #$statement "}". block::=$compound_statement & `with at least one $declaration statemnt`. declaration_statement::= $declaration. for_init_statement::= expression_statement | $declaration_statement. iteration_statement::="while" "(" $expression ")" $statement | "do" $statement "while" "(" $expression ")" ";" | $for_statement. for_statement::="for" "(" $for_init_statement O($expression) ";" O($expression) ")" $statement. -- thanks to Ben Evans for pointing out a typographical error here. . Templates template_declaration::= "template" "<" List($template_argument) ">" $declaration. template_argument::=$type_argument | argument_deeclaration. type_argument::="class" $identifier. template_clase_name::= template_name "<" List($template_arg) ">". template_arg::=$expression| $type_name. .Exception Handling try_block::= "try" $compound_statement #$handler. handler::= "catch" "(" $exception_declaration ")" $compound_statement. exception_declaration::= "..." | N($type_specifier) O($declarator | abstarct_declarator). throw_expression::= "throw" O(assignment_expression). exception_specification::= "throw" "(" List(type) ")". ... . Namespaces: .Source //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: namespace_definition::="namespace" $identifier "{" O( $declaration_list) "}". namespace_alias_definition::= "namespace" $identifier "=" $namespace_name ";". using_declaration::= "using" $namespace_name "::" $identifier ";". using_directive::= "using" "namespace" $namespace_name ";". 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. .Close The Syntax of C++