# The Grammar of Cyclone

Cyclone's grammar is derived from the next proposed OSI C standard, C9X. Changes from and additions to the C9X grammar are noted in red. The start symbol is translation-unit.

Quick links: declaration, struct-or-union-specifier, enum-specifier, declarator, statement, pattern, expression

translation-unit:

external-declaration translation-unitopt
`using` identifier `;` translation-unit
`namespace` identifier `;` translation-unit
`using` identifier `{` translation-unit `}`
`namespace` identifier `{` translation-unit `}`

external-declaration:

function-definition
declaration

function-definition:

declaration-specifiersopt declarator declaration-listopt compound-statement

declaration:

declaration-specifiers init-declarator-listopt `;`
`let` pattern `=` expression `;`

declaration-list:

declaration
declaration-list declaration

declaration-specifiers:

storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt
function-specifier declaration-specifiersopt

storage-class-specifier: one of

`auto` `register` `static` `extern` `typedef` `abstract`

(The C9X type-specifiers `_Bool`, `_Complex`, and `_Imaginary` are not yet supported)
type-specifier: one of

`void` `char` `short` `int` `long` `float` `double` `signed`
`unsigned` struct-or-union-specifier enum-specifier
typedef-name type-paramsopt
type-var `bool` `Bool` `Char` `Short` `Int` `Long` `Float` `Double`
`$` `(` parameter-list `)`

type-qualifier: one of

`const` `restrict` `volatile`

function-specifier:

`inline`

struct-or-union-specifier:

struct-or-union `{` struct-declaration-list `}`
struct-or-union identifier type-paramsopt `{` struct-declaration-list `}`
struct-or-union identifier type-paramsopt

type-params:

`<` type-name-list `>`

struct-or-union: one of

`struct` `union`

struct-declaration-list:

struct-declaration
struct-declaration-list struct-declaration

init-declarator-list:

init-declarator
init-declarator-list `,` init-declarator

init-declarator:

declarator
declarator `=` initializer

struct-declaration:

specifier-qualifier-list struct-declarator-list `;`

specifier-qualifier-list:

type-specifier specifier-qualifier-listopt
type-qualifier specifier-qualifier-listopt

struct-declarator-list:

struct-declarator
struct-declarator-list `,` struct-declarator

struct-declarator:

declarator
declaratoropt `:` constant-expression

enum-specifier:

`enum` `{` enumerator-list `}`
`enum` identifier type-paramsopt `{` enumerator-list `}`
`enum` identifier type-paramsopt
`xenum` identifier `{` enumerator-list `}`
`xenum` identifier

enumerator-list:

enumerator
enumerator `,` enumerator-list
enumerator `;`
enumerator `;` enumerator-list

enumerator:

identifier
identifier `=` constant-expression
identifier `(` parameter-list `)`

declarator:

pointeropt direct-declarator

(We do not support C9X `[*]` direct-declarators.)
direct-declarator:

identifier
`(` declarator `)`
direct-declarator `[` constant-expressionopt `]`
direct-declarator `(` parameter-type-list `)`
direct-declarator `(` identifier-listopt `)`
direct-declarator `[` `?` `]`
direct-declarator `<` type-name-list `>`

pointer:

`*` type-qualifier-listopt pointeropt
`@` type-qualifier-listopt pointeropt

type-qualifier-list:

type-qualifier
type-qualifier-list type-qualifier

parameter-type-list:

parameter-list
parameter-list `,` `...`

parameter-list:

parameter-declaration
parameter-list `,` parameter-declaration

parameter-declaration:

specifier-qualifier-list declarator
specifier-qualifier-list abstract-declaratoropt

identifier-list:

identifier
identifier-list `,` identifier

initializer:

assignment-expression
`{` initializer-listopt `}`
`{` initializer-list `,` `}`

initializer-list:

designationopt initializer
initializer-list `,` designationopt initializer

designation:

designator-list `=`

designator-list:

designator
designator-list designator

designator:

`[` constant-expression `]`
`.` identifier

type-name:

specifier-qualifier-list abstract-declaratoropt

type-name-list:

type-name
type-name-list `,` type-name

