© APSIS GmbH , Polling, 2002, 2003

Last modification: June 25, 2003


A Context Free Grammar for C#

from the book "Effizient Programmieren mit C# und .NET" (in German)


Name spaces

compilation-unit: {using-directive} {namespace-member-declaration}

using-directive: using identifier = namespace-or-type-name ; | using namespace-name ;

namespace-member-declaration: namespace-declaration | type-declaration

namespace-declaration: namespace {identifier .} identifier { {using-directive} {namespace-member-declaration} } [;]

type-declaration: class-declaration | struct-declaration | interface-declaration | enum-declaration | delegate-declaration


Classes

class-declaration: [attributes] {class-modifier} class identifier [class-base] { {class-member-declaration} } [;]

class-modifier: new | public | protected | internal | private | abstract | sealed

class-base: : class-type | : interface-type-list | : class-type , interface-type-list

interface-type-list: interface-type { , interface-type}

class-member-declaration: constant-declaration | field-declaration | method-declaration | property-declaration | event-declaration | indexer-declaration | operator-declaration | constructor-declaration | destructor-declaration | static-constructor-declaration | type-declaration

constant-declaration: [attributes] {constant-modifier} const type constant-declarator {, constant-declarator} ;

constant-modifier: new | public | protected | internal | private

field-declaration: [attributes] {field-modifier} type variable-declarator {, variable-declarator} ;

field-modifier: new | public | protected | internal | private | static | readonly


Methods

method-declaration: [attributes] {method-modifier} return-type member-name ( [formal-parameter-list] ) method-body

method-modifier: new | public | protected | internal | private | static | virtual | override | abstract | extern

return-type: type | void

member-name: [interface-type .] identifier

method-body: block | ;

formal-parameter-list: fixed-parameters | parameter-array | fixed-parameters , parameter-array

fixed-parameters: fixed-parameter {, fixed-parameter}

fixed-parameter: [attributes] [parameter-modifier] type identifier

parameter-modifier: ref | out

parameter-array: [attributes] params array-type identifier

property-declaration: [attributes] {property-modifier} type member-name { accessor-declarations }

accessor-declarations: [get-accessor-declaration]+[set-accessor-declaration]

property-modifier: new | public | protected | internal | private | static | virtual | override | abstract

get-accessor-declaration: [attributes] get accessor-body

set-accessor-declaration: [attributes] set accessor-body

accessor-body: block | ;

event-declaration: [attributes] {event-modifier} event type field-or-property

event-modifier: new | public | protected | internal | private | static

field-or-property: variable-declarator {, variable-declarator} ; | member-name { accessor-declarations }

indexer-declaration: [attributes] {indexer-modifier} type [interface-type .] this [ formal-parameter-list ] { accessor-declarations }

indexer-modifier: new | public | protected | internal | private | virtual | override | abstract

operator-declaration: [attributes] operator-modifiers operator-declarator block

operator-modifiers: public static | static public

operator-declarator: unary-operator-declarator | binary-operator-declarator | conversion-operator-declarator

unary-operator-declarator: type operator overloadable-unary-operator ( type identifier )

overloadable-unary-operator: + | - | ! | ~ | ++ | -- | true | false

binary-operator-declarator: type operator overloadable-binary-operator ( type identifier , type identifier )

overloadable-binary-operator: + | - | * | / | % | & | | | ^ | << | >> | == | != | > | < | >= | <=

conversion-operator-declarator: [implicit | explicit]+ operator type ( type identifier )

constructor-declaration: [attributes] {constructor-modifier} constructor-declarator block

constructor-modifier: public | protected | internal | private

constructor-declarator: identifier ( [formal-parameter-list] ) [constructor-initializer]

constructor-initializer: : [base | this] ( [argument-list] )

destructor-declaration: [attributes] ~ identifier ( ) block

static-constructor-declaration: [attributes] static identifier ( ) block

struct-declaration: [attributes] {struct-modifier} struct identifier [: interface-type-list] { {class-member-declaration} } [;]

struct-modifier: new | public | protected | internal | private


Interfaces

interface-declaration: [attributes] {interface-modifier}interface identifier [: interface-type-list] [{ {interface-member-declaration} } ; ]

interface-modifier: new | public | protected | internal | private

interface-member-declaration: interface-method-declaration | interface-property-declaration | interface-event-declaration | interface-indexer-declaration

interface-method-declaration: [attributes] [new] return-type identifier ( [formal-parameter-list] ) ;

interface-property-declaration: [attributes] [new] type identifier { interface-accessors }

interface-accessors: [ [attributes] get ; ]+[ [attributes] set ; ]

interface-event-declaration: [attributes] [new] event type identifier ;

interface-indexer-declaration: [attributes] [new] type this [ formal-parameter-list ] { interface-accessors }


Enum types

enum-declaration: [attributes] {enum-modifier}enum identifier [: non-char-integral-type] enum-body [;]

enum-modifier: new | public | protected | internal | private

non-char-integral-type: sbyte | byte | short | ushort | int | uint | long | ulong

enum-body: { [enum-member-declaration {, enum-member-declaration}] }

enum-member-declaration: [attributes] identifier [= constant-expression]

Delegates

