Code Monkey home page Code Monkey logo

peg's Introduction

(peg)

(peg) is a small R7RS Scheme library for parsing text according to Parsing Expression Grammars (PEGs for short). It's split up into small, cohesive modules that deal, separately, with specifying grammars, providing input and parsing.

Example

The following program tries to match parentheses in a few input strings. This is a common example of a grammar that cannot be matched using regular expressions.

(import (scheme base)
        (scheme write)
        (peg grammar)
        (peg parse)
        (peg input string))


;; Grammar for matching empty parentheses.
(define-grammar (matching-parens parens)
  (parens (group (lit-char #\() (optional parens) (lit-char #\)))))

;; Try matching on the grammar and report how it went
(define (show-case input-string)

  (define (present status)
    (display "Input: ")
    (write input-string)
    (display status)
    (newline))

  (let* ((input (string->input input-string))
         (result (parse matching-parens input)))

    (cond ((parse-fail? result) (present " does not match"))

          (else (present " matches")))))

;; Try all the cases out
(for-each show-case
          (list "" "()" "()(" ")()" "(())"))

The program outputs:

Input: "" does not match
Input: "()" matches
Input: "()(" matches
Input: ")()" does not match
Input: "(())" matches

Note, that the parsers is perfectly happy with matching only a prefix of the input (ie, not consuming all of it), as is the case with "()(".

Plans

Implementing a packrat parser, which supports PEG grammars in linear time might be worthwhile.

Generating random strings that will match a given grammar might be a fun if not very useful endeavour.

License

The library is licensed under the Mozilla Public License 2.0 (for more details read the LICENSE file).

peg's People

Contributors

szabba avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.