Browsable C++98 Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: cpp/cpp98/iso-14882-1998/extracted
Source used for this grammar: ISO/IEC 14882:1998(E), Programming languages — C++, First edition, September 1998, Annex A, pages 667–684

Summary

Syntax

translation-unit ::=
	declaration-seq?
declaration-seq ::=
	declaration
	declaration-seq declaration
declaration ::=
	block-declaration
	function-definition
	template-declaration
	explicit-instantiation
	explicit-specialization
	linkage-specification
	namespace-definition
block-declaration ::=
	simple-declaration
	asm-definition
	namespace-alias-definition
	using-declaration
	using-directive
simple-declaration ::=
	decl-specifier-seq? init-declarator-list? ";"
decl-specifier-seq ::=
	decl-specifier-seq? decl-specifier
decl-specifier ::=
	storage-class-specifier
	type-specifier
	function-specifier
	"friend"
	"typedef"
storage-class-specifier ::=
	"auto"
	"register"
	"static"
	"extern"
	"mutable"
type-specifier ::=
	simple-type-specifier
	class-specifier
	enum-specifier
	elaborated-type-specifier
	cv-qualifier
simple-type-specifier ::=
	"::"? nested-name-specifier? type-name
	"::"? nested-name-specifier "template" template-id
	"char"
	"wchar_t"
	"bool"
	"short"
	"int"
	"long"
	"signed"
	"unsigned"
	"float"
	"double"
	"void"
nested-name-specifier ::=
	class-or-namespace-name "::" nested-name-specifier?
	class-or-namespace-name "::" "template" nested-name-specifier
class-or-namespace-name ::=
	class-name
	namespace-name
class-name ::=
	identifier
	template-id
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
	id-expression
assignment-expression ::=
	conditional-expression
	logical-or-expression assignment-operator assignment-expression
	throw-expression
conditional-expression ::=
	logical-or-expression
	logical-or-expression "?" expression ":" assignment-expression
logical-or-expression ::=
	logical-and-expression
	logical-or-expression "||" logical-and-expression
logical-and-expression ::=
	inclusive-or-expression
	logical-and-expression "&&" inclusive-or-expression
inclusive-or-expression ::=
	exclusive-or-expression
	inclusive-or-expression "|" exclusive-or-expression
exclusive-or-expression ::=
	and-expression
	exclusive-or-expression "^" and-expression
and-expression ::=
	equality-expression
	and-expression "&" equality-expression
equality-expression ::=
	relational-expression
	equality-expression "==" relational-expression
	equality-expression "!=" relational-expression
relational-expression ::=
	shift-expression
	relational-expression "<" shift-expression
	relational-expression ">" shift-expression
	relational-expression "<=" shift-expression
	relational-expression ">=" shift-expression
shift-expression ::=
	additive-expression
	shift-expression "<<" additive-expression
	shift-expression ">>" additive-expression
additive-expression ::=
	multiplicative-expression
	additive-expression "+" multiplicative-expression
	additive-expression "-" multiplicative-expression
multiplicative-expression ::=
	pm-expression
	multiplicative-expression "*" pm-expression
	multiplicative-expression "/" pm-expression
	multiplicative-expression "%" pm-expression
pm-expression ::=
	cast-expression
	pm-expression ".*" cast-expression
	pm-expression "->*" cast-expression
cast-expression ::=
	unary-expression
	"(" type-id ")" cast-expression
unary-expression ::=
	postfix-expression
	"++" cast-expression
	"--" cast-expression
	unary-operator cast-expression
	"sizeof" unary-expression
	"sizeof" "(" type-id ")"
	new-expression
	delete-expression
postfix-expression ::=
	primary-expression
	postfix-expression "[" expression "]"
	postfix-expression "(" expression-list? ")"
	simple-type-specifier "(" expression-list? ")"
	"typename" "::"? nested-name-specifier identifier "(" expression-list? ")"
	"typename" "::"? nested-name-specifier "template"? template-id "(" expression-list? ")"
	postfix-expression "." "template"? id-expression
	postfix-expression "->" "template"? 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 ")"
primary-expression ::=
	literal
	"this"
	"(" expression ")"
	id-expression
expression ::=
	assignment-expression
	expression "," assignment-expression
id-expression ::=
	unqualified-id
	qualified-id
unqualified-id ::=
	identifier
	operator-function-id
	conversion-function-id
	"~" class-name
	template-id
operator-function-id ::=
	"operator" operator
