Skip to content

Instantly share code, notes, and snippets.

@brendanberg
Created November 15, 2012 18:49
Show Gist options
  • Select an option

  • Save brendanberg/4080423 to your computer and use it in GitHub Desktop.

Select an option

Save brendanberg/4080423 to your computer and use it in GitHub Desktop.

Revisions

  1. brendanberg created this gist Nov 15, 2012.
    26 changes: 26 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /*
    * Use this at http://pegjs.majda.cz/online
    *
    * Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo
    * - becomes -
    * Fishy fish fishy fish fish fish fishy fish
    *
    * Also, the grammar is recursive, so it could also generate ALL THE FISH.
    */

    start
    = np:nounphrase " " vp:verbphrase "." { var s = np + " " + vp + "."; return s.charAt(0).toUpperCase() + s.slice(1); }

    nounphrase
    = pn:propernoun " " n:noun " " rc:relativeclause { return pn + " " + n + " " + rc; }
    / pn:propernoun " " n:noun { return pn + " " + n; }

    verbphrase
    = v:verb " " np:nounphrase { return v + " " + np; }

    relativeclause
    = np:nounphrase " " v:verb { return np + " " + v; }

    propernoun = "Buffalo" { return "fishy" }
    noun = "buffalo" { return "fish"; }
    verb = "buffalo" { return "fish"; }