Skip to content
This repository has been archived by the owner on Apr 21, 2019. It is now read-only.

C++11 grammar regex rule

kennytm edited this page Sep 25, 2011 · 1 revision

C++11 Grammar Rules

BINARY-SEPARATED

BINARY-SEPARATED{T, S} = T ( ( S ) T ) *

COMMA-SEPARATED

COMMA-SEPARATED{T} = BINARY-SEPARATED{ T, , }

COMMA-SEPARATED-DDD

COMMA-SEPARATED-DDD{T} = COMMA-SEPARATED{ T ...? }

COMMA-TRAILED-OPT

COMMA-TRAILED-OPT{T} = ( COMMA-SEPARATED{ T } ,? )?

ATTRIB-SEQ

ATTRIB-SEQ{T} = T+ attribute-specifier*

ATTRIB-SEQ-BOTH

ATTRIB-SEQ-BOTH{T} = attribute-specifier* ATTRIB-SEQ{ T }

keyword

alignas | alignof | asm | auto | bool | break | case | catch | char | char16_t | char32_t | class | const | constexpr | const_cast | continue | decltype | default | delete | do | double | dynamic_cast | else | enum | explicit | export | extern | false | float | for | friend | goto | if | inline | int | long | mutable | namespace | new | noexcept | nullptr | operator | private | protected | public | register | reinterpret_cast | return | short | signed | sizeof | static | static_assert | static_cast | struct | switch | template | this | thread_local | throw | true | try | typedef | typeid | typename | union | unsigned | using | virtual | void | volatile | wchar_t | while

universal-character-name

/\\u[0-9a-f]{4}|\\U[0-9a-f]{8}/i

preprocessing-token

header-name | identifier | pp-number | character-literal | user-defined-character-literal | string-literal | user-defined-string-literal | preprocessing-op-or-punc | /\S/

token

identifier | keyword | literal | preprocessing-op-or-punc

header-name

/<[^\n>]+>|"[^\n"]+"/

pp-number

/\.?\d/ ( /e[-+]|[\w.]/i | universal-character-name )*

identifier

( /[_a-z]/i | universal-character-name ) ( /\w/ | universal-character-name )*

preprocessing-op-or-punc

{ | } | [ | ] | # | ## | ( | ) | <: | :> | <% | %> | %: | %:%: | ; | : | ... | new | delete | ? | :: | . | .* | + | - | * | / | % | ^ | & | | | ~ | ! | = | < | > | += | -= | *= | /= | %= | ^= | &= | |= | << | >> | >>= | <<= | == | != | and | and_eq | bitand | bitor | compl | not | not_eq | or | or_eq | xor | xor_eq

literal

integer-literal | character-literal | floating-literal | string-literal | boolean-literal | pointer-literal | user-defined-literal

integer-literal

integer-literal-without-suffix /ul{0,2}|l{1,2}u?/i?

integer-literal-without-suffix

/[1-9]\d*|0[0-7]*|0x[0-9a-f]+/i

character-literal

/[uUL]?'/ ( /[^'\n\\]/ | escape-sequence | universal-character-name)+ '

escape-sequence

/\\['"?\\abfnrtv]|\\[0-7]{1,3}|\\x[0-9a-fA-F]{1,2}/

floating-literal

floating-literal-without-suffix /[fl]/i?

floating-literal-without-suffix

/(\d*\.\d+|\d+\.)(e[-+]?\d+)|(\d+e[-+]?\d+)/i

string-literal

/(u8|[uUL])?/ ( " ( /[^"\n\\]/ | escape-sequence | universal-character-name )* " | R raw-string )

raw-string

/"([^s()\\]*)\(.*?\)\1"/

Note that "\1" here is a back-reference.

boolean-literal

true | false

pointer-literal

nullptr

user-defined-literal

user-defined-integer-literal | user-defined-floating-literal | user-defined-string-literal | user-defined-character-literal

user-defined-integer-literal

integer-literal-without-suffix ud-suffix

user-defined-floating-literal

floating-literal-without-suffix ud-suffix

