Wondering what’s next for npm?Check out our public roadmap! »


    1.1.0 • Public • Published

    Nearley Generator

    This module takes a compiled Nearley parser grammar and turns it into an efficient fake-text generator that produces random strings from the grammar.

    To use:

    import NearleyGenerator from 'nearley-generator';
    import myGrammar from './compiled-nearley-grammar.js';
    let g = new NearleyGenerator(myGrammar);
    g.generate('startSymbol', convergenceFactor);

    The convergence (0 < convergenceFactor < 1) determines, roughly, how "deep" the generator will descend into recursive production rules. Using 1.0 means that any production for a every symbol is always equally likely, but may produce extremely large strings or take a long time to terminate, whereas using a value like 0.6 or 0.7 discourages following a recursive rule more than two or three levels deep.

    Custom PRNG

    By default, the generator uses Math.random() for randomness. To use a seedable or otherwise custom PRNG, a replacement function for Math.random can be passed as an additional argument to the constructor:

    let g = new NearleyGenerator(myGrammer, myRandom);
    // myRandom() should return a float in [0, 1)


    This package powers the mLab, a satire/parody site making fun of the nLab, a wiki for higher mathematics and category theory. You can view the Nearley grammar powering the site here.


    The algorithm used for generation was heavily inspired by this article by Eli Bendersky.


    npm i nearley-generator

    DownloadsWeekly Downloads






    Unpacked Size

    19.4 kB

    Total Files


    Last publish


    • avatar