C# Grammar

Top level symbols:

1 Lexical grammar
input ::=
input-section-part ::=
|
input-element ::=
whitespace | comment | token
1.1 Line terminators
new-line ::=
Carriage_return_character_(U+000D)
|
Line_feed_character_(U+000A)
|
Carriage_return_character_(U+000D) Line_feed_character_(U+000A)
|
Line_separator_character_(U+2028)
|
Paragraph_separator_character_(U+2029)
1.2 White space
whitespace ::=
Any_character_with_Unicode class_Zs
|
Horizontal_tab_character_(U+0009)
|
Vertical_tab_character_(U+000B)
|
Form_feed_character_(U+000C)
1.3 Comments
comment ::=
|
single-line-comment ::=
input-character ::=
Any_Unicode_character_except_a new-line-character
new-line-character ::=
Carriage_return_character_(U+000D)
|
Line_feed_character_(U+000A)
|
Line_separator_character_(U+2028)
|
Paragraph_separator_character_(U+2029)
delimited-comment ::=
delimited-comment-character ::=
|
not-asterisk ::=
Any_Unicode_character_except_star_(*)
not-slash ::=
Any_Unicode_character_except_slash_(/)
1.4 Tokens
token ::=
|
|
|
|
|
|
1.5 Unicode characterescape sequences
unicode-character-escape-sequence ::=
|
1.6 Identifiers
identifier ::=
|
available-identifier ::=
An identifier-or-keyword that_is_not_a keyword
identifier-or-keyword ::=
identifier-start-character ::=
|
The_underscore_character_(_)
identifier-part-character ::=
|
|
|
|
letter-character ::=
A_Unicode_character_of_classes_Lu,_Ll,_Lt,_Lm,_Lo,_or_Nl
|
A unicode-character-escape-sequence representing_a_character_of_classes_Lu,_Ll,_Lt,_Lm,_Lo,_or_Nl
combining-character ::=
A_Unicode_character_of_classes_Mn_or_Mc
|
A unicode-character-escape-sequence representing_a_character_of_classes_Mn_or_Mc
decimal-digit-character ::=
A_Unicode_character_of_the_class_Nd
|
A unicode-character-escape-sequence representing_a_character_of_the_class_Nd
connecting-character ::=
A_Unicode_character_of_theclass_Pc
|
A unicode-character-escape-sequence representing_a_character_of_the_class_Pc
formatting-character ::=
A_Unicode_character_of_the_class_Cf
|
A unicode-character-escape-sequence representing_a_character_of_the_class_Cf
1.7 Keywords
keyword ::=
abstract | as | base | bool | break | byte | case | catch | char | checked | class | const | continue | decimal | default | delegate | do | double | else | enum | event | explicit | extern | false | finally | fixed | float | for | foreach | goto | if | implicit | in | int | interface | internal | is | lock | long | namespace | new | null | object | operator | out | override | params | private | protected | public | readonly | ref | return | sbyte | sealed | short | sizeof | stackalloc | static | string | struct | switch | this | throw | true | try | typeof | uint | ulong | unchecked | unsafe | ushort | using | virtual | void | while
1.8 Literals
literal ::=
|
|
|
|
|
boolean-literal ::=
true | false
integer-literal ::=
|
decimal-integer-literal ::=
decimal-digit ::=
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
integer-type-suffix ::=
U | u | L | l | UL | Ul | uL | ul | LU | Lu | lU | lu
hexadecimal-integer-literal ::=
|
hex-digit ::=
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F | a | b | c | d | e | f
real-literal ::=
|
|
exponent-part ::=
e ( sign )? ( decimal-digit )+
|
E ( sign )? ( decimal-digit )+
sign ::=
+ | -
real-type-suffix ::=
F | f | D | d | M | m
character-literal ::=
character ::=
|
|
|
single-character ::=
Any_character_except_'(U+0027),_\(U+005C),_and new-line-character
simple-escape-sequence ::=
\' | \" | \\ | \0 | \a | \b | \f | \n | \r | \t | \v
hexadecimal-escape-sequence ::=
string-literal ::=
|
regular-string-literal ::=
regular-string-literal-character ::=
|
|
|
single-regular-string-literal-character ::=
Any_character_except_"(U+0022),_\(U+005C),_and new-line-character
verbatim-string-literal ::=
verbatim-string-literal-character ::=
|
single-verbatim-string-literal-character ::=
Any_character_except "
quote-escape-sequence ::=
""
null-literal ::=
null
1.9 Operators andpunctuators
operator-or-punctuator ::=
{ | } | [ | ] | ( | ) | . | , | : | ; | + | - | * | / | % | & | | | ^ | ! | ~ | = | < | > | ? | ++ | -- | && | || | << | >> | == | != | <= | >= | += | -= | *= | /= | %= | &= | |= | ^= | <<= | >>= | ->
1.10 Pre-processing directives
pp-directive ::=
pp-declaration | pp-conditional | pp-line | pp-diagnostic | pp-region
pp-new-line ::=
conditional-symbol ::=
Any identifier-or-keyword except_true_or_false
pp-expression ::=
pp-or-expression ::=
|
pp-and-expression ::=
|
pp-equality-expression ::=
|
|
pp-unary-expression ::=
|
pp-primary-expression ::=
true
|
false
|
|
pp-declaration ::=
|
pp-conditional ::=
pp-if-section ::=
pp-elif-section ::=
pp-else-section ::=
pp-endif ::=
( whitespace )? # ( whitespace )? endif pp-new-line
conditional-section ::=
|
skipped-section-part ::=
|
skipped-characters ::=
not-number-sign ::=
Any input-character except_#
pp-line ::=
line-indicator ::=
|
|
default
file-name ::=
file-name-character ::=
Any_character_except_&quot;(U+0022),_and new-line
pp-diagnostic ::=
( whitespace )? # ( whitespace )? error ( whitespace )? pp-message
|
( whitespace )? # ( whitespace )? warning ( whitespace )? pp-message
pp-message ::=
pp-region ::=
pp-start-region ::=
( whitespace )? # ( whitespace )? region ( whitespace )? pp-message
pp-end-region ::=
( whitespace )? # ( whitespace )? endregion ( whitespace )? pp-message
2 Syntactic grammar
2.1 Basic concepts
namespace-name ::=
type-name ::=
namespace-or-type-name ::=
|
2.2 Types
type ::=
value-type | reference-type | pointer-type a
value-type ::=
struct-type | enum-type
struct-type ::=
type-name | simple-type
simple-type ::=
numeric-type | bool
numeric-type ::=
|
|
decimal
integral-type ::=
sbyte | byte | short | ushort | int | uint | long | ulong | char
floating-point-type ::=
float | double
enum-type ::=
type-name
reference-type ::=
class-type | interface-type | array-type | delegate-type
class-type ::=
type-name | object | string
interface-type ::=
type-name
delegate-type ::=
type-name
2.3 Variables
variable-reference ::=
expression
2.4 Expressions
argument-list ::=
|
argument ::=
|
|
primary-expression ::=
|
primary-no-array-creation-expression ::=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
simple-name ::=
identifier
parenthesized-expression ::=
member-access ::=
|
predefined-type ::=
bool | byte | char | decimal | double | float | int | long | object | sbyte | short | string | uint | ulong | ushort
invocation-expression ::=
element-access ::=
expression-list ::=
|
this-access ::=
this
base-access ::=
base . identifier
|
base [ expression-list ]
post-increment-expression ::=
post-decrement-expression ::=
object-creation-expression ::=
new type ( ( argument-list )? )
array-creation-expression ::=
|
delegate-creation-expression ::=
typeof-expression ::=
typeof ( type )
|
typeof ( void )
checked-expression ::=
checked ( expression )
unchecked-expression ::=
unchecked ( expression )
unary-expression ::=
|
|
|
|
|
|
|
|
|
|
pre-increment-expression ::=
pre-decrement-expression ::=
cast-expression ::=
multiplicative-expression ::=
|
|
|
additive-expression ::=
|
|
shift-expression ::=
|
|
relational-expression ::=
|
|
|
|
|
|
equality-expression ::=
|
|
and-expression ::=
|
exclusive-or-expression ::=
|
inclusive-or-expression ::=
|
conditional-and-expression ::=
|
conditional-or-expression ::=
|
conditional-expression ::=
|
assignment ::=
assignment-operator ::=
= | += | -= | *= | /= | %= | &= | |= | ^= | <<= | >>=
expression ::=
|
constant-expression ::=
expression
boolean-expression ::=
expression
2.5 Statements
statement ::=
|
|
embedded-statement ::=
|
|
|
|
|
|
|
|
|
|
|
|
block ::=
{ ( statement )* }
empty-statement ::=
;
labeled-statement ::=
declaration-statement ::=
|
local-variable-declaration ::=
local-variable-declarators ::=
|
local-variable-declarator ::=
|
local-variable-initializer ::=
|
local-constant-declaration ::=
constant-declarators ::=
|
constant-declarator ::=
expression-statement ::=
statement-expression ::=
|
|
|
|
|
|
selection-statement ::=
|
if-statement ::=
|
switch-statement ::=
switch-block ::=
{ ( switch-section )* }
switch-section ::=
switch-label ::=
|
default :
iteration-statement ::=
|
|
|
while-statement ::=
do-statement ::=
for-statement ::=
for-initializer ::=
|
for-condition ::=
for-iterator ::=
statement-expression-list ::=
|
foreach-statement ::=
jump-statement ::=
|
|
|
|
break-statement ::=
break ;
continue-statement ::=
continue ;
goto-statement ::=
goto identifier ;
|
goto case constant-expression ;
|
goto default ;
return-statement ::=
return ( expression )? ;
throw-statement ::=
throw ( expression )? ;
try-statement ::=
|
|
catch-clauses ::=
|
specific-catch-clause ::=
catch ( class-type ( identifier )? ) block
general-catch-clause ::=
catch block
finally-clause ::=
finally block
checked-statement ::=
checked block
unchecked-statement ::=
unchecked block
lock-statement ::=
using-statement ::=
resource-acquisition ::=
|
compilation-unit ::=
namespace-declaration ::=
qualified-identifier ::=
|
namespace-body ::=
using-directive ::=
|
using-alias-directive ::=
using-namespace-directive ::=
namespace-member-declaration ::=
|
type-declaration ::=
|
|
|
|
2.6 Classes
class-declaration ::=
class-modifier ::=
new | public | protected | internal | private | abstract | sealed
class-base ::=
|
|
interface-type-list ::=
|
class-body ::=
class-member-declaration ::=
|
|
|
|
|
|
|
|
|
|
constant-declaration ::=
constant-modifier ::=
new | public | protected | internal | private
field-declaration ::=
field-modifier ::=
new | public | protected | internal | private | static | readonly | volatile
variable-declarators ::=
|
variable-declarator ::=
|
variable-initializer ::=
|
|
method-declaration ::=
method-header ::=
method-modifier ::=
new | public | protected | internal | private | static | virtual | sealed | override | abstract | extern
return-type ::=
type | void
member-name ::=
|
method-body ::=
block | ;
formal-parameter-list ::=
|
|
fixed-parameters ::=
|
fixed-parameter ::=
parameter-modifier ::=
ref | out
parameter-array ::=
property-declaration ::=
property-modifier ::=
new | public | protected | internal | private | static | virtual | sealed | override | abstract | extern
accessor-declarations ::=
|
get-accessor-declaration ::=
set-accessor-declaration ::=
accessor-body ::=
block | ;
event-declaration ::=
|
event-modifier ::=
new | public | protected | internal | private | static | virtual | sealed | override | abstract | extern
event-accessor-declarations ::=
|
add-accessor-declaration ::=
remove-accessor-declaration ::=
indexer-declaration ::=
indexer-modifier ::=
new | public | protected | internal | private | virtual | sealed | override | abstract | extern
indexer-declarator ::=
|
operator-declaration ::=
operator-modifier ::=
public | static | extern
operator-declarator ::=
|
|
unary-operator-declarator ::=
overloadable-unary-operator ::=
+ | - | ! | ~ | ++ | -- | true | false
binary-operator-declarator ::=
overloadable-binary-operator ::=
+ | - | * | / | % | & | | | ^ | << | >> | == | != | > | < | >= | <=
conversion-operator-declarator ::=
implicit operator type ( type identifier )
|
explicit operator type ( type identifier )
operator-body ::=
block | ;
constructor-declaration ::=
constructor-modifier ::=
public | protected | internal | private | extern
constructor-declarator ::=
constructor-initializer ::=
: base ( ( argument-list )? )
|
: this ( ( argument-list )? )
constructor-body ::=
block | ;
static-constructor-declaration ::=
static-constructor-modifiers ::=
( extern )? static
|
static ( extern )?
static-constructor-body ::=
block | ;
destructor-declaration ::=
destructor-body ::=
block | ;
2.7 Structs
struct-declaration ::=
struct-modifier ::=
new | public | protected | internal | private
struct-interfaces ::=
struct-body ::=
struct-member-declaration ::=
|
|
|
|
|
|
|
|
|
2.8 Arrays
array-type ::=
non-array-type ::=
type
rank-specifier ::=
[ ( dim-separators )? ]
dim-separators ::=
,
|
array-initializer ::=
|
variable-initializer-list ::=
|
2.9 Interfaces
interface-declaration ::=
interface-modifier ::=
new | public | protected | internal | private
interface-base ::=
interface-body ::=
interface-member-declaration ::=
|
|
|
interface-method-declaration ::=
interface-property-declaration ::=
interface-accessors ::=
( attribute-section )* get ;
|
( attribute-section )* set ;
|
( attribute-section )* get ; ( attribute-section )* set ;
|
( attribute-section )* set ; ( attribute-section )* get ;
interface-event-declaration ::=
( attribute-section )* ( new )? event type identifier ;
interface-indexer-declaration ::=
2.10 Enums
enum-declaration ::=
enum-base ::=
enum-body ::=
|
enum-modifier ::=
new | public | protected | internal | private
enum-member-declarations ::=
|
enum-member-declaration ::=
|
2.11 Delegates
delegate-declaration ::=
delegate-modifier ::=
new | public | protected | internal | private
2.12 Attributes
global-attribute-section ::=
|
global-attribute-target-specifier ::=
global-attribute-target ::=
assembly
attribute-section ::=
|
attribute-target-specifier ::=
attribute-target ::=
field | event | method | module | param | property | return | type
attribute-list ::=
|
attribute ::=
attribute-name ::=
type-name
attribute-arguments ::=
|
|
positional-argument-list ::=
|
positional-argument ::=
named-argument-list ::=
|
named-argument ::=
attribute-argument-expression ::=
expression
3 Grammar extensions for unsafe code
unsafe-statement ::=
unsafe block
pointer-type ::=
|
void *
unmanaged-type ::=
type
pointer-indirection-expression ::=
pointer-member-access ::=
pointer-element-access ::=
addressof-expression ::=
sizeof-expression ::=
sizeof ( unmanaged-type )
fixed-statement ::=
fixed-pointer-declarators ::=
|
fixed-pointer-declarator ::=
fixed-pointer-initializer ::=
|
stackalloc-initializer ::=
stackalloc unmanaged-type [ expression ]


Notes:
a) Available in an unsafe context.
b) Starts unsafe statement.
c) Declares unsafe pointer.

Created from ECMA-334 Standard. Text form of the grammar was brushed a little (this was partially manual work and a probable source of errors if there are any :o( ), parsed to generate the grammar description in an XML format, transformed to EBNF using XSLT, then transformed to DHTML using another XSLT. Enjoy.

Copyright 2002, 2003 Peter Hercek, All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You can receive a copy of the GNU General Public License here.

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~

~