{"size": 3455, "ext": "scrbl", "max_stars_repo_path": "scribble-doc/scribblings/scribble/bnf.scrbl", "max_stars_repo_name": "cwebber/scribble", "max_stars_repo_stars_event_min_datetime": "2015-01-11T18:19:42.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T08:45:52.000Z", "max_issues_repo_path": "scribble-doc/scribblings/scribble/bnf.scrbl", "max_issues_repo_name": "cwebber/scribble", "max_issues_repo_issues_event_min_datetime": "2015-03-24T20:53:21.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-20T22:51:57.000Z", "max_forks_repo_path": "scribble-doc/scribblings/scribble/bnf.scrbl", "max_forks_repo_name": "cwebber/scribble", "max_forks_repo_forks_event_min_datetime": "2015-03-09T12:23:06.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-07T15:00:13.000Z"}
{"max_stars_count": 161, "max_issues_count": 260, "max_forks_count": 103, "avg_line_length": 31.1261261261, "max_line_length": 73, "alphanum_fraction": 0.6332850941}
#lang scribble/doc
@(require scribble/manual "utils.rkt" scribble/bnf
          ;; "utils.rkt" provides scribble/bnf for-label
          )

@title[#:tag "bnf"]{BNF Grammars}

@defmodule[scribble/bnf]{The @racket[scribble/bnf] library
provides utilities for typesetting grammars.}

For example,

@verbatim[#:indent 2]|{
@(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

@(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}})])

See also @racket[racketgrammar].

@defproc[(BNF [prod (cons/c (or/c block? content?)
                            (non-empty-listof (or/c block? content?)))]
              ...)
         table?]{

Typesets a grammar table. Each production starts with an element
(typically constructed with @racket[nonterm]) for the non-terminal
being defined, and then a list of possibilities (typically constructed
with @racket[BNF-seq], etc.) to show on separate lines.}

@defproc[(nonterm [pre-content pre-content?] ...) element?]{

Typesets a non-terminal: italic in angle brackets.}

@defproc[(BNF-seq [elem content?] ...) (or/c element? "")]{

Typesets a sequence.}

@defproc[(BNF-seq-lines [elems (listof content?)] ...) block?]{

Typesets a sequence that is broken into multiple lines, where each
@racket[elems] is one line.}

@defproc[(BNF-group [pre-content pre-content?] ...) element?]{

Typesets a group surrounded by curly braces (so the entire group can
be repeated, for example).}

@defproc[(optional [pre-content pre-content?] ...) element?]{

Typesets an optional element: in square brackets.}

@defproc[(kleenestar [pre-content pre-content?] ...) element?]{

Typesets a 0-or-more repetition.}

@defproc[(kleeneplus [pre-content pre-content?] ...) element?]{

Typesets a 1-or-more repetition.}

@defproc[(kleenerange [n any/c] [m any/c] [pre-content pre-content?] ...)
         element?]{

Typesets a @racket[n]-to-@racket[m] repetition. The @racket[n] and
@racket[m] arguments are converted to a string using @racket[(format
"~a" n)] and @racket[(format "~a" m)].}

@defproc[(BNF-alt [elem element?] ...) 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 @racket[BNF].}

@; BNF-alt/close is exported but undocumented.
@; It looks like it produces a more densely packed version of
@; BNF-alt, but I haven't confirmed this.

@defthing[BNF-etc element?]{

An element to use for omitted productions or content.
Renders as: @BNF-etc
}


