ISO/IEC
Annex A Grammar summary
ISO/IEC 14882:2003(E)
A Grammar summary
[gram]
Annex A
(informative)
Grammar summary
1
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. Disam-
biguation rules (6.8, 7.1, 10.2) must be applied to distinguish expressions from declarations. Further,
access control, ambiguity, and type rules must be used to weed out syntactically valid but meaningless con-
structs.
A.1 Keywords
[gram.key]
New context-dependent keywords are introduced into a program by typedef (7.1.3), namespace (7.3.1),
class (clause 9), enumeration (7.2), and template (clause 14) declarations.
typedef-name:
identifier
namespace-name:
original-namespace-name
namespace-alias
original-namespace-name:
identifier
namespace-alias:
identifier
class-name:
identifier
template-id
enum-name:
identifier
template-name:
identifier
Note that a typedef-name naming a class is also a class-name (9.1).
A.2 Lexical conventions
hex-quad:
hexadecimal-digit hexadecimal-digit hexadecimal-digit hexadecimal-digit
[gram.lex]
universal-character-name:
\u hex-quad
\U hex-quad hex-quad
675
ISO/IEC
Annex A Grammar summary
ISO/IEC 14882:2003(E)
A.2 Lexical conventions
preprocessing-token:
header-name
identifier
pp-number
character-literal
string-literal
preprocessing-op-or-punc
each non-white-space character that cannot be one of the above
token:
identifier
keyword
literal
operator
punctuator
header-name:
"q-char-sequence"
h-char-sequence:
h-char
h-char-sequence h-char
h-char:
any member of the source character set except
new-line and >
q-char-sequence:
q-char
q-char-sequence q-char
q-char:
pp-number:
identifier:
any member of the source character set except
new-line and "
digit
. digit
pp-number digit
pp-number nondigit
pp-number e sign
pp-number E sign
pp-number .
nondigit
identifier nondigit
identifier digit
nondigit: one of
universal-character-name
_ 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
676
ISO/IEC
Annex A Grammar summary
preprocessing-op-or-punc: one of
{
<:
new
+
!
ˆ=
<=
and
or
}
:>
delete ?
-
*
<
=
|=
&=
>=
&&
and_eq bitand bitor
or_eq
xor_eq
]
%>
::
/
>
<<
||
[
<%
xor
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
ISO/IEC 14882:2003(E)
A.2 Lexical conventions
#
%:
.
%
+=
>>
++
compl
##
%:%:
.*
ˆ
-=
>>=
--
not
(
;
&
*=
<<=
,
not_eq
)
:
|
/=
==
->*
...
˜
%=
!=
->
677
ISO/IEC
Annex A Grammar summary
ISO/IEC 14882:2003(E)
A.2 Lexical conventions
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
universal-character-name
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-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
floating-literal:
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"
678
ISO/IEC 14882:2003(E)
A.2 Lexical conventions
ISO/IEC
Annex A Grammar summary
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
universal-character-name
boolean-literal:
false
true
A.3 Basic concepts
translation-unit:
declaration-seqopt
A.4 Expressions
primary-expression:
literal
this
( expression )
id-expression
id-expression:
unqualified-id
qualified-id
unqualified-id:
identifier
operator-function-id
conversion-function-id
˜ class-name
template-id
qualified-id:
::opt nested-name-specifier templateopt unqualified-id
:: identifier
:: operator-function-id
:: template-id
nested-name-specifier:
class-or-namespace-name :: nested-name-specifieropt
class-or-namespace-name :: template nested-name-specifier
class-or-namespace-name:
class-name
namespace-name
[gram.basic]
[gram.expr]
679
ISO/IEC 14882:2003(E)
A.4 Expressions
postfix-expression:
ISO/IEC
Annex A Grammar summary
primary-expression
postfix-expression [ expression ]
postfix-expression ( expression-listopt )
simple-type-specifier ( expression-listopt )
typename ::opt nested-name-specifier identifier ( expression-listopt )
typename ::opt nested-name-specifier templateopt template-id ( expression-listopt )
postfix-expression . templateopt id-expression
postfix-expression -> templateopt id-expression
postfix-expression . pseudo-destructor-name
postfix-expression -> pseudo-destructor-name
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
pseudo-destructor-name:
::opt nested-name-specifieropt type-name :: ˜ type-name
::opt nested-name-specifier template template-id :: ˜ type-name
::opt nested-name-specifieropt ˜ type-name
unary-expression:
postfix-expression
++ cast-expression
-- cast-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:
ptr-operator new-declaratoropt
direct-new-declarator
direct-new-declarator:
[ expression ]
direct-new-declarator [ constant-expression ]
680
ISO/IEC
Annex A Grammar summary
ISO/IEC 14882:2003(E)
A.4 Expressions
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
681
ISO/IEC 14882:2003(E)
A.4 Expressions
ISO/IEC
Annex A Grammar summary
[gram.stmt.stmt]
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
logical-or-expression assignment-operator assignment-expression
throw-expression
assignment-operator: one of
= *= /= %= += -= >>= <<= &= ˆ= |=
expression:
assignment-expression
expression , assignment-expression
constant-expression:
conditional-expression
A.5 Statements
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
682