user-defined-string-literal

string-literal ud-suffix

user-defined-character-literal

character-literal ud-suffix

ud-suffix

identifier

translation-unit

declaration*

primary-expression

literal | this | ( expression ) | id-expression | lambda-expression

id-expression

unqualified-id | qualified-id

unqualified-id

identifier | operator-function-id | conversion-function-id | literal-operator-id | ~ class-name | ~ decltype-specifier | template-id

qualified-id

One of:

nested-name-specifier

( type-name | namespace-name | decltype-specifier ) :: ( ( identifier | template? simple-template-id ) :: )*

lambda-expression

lambda-introducer lambda-declarator? compound-statement

lambda-introducer

[ lambda-capture? ]

lambda-capture

One of:

capture-default

& | =

capture

&? identifier | this

lambda-declarator

( parameter-declaration-clause ) mutable? exception-specification? attribute-specifier* trailing-return-type?

postfix-expression

postfix-expression-head postfix-expression-tail*

postfix-expression-head

One of:

postfix-expression-tail

One of:

expression-list

initializer-list

pseudo-destructor-name

One of:

unary-expression

sizeof* unary-expression-last

unary-expression-last

One of:

unary-operator

* | & | + | - | ! | ~

new-expression

::? new new-placement? ( new-type-id | ( type-id ) ) new-initializer?

new-placement

( expression-list )

new-type-id

ATTRIB-SEQ{ type-specifier } new-declarator?

new-declarator

ptr-operator* noptr-new-declarator

noptr-new-declarator

[ expression ] attribute-specifier* ( [ constant-expression ] attribute-specifier* )*

new-initializer

( expression-list? ) | braced-init-list

delete-expression

::? delete ( [ ] )? cast-expression

noexcept-expression

noexcept ( expression )

cast-expression

( ( type-id ) )* unary-expression

pm-expression

BINARY-SEPARATED{ cast-expression, .* | ->* }

multiplicative-expression

BINARY-SEPARATED{ pm-expression, * | / | % }

additive-expression

BINARY-SEPARATED{ multiplicative-expression, + | - }

shift-expression

BINARY-SEPARATED{ additive-expression, << | >> }

relational-expression

BINARY-SEPARATED{ shift-expression, < | > | <= | >= }

equality-expression

BINARY-SEPARATED{ relational-expression, == | != }

and-expression

BINARY-SEPARATED{ equality-expression, & }

exclusive-or-expression

BINARY-SEPARATED{ and-expression, ^ }

inclusive-or-expression

BINARY-SEPARATED{ exclusive-or-expression, | }

logical-and-expression

BINARY-SEPARATED{ inclusive-or-expression, && }

logical-or-expression

BINARY-SEPARATED{ logical-and-expression, || }

conditional-expression

logical-or-expression ( ? expression : assignment-expression )?

assignment-expression

conditional-expression | logical-or-expression assignment-operator initializer-clause | throw-expression

assignment-operator

= | *= | /= | %= | += | -= | >>= | <<= | &= | ^= | |=

expression

COMMA-SEPARATED{ assignment-expression }

constant-expression

conditional-expression

statement

labeled-statement | declaration-statement | attribute-specifier* ( expression-statement | compound-statement | selection-statement | iteration-statement | jump-statement | try-block )

labeled-statement

attribute-specifier* ( identifier | case constant-expression | default ) : statement

expression-statement

expression? ;

compound-statement

{ statement* }

selection-statement

One of:

condition

One of:

iteration-statement

One of:

for-init-statement

expression-statement | simple-declaration

for-range-declaration

ATTRIB-SEQ-BOTH{ type-specifier } declarator

for-range-initializer

expression | braced-init-list

jump-statement

( break | continue | return ( expression? | braced-init-list ) | goto identifier ) ;

declaration-statement

block-declaration

declaration

block-declaration | function-definition | template-declaration | explicit-instantiation | explicit-specialization | linkage-specification | namespace-definition | empty-declaration | attribute-declaration

block-declaration

