Search code examples
syntaxlanguage-agnosticgrammarbnfebnf

Bracket and brace usage in BNF?


Consider the following:

A function parameter list is a sequence of zero or more parameters separated by commas and bracketed by parentheses, "(" and ")" .

If I want to give the syntax of "function parameter list" assuming that the syntactic category of "parameter" has been defined, can I write:

  <function parameter list> ::= ( [<parameter> { , <parameter>} ] )

as a BNF? Is the usage of brackets nested in braces acceptable for EBNF?

Initially my impulse was to give the BNF as:

  <function parameter list> ::= ( <parameter> )
                             | ( <parameter> { , <parameter> } )
                             | ( )

I'm unsure how else I would write this BNF without braces.

I'm trying to get info from my text or online about using brackets/braces in regular BNF and some sources imply that you can, but my text doesn't specify exactly. It seems like the BNF needs some type of brace for this case. I thought BNF couldn't use bracket or braces and now I'm unsure.


Solution

  • Braces and brackets are EBNF constructs -- the E stands for extended. Simple BNF is just a context-free grammar with no extra syntactic sugar for recursive things, so you have to write it out with a nested recursive rule:

    <function parameter list> ::= ( <param-list> ) | ( )
    <param-list> ::= <param> | <param-list> , <param>