abstract-declarator:

pointer
pointeropt direct-abstract-declarator

(We do not support C9X `[*]` direct-abstract-declarators.)
direct-abstract-declarator:

`(` abstract-declarator `)`
direct-abstract-declaratoropt `[` constant-expressionopt `]`
direct-abstract-declaratoropt `(` parameter-type-listopt `)`
direct-abstract-declaratoropt `[` `?` `]`
direct-abstract-declarator `<` type-name-list `>`

statement:

labeled-statement
expression-statement
compound-statement
selection-statement
iteration-statement
jump-statement
`cut` statement
`splice` statement

(We treat the `case` and `default` labels specially; see `switch-clauses`)
labeled-statement:

identifier `:` statement

expression-statement:

expressionopt `;`

compound-statement:

`{` block-item-listopt `}`

block-item-list:

block-item
block-item block-item-list

block-item:

declaration
statement

selection-statement:

`if` `(` expression `)` statement
`if` `(` expression `)` statement `else` statement
`switch` `(` expression `)` `{` switch-clauses `}`
`try` statement `catch` `{` switch-clauses `}`

switch-clauses:

(empty)
`default` `:` block-item-list
`case` pattern `:` block-item-listopt switch-clauses
`case` pattern `where` expression `:` block-item-listopt switch-clauses

iteration-statement:

`while` `(` expression `)` statement
`do` statement `while` `(` expression `)` `;`
`for` `(` expressionopt `;` expressionopt `;` expressionopt `)` statement
`for` `(` declaration expressionopt `;` expressionopt `)` statement

jump-statement:

`goto` identifier `;`
`continue` `;`
`break` `;`
`return` `;`
`return` expression `;`
`fallthru` `;`

pattern:

`_`
`(` pattern `)`
integer-constant
`-` integer-constant
floating-constant
character-constant
`null`
`true`
`false`
identifier
identifier type-paramsopt `(` tuple-pattern-list `)`
`$` `(` tuple-pattern-list `)`
identifier type-paramsopt `{` `}`
identifier type-paramsopt `{` field-pattern-list `}`
`&` pattern

tuple-pattern-list:

(empty)
pattern
tuple-pattern-list `,` pattern

field-pattern-list:

field-pattern
field-pattern-list `,` field-pattern

field-pattern:

pattern
designation pattern

expression:

assignment-expression
expression `,` assignment-expression

assignment-expression:

conditional-expression
unary-expression assignment-operator assignment-expression

assignment-operator: one of

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

conditional-expression:

logical-or-expression
logical-or-expression `?` expression `:` conditional-expression

constant-expression:

conditional-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:

cast-expression
multiplicative-expression `*` cast-expression
multiplicative-expression `/` cast-expression
multiplicative-expression `%` cast-expression

cast-expression:

unary-expression
`(` type-name `)` cast-expression

unary-expression:

postfix-expression
`++` unary-expression
`--` unary-expression
unary-operator cast-expression
`sizeof` unary-expression
`sizeof` `(` type-name `)`
`raise` unary-expression

unary-operator: one of

`&` `*` `+` `-` `~` `!`

postfix-expression:

primary-expression
postfix-expression `[` expression `]`
postfix-expression `(` `)`
postfix-expression `(` argument-expression-list `)`
postfix-expression `.` identifier
postfix-expression `->` identifier
postfix-expression `++`
postfix-expression `--`
`(`type-name `)``{`initializer-list `}`
`(`type-name `)``{`initializer-list `,``}`
`new` `{` initializer-listopt `}`
`new` `{` initializer-list `,` `}`
`new` `{` `for` identifier `<` expression `:` expression `}`
`fill` `(` expression `)`
`codegen` `(` function-definition `)`
``````````````````

primary-expression:

identifier
constant
string
`(` expression `)`
identifier `<>`
`$` `(` argument-expression-list `)`
identifier `{` initializer-list `}`
`(` `{` block-item-list `}` `)`

argument-expression-list:

assignment-expression
argument-expression-list `,` assignment-expression

constant:

integer-constant
character-constant
floating-constant
enumeration-constant
`true`
`false`
`null`