simple-declaration | asm-definition | namespace-alias-definition | using-declaration | static_assert-declaration | alias-declaration | opaque-enum-declaration

alias-declaration

using identifier = type-id ;

simple-declaration

ATTRIB-SEQ-BOTH{ decl-specifier }? init-declarator-list? ;

static_assert-declaration

static_assert ( constant-expression , string-literal ) ;

empty-declaration

;

attribute-declaration

attribute-specifier+ ;

decl-specifier

storage-class-specifier | type-specifier | function-specifier | friend | typedef | constexpr

storage-class-specifier

register | static | thread_local | extern | mutable

function-specifier

inline | virtual | explicit

typedef-name

identifier

type-specifier

trailing-type-specifier | class-specifier | enum-specifier

trailing-type-specifier

simple-type-specifier | elaborated-type-specifier | typename-specifier | cv-qualifier

simple-type-specifier

::? ( nested-name-specifier? type-name | nested-name-specifier template simple-template-id ) | char | char16_t | char32_t | wchar_t | bool | short | int | long | signed | unsigned | float | double | void | auto | decltype-specifier

type-name

class-name | enum-name | typedef-name | simple-template-id

decltype-specifier

decltype ( expression )

elaborated-type-specifier

One of:

enum-name

identifier

enum-specifier

enum-head { COMMA-TRAILED-OPT{ enumerator-definition } }

enum-head

enum-key attribute-specifier* ( identifier? | nested-name-specifier identifier ) enum-base?

opaque-enum-declaration

enum-key attribute-specifier* identifier enum-base? ;

enum-key

enum ( class | struct )?

enum-base

: ATTRIB-SEQ{ type-specifier }

enumerator-definition

enumerator ( = constant-expression )?

enumerator

identifier

namespace-name

original-namespace-name | namespace-alias

original-namespace-name

identifier

namespace-definition

named-namespace-definition | unnamed-namespace-definition

named-namespace-definition

original-namespace-definition | extension-namespace-definition

original-namespace-definition

inline? namespace identifier { namespace-body }

extension-namespace-definition

inline? namespace original-namespace-name { namespace-body }

unnamed-namespace-definition

inline? namespace { namespace-body }

namespace-body

declaration*

namespace-alias

identifier

namespace-alias-definition

namespace identifier = qualified-namespace-specifier ;

qualified-namespace-specifier

::? nested-name-specifier? namespace-name

using-declaration

using ( typename? ::? nested-name-specifier | :: ) unqualified-id ;

using-directive

attribute-specifier* using namespace ::? nested-name-specifier? namespace-name ;

asm-definition

asm ( string-literal ) ;

linkage-specification

extern string-literal ( { declaration* } | declaration )

attribute-specifier

One of:

alignment-specifier

alignas ( (type-id | assignment-expression) ...? )

attribute

attribute-token attribute-argument-clause?

attribute-token

identifier | attribute-scoped-token

attribute-scoped-token

attribute-namespace :: identifier

attribute-namespace

identifier

attribute-argument-clause

( token+ )

The list of tokens should contain balanced (...), [...] and {...}.

init-declarator-list

COMMA-SEPARATED{ init-declarator }

init-declarator

declarator initializer?

declarator

ptr-declarator | noptr-declarator parameters-and-qualifiers trailing-return-type

ptr-declarator

ptr-operator* noptr-declarator

noptr-declarator

( declarator-id attribute-specifier* | ( ptr-declarator ) ) ( parameters-and-qualifiers | [ constant-expression? ] attribute-specifier* )*

parameters-and-qualifiers

( parameter-declaration-clause ) attribute-specifier* cv-qualifier* ref-qualifier? exception-specification?

trailing-return-type

-> ATTRIB-SEQ{ trailing-type-specifier } abstract-declarator?

ptr-operator

One of:

cv-qualifier

const | volatile

ref-qualifier

& | &&

declarator-id

...? id-expression | ::? nested-name-specifier class-name

type-id

ATTRIB-SEQ{ type-specifier } abstract-declarator?

abstract-declarator

