On this page:
define-language
define-extended-language
language-nts
compiled-lang?
Version: 4.1.3

3 Languages

All of the exports in this section are provided both by redex/reduction-semantics (which includes all non-GUI portions of Redex) and also exported by redex (which includes all of Redex).

(define-language lang-name
  (non-terminal-spec pattern ...)
  ...)
 
non-terminal-spec = symbol
  | (symbol ...)

This form defines the grammar of a language. It allows the definition of recursive patterns, much like a BNF, but for regular-tree grammars. It goes beyond their expressive power, however, because repeated `name’ patterns and side-conditions can restrict matches in a context-sensitive way.

The non-terminal-spec can either by a symbol, indicating a single name for this non-terminal, or a sequence of symbols, indicating that all of the symbols refer to these productions.

As a simple example of a grammar, this is the lambda calculus:

  (define-language lc-lang
    (e (e e ...)
       x
       v)
    (c (v ... c e ...)
       hole)
    (v (lambda (x ...) e))
    (x variable-not-otherwise-mentioned))

with non-terminals e for the expression language, x for variables, c for the evaluation contexts and v for values.

(define-extended-language language language
  (non-terminal pattern ...)
  ...)

This form extends a language with some new, replaced, or extended non-terminals. For example, this language:

  (define-extended-language lc-num-lang
    lc-lang
    (e ....     ; extend the previous `e' non-terminal
       +
       number)
    (v ....
       +
       number)
    (x (variable-except lambda +)))

extends lc-lang with two new alternatives for both the e and v nonterminal, replaces the x non-terminal with a new one, and carries the c non-terminal forward.

The four-period ellipses indicates that the new language’s non-terminal has all of the alternatives from the original language’s non-terminal, as well as any new ones. If a non-terminal occurs in both the base language and the extension, the extension’s non-terminal replaces the originals. If a non-terminal only occurs in either the base language, then it is carried forward into the extension. And, of course, extend-language lets you add new non-terminals to the language.

If a language is has a group of multiple non-terminals defined together, extending any one of those non-terminals extends all of them.

(language-nts lang)  (listof symbol?)
  lang : compiled-lang?

Returns the list of non-terminals (as symbols) that are defined by this language.

(compiled-lang? l)  boolean?
  l : any/c

Returns #t if its argument was produced by `language’, #f otherwise.