delegate-declaration: [attributes] [delegate-modifier {, delegate-modifier}] delegate type identifier ( [formal-parameter-list] ) ;

delegate-modifier: new | public | protected | internal | private

Attributes

attributes: [attribute {, attribute}]

attribute: type-name ( [positional-argument-list]+[named-argument-list]+[positional-argument-list , named-argument-list] )

positional-argument-list: positional-argument {, positional-argument}

positional-argument: expression

named-argument-list: named-argument {, named-argument}

named-argument: identifier = expression


Types

type: value-type | reference-type

value-type: type-name | integral-type | float | double | decimal | bool

integral-type: sbyte | byte | short | ushort | int | uint | long | ulong | char

reference-type: class-type | interface-type | array-type | delegate-type

class-type: type-name | object | string

interface-type: type-name

array-type: non-array-type {[ {,} ]}+

non-array-type: type

delegate-type: type-name

variable-reference: expression

namespace-or-type-name: identifier {. identifier}

namespace-name: namespace-or-type-name

type-name: namespace-or-type-name

identifier : letter { character } | @ keyword


Expressions

argument-list: argument {, argument}

argument: expression | [ref | out] variable-reference

primary-expression: literal | identifier | ( expression ) | primary-expression . identifier | predefined-type . identifier | invocation-expression | primary-expression [ expression {, expression} ] | this | base . identifier | base [ expression {, expression} ] | primary-expression [++ | --]+ | new-expression | typeof ( type ) | sizeof ( type ) | checked ( expression ) | unchecked ( expression )

predefined-type: bool | byte | char | decimal | double | float | int | long | object | sbyte | short | string | uint | ulong | ushort

invocation-expression: primary-expression ( [argument-list] )

new-expression: object-creation-expression | array-creation-expression | new delegate-type ( expression )

object-creation-expression: new type ( [argument-list] )

array-creation-expression: new non-array-type [ expression {, expression} ] {[ {,} ]} [array-initializer] | new array-type array-initializer

array-initializer: { {variable-initializer ,} [variable-initializer]] }

unary-expression: { + | -! | ~ | * | & | ++ | -- | ( type ) } primary-expression  

multiplicative-expression: unary-expressionmultiplicative-operator unary-expression }

multiplicative-operator: * | / | %

additive-expression: multiplicative-expression {additive-operator multiplicative-expression}

additive-operator: + | -

shift-expression: additive-expression {shift-operator additive-expression}

shift-operator: << | >>

relational-expression: shift-expression {relational-operator shift-expression} [ is reference-type]

relational-operator: < | > | <= | >=

equality-expression: relational-expression {equality-operator relational-expression}

equality-operator: == | !=

and-expression: equality-expression { & equality-expression}

exclusive-or-expression: and-expression { ^ and-expression}

inclusive-or-expression: exclusive-or-expression { | exclusive-or-expression}

conditional-and-expression: inclusive-or-expression { && inclusive-or-expression}

conditional-or-expression: conditional-and-expression { || conditional-and-expression}

assignment: unary-expression assignment-operator expression

assignment-operator: = | += | -= | *= | /= | %= | &= | |= | ^= | <<= | >>=

expression: conditional-or-expression [? expression : expression] | assignment

constant-expression: expression

boolean-expression: expression


Statements

statement: labeled-statement | declaration-statement | embedded-statement

embedded-statement: block | [statement-expression] ; | selection-statement | iteration-statement | jump-statement | try-statement | check-statement | lock-statement | using-statement

block: { {statement} }

labeled-statement: identifier : statement

declaration-statement: local-variable-declaration ; | local-constant-declaration ;

local-variable-declaration: type variable-declarator {, variable-declarator}

variable-declarator: identifier | identifier = variable-initializer

variable-initializer: expression | array-initializer

local-constant-declaration: const type constant-declarator {, constant-declarator}

constant-declarator: identifier = constant-expression

statement-expression: invocation-expression | object-creation-expression | assignment | primary-expression [

++ | --]+ | [++ | --]+ primary-expression

selection-statement: if ( boolean-expression ) embedded-statement [else embedded-statement] | switch ( expression ) { { switch-section } }

switch-section: { switch-label } { statement }

switch-label: case constant-expression : | default :

iteration-statement: while ( boolean-expression ) embedded-statement | do embedded-statement while ( boolean-expression ) ; | for ( [for-initializer] ; [boolean-expression] ; [for-iterator] ) embedded-statement | foreach ( type identifier in expression ) embedded-statement

for-initializer: local-variable-declaration | statement-expression {, statement-expression }

for-iterator: statement-expression {, statement-expression }

jump-statement: break ; | continue ; | return [ expression] ; | throw [ expression] ; | goto identifier ; | goto case constant-expression ; | goto default ;

try-statement: try block {catch-clauses}+[finally block]

catch-clauses: catch [( class-type [identifier] )] block

check-statement: [checked | unchecked ]+ block

lock-statement: lock ( expression ) embedded-statement

using-statement: using ( local-variable-declaration { catch ( [class-type identifier] ) block } expression ) embedded-statement


Home

Authors e-mail: andreas@solymosi.com

© APSIS GmbH , Polling, 2002, 2003