On this page:
grammar
BNFEntry
nt
seq
alt
group
opt
star
plus
range
seq_  lines
etc
8.15.0.2

4.1 Backus-Naur Form (BNF) Grammars🔗ℹ

 import: scribble/bnf package: rhombus-scribble-lib

The scribble/bnf library provides functions for typesetting grammars in traditional BNF notation, as opposed to shrubbery grammars.

function

fun grammar([nt :: BNFEntry, [prod0 :: BNFEntry,

                              prod :: BNFEntry,

                              ...]],

            ...)

  :: FlowBlock

 

annotation

BNFEntry

Produces a table for a BNF grammar. Each argument to grammar defines a non-terminal nt with one or more productions prod0 plus prods.

A BNFEntry for either the left-hand or right-hand of a production is either a FlowBlock or Content.

For example,

@(import scribble/bnf)

 

@(

  block:

    def open: @litchar{(}

    def close: @litchar{)}

    bnf.grammar([@bnf.nt{expr}, [@bnf.nt{id},                                  

                                 @bnf.seq(open, @bnf.plus(@bnf.nt{expr}), close),

                                 bnf.seq(open, @litchar{lambda},

                                         open, @bnf.star(@bnf.nt{id}), close,

                                         @bnf.nt{expr}, close),

                                 @bnf.nt{val}]],

                [@bnf.nt{val}, [@bnf.alt(@bnf.nt{number}, @bnf.nt{primop})]],

                [@bnf.nt{id}, [@elem{any name except for @litchar{lambda}}]])

)

renders as

 

expr

 ::= 

id

 

  |  

( expr+ )

 

  |  

( lambda ( id* ) expr )

 

  |  

val

 

val

 ::= 

number  |  primop

 

id

 ::= 

any name except for lambda

function

fun nt(c :: PreContent)

  :: Element

A BNF nonterminal type in angle brackets.

function

fun seq(e :: Element, ...)

  :: Element || Any.of("")

 

function

fun alt(e :: Element, ...)

  :: Element

 

function

fun group(c :: PreContent)

  :: Element

 

function

fun opt(c :: PreContent)

  :: Element

 

function

fun star(c :: PreContent)

  :: Element

 

function

fun plus(c :: PreContent)

  :: Element

 

function

fun range(m, n, c :: PreContent)

  :: Element

Functions for constructing single-line produce right-hand sides:

function

fun seq_lines(line :: Content, ...)

  :: FlowBlock

Typesets a sequence broken across multiple lines.

value

def etc :: Element

An element to use for omitted productions or content, renders as ....