peggen
()
(require peg-gen) | package: peg-gen |
1 Overview
This module defines a collection of PEG rackcheck generators. To generate well-formed PEGs, each term is always generated as a tern (peg, nullable, headset). This approach allows for incremental type correct construction of the term.
When generating a grammar, the context of all possible variables (Γ) is kept and updated accordingly. An additional context called Δ is used to avoid generated calls to non-terminals that would result in left recursion.
The general use case for this library is the function gen:peg whose arguments are the maximum number of variables, the maximum number of terminal symbols, and the maximum depth of a peg.
> (sample (gen:peg 3 2 1) 3)
'((∅ 0 ())
(∅ 0 ())
((R (• 0 S) (S (/ 2 0) (F (/ 2 ε) ∅)))
(/ 0 0)
((F #t ()) (S #f ()) (R #f ()))))
G (Grammar) : A list of variables followe by a peg. The list ends with ∅ (empty grammar)
e: A peg expression
Context : The type context for gamma
2 Generators reference
procedure
(gen:Γ maxVars [#:varSize varSize]) → gen?
maxVars : exact-positive-integer? varSize : exact-positive-integer? = 0
procedure
(gen:peg maxVars maxLits maxDepth) → gen?
maxVars : exact-positive-integer? maxLits : exact-positive-integer? maxDepth : exact-positive-integer?
maxVars : The maximum number of variables to be present in the grammamr
maxVars : The maximum number of literals to be present in the grammamr
maxDepth : The maximum height of a PEG AST (all leaves have high 0)
procedure
(gen:grm G Γ Δ Σ n pmax) → gen?
G : (list/c symbol? peg G) Γ : (list/c symbol? boolean? (listof symbol?)) Δ : (hash/c symbol? (listof symbol?)) Σ : (listof natural?) n : exact-positive-integer? pmax : exact-positive-integer?
procedure
(gen:expr Γ Δ Σ b p) → gen?
Γ : (list/c symbol? boolean? (listof symbol?)) Δ : (listof symbol?) Σ : (listof natural?) b : boolean? p : exact-positive-integer?
Γ: is a list of terns: v is a variable name; nullable is a value that determines whether or not that variable is nullable; italic{headset} is a list of possible variables starting the body of v.
Δ: is a list of constraints - forbidden variables
Σ: is an alphabet
b: Should be #t whenever the generated expression must be nullable.
p: Depth of the expression. If p is 0, only generate terminals or allowed variables. Samples n values from g.
procedure
(gen:var varSize) → gen?
varSize : exact-positive-integer?
procedure
(gen:symbolVar varSize) → gen?
varSize : exact-positive-integer?