operator ::=
	"new"
	"delete"
	"new" "[" "]"
	"delete" "[" "]"
	"+"
	"-"
	"*"
	"/"
	"%"
	"^"
	"&"
	"|"
	"~"
	"!"
	"="
	"<"
	">"
	"+="
	"-="
	"*="
	"/="
	"%="
	"^="
	"&="
	"|="
	"<<"
	">>"
	">>="
	"<<="
	"=="
	"!="
	"<="
	">="
	"&&"
	"||"
	"++"
	"--"
	","
	"->*"
	"->"
	"(" ")"
	"[" "]"
conversion-function-id ::=
	"operator" conversion-type-id
conversion-type-id ::=
	type-specifier-seq conversion-declarator?
type-specifier-seq ::=
	type-specifier type-specifier-seq?
conversion-declarator ::=
	ptr-operator conversion-declarator?
ptr-operator ::=
	"*" cv-qualifier-seq?
	"&"
	"::"? nested-name-specifier "*" cv-qualifier-seq?
cv-qualifier-seq ::=
	cv-qualifier cv-qualifier-seq?
cv-qualifier ::=
	"const"
	"volatile"
qualified-id ::=
	"::"? nested-name-specifier "template"? unqualified-id
	"::" identifier
	"::" operator-function-id
	"::" template-id
expression-list ::=
	assignment-expression
	expression-list "," assignment-expression
pseudo-destructor-name ::=
	"::"? nested-name-specifier? type-name "::" "~" type-name
	"::"? nested-name-specifier "template" template-id "::" "~" type-name
	"::"? nested-name-specifier? "~" type-name
type-name ::=
	class-name
	enum-name
	typedef-name
enum-name ::=
	identifier
typedef-name ::=
	identifier
type-id ::=
	type-specifier-seq abstract-declarator?
abstract-declarator ::=
	ptr-operator abstract-declarator?
	direct-abstract-declarator
direct-abstract-declarator ::=
	direct-abstract-declarator? "(" parameter-declaration-clause ")" cv-qualifier-seq? exception-specification?
	direct-abstract-declarator? "[" constant-expression? "]"
	"(" abstract-declarator ")"
parameter-declaration-clause ::=
	parameter-declaration-list? "..."?
	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-declarator?
	decl-specifier-seq abstract-declarator? "=" assignment-expression
declarator ::=
	direct-declarator
	ptr-operator declarator
direct-declarator ::=
	declarator-id
	direct-declarator "(" parameter-declaration-clause ")" cv-qualifier-seq? exception-specification?
	direct-declarator "[" constant-expression? "]"
	"(" declarator ")"
declarator-id ::=
	id-expression
	"::"? nested-name-specifier? type-name
exception-specification ::=
	"throw" "(" type-id-list? ")"
type-id-list ::=
	type-id
	type-id-list "," type-id
constant-expression ::=
	conditional-expression
unary-operator ::=
	"*"
	"&"
	"+"
	"-"
	"!"
	"~"
new-expression ::=
	"::"? "new" new-placement? new-type-id new-initializer?
	"::"? "new" new-placement? "(" type-id ")" new-initializer?
new-placement ::=
	"(" expression-list ")"
new-type-id ::=
	type-specifier-seq new-declarator?
new-declarator ::=
	ptr-operator new-declarator?
	direct-new-declarator
direct-new-declarator ::=
	"[" expression "]"
	direct-new-declarator "[" constant-expression "]"
new-initializer ::=
	"(" expression-list? ")"
delete-expression ::=
	"::"? "delete" cast-expression
	"::"? "delete" "[" "]" cast-expression
assignment-operator ::=
	"="
	"*="
	"/="
	"%="
	"+="
	"-="
	">>="
	"<<="
	"&="
	"^="
	"|="
throw-expression ::=
	"throw" assignment-expression?
namespace-name ::=
	original-namespace-name
	namespace-alias
original-namespace-name ::=
	identifier
namespace-alias ::=
	identifier
class-specifier ::=
	class-head "{" member-specification? "}"
class-head ::=
	class-key identifier? base-clause?
	class-key nested-name-specifier identifier base-clause?
	class-key nested-name-specifier? template-id base-clause?
class-key ::=
	"class"
	"struct"
	"union"
base-clause ::=
	":" base-specifier-list
base-specifier-list ::=
	base-specifier
	base-specifier-list "," base-specifier
base-specifier ::=
	"::"? nested-name-specifier? class-name
	"virtual" access-specifier? "::"? nested-name-specifier? class-name
	access-specifier "virtual"? "::"? nested-name-specifier? class-name
access-specifier ::=
	"private"
	"protected"
	"public"
member-specification ::=
	member-declaration member-specification?
	access-specifier ":" member-specification?