ptr-abstract-declarator | noptr-abstract-declarator? parameters-and-qualifiers trailing-return-type | ...

ptr-abstract-declarator

noptr-abstract-declarator | ptr-operator+ noptr-abstract-declarator?

noptr-abstract-declarator

noptr-abstract-declarator-head noptr-abstract-declarator-tail*

noptr-abstract-declarator-head

( ptr-abstract-declarator ) | noptr-abstract-declarator-tail

noptr-abstract-declarator-tail

parameters-and-qualifiers | [ constant-expression ] attribute-specifier*

parameter-declaration-clause

...? | COMMA-SEPARATED{ parameter-declaration } ( ,? ... )?

parameter-declaration

ATTRIB-SEQ-BOTH{ decl-specifier } ( declarator | abstract-declarator? ) ( = initializer-clause )?

function-definition

ATTRIB-SEQ-BOTH{ decl-specifier }? declarator ( function-body | = ( default | delete ) ; )

function-body

ctor-initializer? compound-statement | function-try-block

initializer

brace-or-equal-initializer | ( expression-list )

brace-or-equal-initializer

= initializer-clause | braced-init-list

initializer-clause

assignment-expression | braced-init-list

initializer-list

COMMA-SEPARATED-DDD{ initializer-clause }

braced-init-list

{ ( initializer-list ,? )? }

class-name

identifier | simple-template-id

class-specifier

class-head { member-specification? }

class-head

class-key attribute-specifier* ( class-head-name class-virt-specifier* )? base-clause?

class-head-name

nested-name-specifier? class-name

class-virt-specifier

final | explicit

class-key

class | struct | union

member-specification

( member-declaration | access-specifier : )+

member-declaration

One of:

member-declarator

One of:

virt-specifier

override | final | new

pure-specifier

= 0

base-clause

: base-specifier ...? ( , base-specifier ...? )*

base-specifier

attribute-specifier* ( virtual ^ access-specifier )* base-type-specifier

class-or-decltype

::? nested-name-specifier? class-name | decltype-specifier

base-type-specifier

class-or-decltype

access-specifier

private | protected | public

conversion-function-id

operator conversion-type-id

conversion-type-id

ATTRIB-SEQ{ type-specifier } conversion-declarator?

conversion-declarator

ptr-operator+

ctor-initializer

: COMMA-SEPARATED-DDD{ mem-initializer }

mem-initializer

mem-initializer-id ( ( expression-list? ) | braced-init-list )

mem-initializer-id

class-or-decltype | identifier

operator-function-id

operator operator

operator

new | delete | new [ ] | delete [ ] | + | - | * | / | % | ^ | & | | | ~ | ! | = | < | > | += | -= | *= | /= | %= | ^= | &= | |= | << | >> | >>= | <<= | == | != | <= | && | || | ++ | -- | , | ->* | -> | ( ) | [ ]

literal-operator-id

operator "" identifier

template-declaration

template < COMMA-SEPARATED{ template-parameter } > declaration

template-parameter

type-parameter | parameter-declaration

type-parameter

One of:

simple-template-id

template-name < COMMA-SEPARATED-DDD{ template-argument }? >

template-id

simple-template-id | ( operator-function-id | literal-operator-id ) < COMMA-SEPARATED-DDD{ template-argument }? >

template-name

identifier

template-argument

constant-expression | type-id | id-expression

typename-specifier

typename ::? nested-name-specifier ( identifier | template? simple-template-id )

explicit-instantiation

extern? template declaration

explicit-specialization

template < > declaration

try-block

try compound-statement handler+

function-try-block

try ctor-initializer? compound-statement handler+

handler

catch ( exception-declaration ) compound-statement

exception-declaration

ATTRIB-SEQ-BOTH{ type-specifier } ( declarator | abstract-declarator? ) | ...

throw-expression

throw assignment-expression?

exception-specification

dynamic-exception-specification | noexcept-specification

dynamic-exception-specification

throw ( COMMA-SEPARATED-DDD{ type-id }? )

noexcept-specification

noexcept ( ( constant-exception ) )?
Clone this wiki locally