Preprocessor grammar summary (C/C++)

This article describes the formal grammar of the C and C++ preprocessor. It covers the syntax of preprocessing directives and operators. For more information, see Preprocessor and Pragma directives and the __pragma and _Pragma keywords.

Definitions for the grammar summary

Terminals are endpoints in a syntax definition. No other resolution is possible. Terminals include the set of reserved words and user-defined identifiers.

Nonterminals are placeholders in the syntax. Most are defined elsewhere in this syntax summary. Definitions can be recursive. The following nonterminals are defined in the Lexical conventions section of the C++ Language Reference:

constant, constant-expression, identifier, keyword, operator, punctuator

An optional component is indicated by the subscripted opt. For example, the following syntax indicates an optional expression enclosed in curly braces:

{ expressionopt }

Document conventions

The conventions use different font attributes for different components of the syntax. The symbols and fonts are as follows:

Attribute Description
nonterminal Italic type indicates nonterminals.
#include Terminals in bold type are literal reserved words and symbols that must be entered as shown. Characters in this context are always case sensitive.
opt Nonterminals followed by opt are always optional.
default typeface Characters in the set described or listed in this typeface can be used as terminals in statements.

A colon (:) following a nonterminal introduces its definition. Alternative definitions are listed on separate lines.

In code syntax blocks, these symbols in the default typeface have a special meaning:

Symbol Description
[ ] Square brackets surround an optional element.
{ | } Curly braces surround alternative elements, separated by vertical bars.
... Indicates the previous element pattern can be repeated.

In code syntax blocks, commas (,), periods (.), semi-colons (;), colons (:), parentheses (( )), double-quotes ("), and single-quotes (') are literals.

Preprocessor grammar

control-line:
#define identifier token-stringopt
#define identifier ( identifieropt , ... , identifieropt ) token-stringopt
#include "path-spec"
#include <path-spec>
#line digit-sequence "filename"opt
#undef identifier
#error token-string
#pragma token-string

constant-expression:
defined( identifier )
defined identifier
 any other constant expression

conditional:
if-part elif-partsopt else-partopt endif-line

if-part:
if-line text

if-line:
#if constant-expression
#ifdef identifier
#ifndef identifier

elif-parts:
elif-line text
elif-parts elif-line text

elif-line:
#elif constant-expression

else-part:
else-line text

else-line:
#else

endif-line:
#endif

digit-sequence:
digit
digit-sequence digit

digit: one of
0 1 2 3 4 5 6 7 8 9

token-string:
 String of token

token:
keyword
identifier
constant
operator
punctuator

filename:
 Legal operating system filename

path-spec:
 Legal file path

text:
 Any sequence of text

Note

The following nonterminals are expanded in the Lexical conventions section of the C++ Language Reference: constant, constant-expression, identifier, keyword, operator, and punctuator.

See also

C/C++ preprocessor reference