On this page:
BNF
nonterm
BNF-seq
BNF-seq-lines
BNF-group
optional
kleenestar
kleeneplus
kleenerange
BNF-alt
BNF-etc

4.6 BNF Grammars🔗ℹ

The scribble/bnf library provides utilities for typesetting grammars.

For example,

  @(let ([open @litchar{(}]

         [close @litchar{)}])

     @BNF[(list @nonterm{expr}

                @nonterm{id}

                @BNF-seq[open @kleeneplus[@nonterm{expr}] close]

                @BNF-seq[open @litchar{lambda}

                          open @kleenestar[@nonterm{id}] close

                          @nonterm{expr} close]

                @nonterm{val})

          (list @nonterm{val}

                @BNF-alt[@nonterm{number} @nonterm{primop}])

          (list @nonterm{id}

                @elem{any name except for @litchar{lambda}})])

produces the output

 

expr

 ::= 

id

 

  |  

( expr+ )

 

  |  

( lambda ( id* ) expr )

 

  |  

val

 

val

 ::= 

number  |  primop

 

id

 ::= 

any name except for lambda

See also racketgrammar.

procedure

(BNF prod ...)  table?

  prod : 
(cons/c (or/c block? content?)
        (non-empty-listof (or/c block? content?)))
Typesets a grammar table. Each production starts with an element (typically constructed with nonterm) for the non-terminal being defined, and then a list of possibilities (typically constructed with BNF-seq, etc.) to show on separate lines.

procedure

(nonterm pre-content ...)  element?

  pre-content : pre-content?
Typesets a non-terminal: italic in angle brackets.

procedure

(BNF-seq elem ...)  (or/c element? "")

  elem : content?
Typesets a sequence.

procedure

(BNF-seq-lines elems ...)  block?

  elems : (listof content?)
Typesets a sequence that is broken into multiple lines, where each elems is one line.

procedure

(BNF-group pre-content ...)  element?

  pre-content : pre-content?
Typesets a group surrounded by curly braces (so the entire group can be repeated, for example).

procedure

(optional pre-content ...)  element?

  pre-content : pre-content?
Typesets an optional element: in square brackets.

procedure

(kleenestar pre-content ...)  element?

  pre-content : pre-content?
Typesets a 0-or-more repetition.

procedure

(kleeneplus pre-content ...)  element?

  pre-content : pre-content?
Typesets a 1-or-more repetition.

procedure

(kleenerange n m pre-content ...)  element?

  n : any/c
  m : any/c
  pre-content : pre-content?
Typesets a n-to-m repetition. The n and m arguments are converted to a string using (format "~a" n) and (format "~a" m).

procedure

(BNF-alt elem ...)  element?

  elem : element?
Typesets alternatives for a production’s right-hand side to appear on a single line. The result is normally used as a single possibility in a production list for BNF.

value

BNF-etc : element?

An element to use for omitted productions or content. Renders as: ...