member-declaration ::=
	decl-specifier-seq? member-declarator-list? ";"
	function-definition ";"?
	"::"? nested-name-specifier "template"? unqualified-id ";"
	using-declaration
	template-declaration
member-declarator-list ::=
	member-declarator
	member-declarator-list "," member-declarator
member-declarator ::=
	declarator pure-specifier?
	declarator constant-initializer?
	identifier? ":" constant-expression
pure-specifier ::=
	"=" "0"
constant-initializer ::=
	"=" constant-expression
function-definition ::=
	decl-specifier-seq? declarator ctor-initializer? function-body
	decl-specifier-seq? declarator function-try-block
ctor-initializer ::=
	":" mem-initializer-list
mem-initializer-list ::=
	mem-initializer
	mem-initializer "," mem-initializer-list
mem-initializer ::=
	mem-initializer-id "(" expression-list? ")"
mem-initializer-id ::=
	"::"? nested-name-specifier? class-name
	identifier
function-body ::=
	compound-statement
compound-statement ::=
	"{" statement-seq? "}"
statement-seq ::=
	statement
	statement-seq statement
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 ::=
	expression? ";"
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 condition? ";" expression? ")" statement
for-init-statement ::=
	expression-statement
	simple-declaration
jump-statement ::=
	"break" ";"
	"continue" ";"
	"return" expression? ";"
	"goto" identifier ";"
declaration-statement ::=
	block-declaration
try-block ::=
	"try" compound-statement handler-seq
handler-seq ::=
	handler handler-seq?
handler ::=
	"catch" "(" exception-declaration ")" compound-statement
exception-declaration ::=
	type-specifier-seq declarator
	type-specifier-seq abstract-declarator
	type-specifier-seq
	"..."
function-try-block ::=
	"try" ctor-initializer? function-body handler-seq
using-declaration ::=
	"using" "typename"? "::"? nested-name-specifier unqualified-id ";"
	"using" "::" unqualified-id ";"
template-declaration ::=
	"export"? "template" "<" template-parameter-list ">" declaration
template-parameter-list ::=
	template-parameter
	template-parameter-list "," template-parameter
template-parameter ::=
	type-parameter
	parameter-declaration
type-parameter ::=
	"class" identifier?
	"class" identifier? "=" type-id
	"typename" identifier?
	"typename" identifier? "=" type-id
	"template" "<" template-parameter-list ">" "class" identifier?
	"template" "<" template-parameter-list ">" "class" identifier? "=" id-expression
enum-specifier ::=
	"enum" identifier? "{" enumerator-list? "}"
enumerator-list ::=
	enumerator-definition
	enumerator-list "," enumerator-definition
enumerator-definition ::=
	enumerator
	enumerator "=" constant-expression
enumerator ::=
	identifier
elaborated-type-specifier ::=
	class-key "::"? nested-name-specifier? identifier
	"enum" "::"? nested-name-specifier? identifier
	"typename" "::"? nested-name-specifier identifier
	"typename" "::"? nested-name-specifier "template"? template-id
function-specifier ::=
	"inline"
	"virtual"
	"explicit"
init-declarator-list ::=
	init-declarator
	init-declarator-list "," init-declarator
init-declarator ::=
	declarator initializer?
initializer ::=
	"=" initializer-clause
	"(" expression-list ")"
initializer-clause ::=
	assignment-expression
	"{" initializer-list ","? "}"
	"{" "}"
initializer-list ::=
	initializer-clause
	initializer-list "," initializer-clause
asm-definition ::=
	"asm" "(" string-literal ")" ";"
namespace-alias-definition ::=
	"namespace" identifier "=" qualified-namespace-specifier ";"
qualified-namespace-specifier ::=
	"::"? nested-name-specifier? namespace-name
using-directive ::=
	"using" "namespace" "::"? nested-name-specifier? namespace-name ";"
explicit-instantiation ::=
	"template" declaration
explicit-specialization ::=
	"template" "<" ">" declaration
linkage-specification ::=
	"extern" string-literal "{" declaration-seq? "}"
	"extern" string-literal declaration
namespace-definition ::=
	named-namespace-definition
	unnamed-namespace-definition
named-namespace-definition ::=
	original-namespace-definition
	extension-namespace-definition
original-namespace-definition ::=
	"namespace" identifier "{" namespace-body "}"
namespace-body ::=
	declaration-seq?
extension-namespace-definition ::=
	"namespace" original-namespace-name "{" namespace-body "}"
unnamed-namespace-definition ::=
	"namespace" "{" namespace-body "}"

GrammarLabMaintained by Dr. Vadim Zaytsev a.k.a. @grammarware. Last updated in September 2015. []