Skip to content
coonsta edited this page Sep 13, 2010 · 19 revisions

Guruguru is a parsing library for Emacs.

ぐるぐる (guru-guru) is a Japanese onomatopoeiac word meaning “around and around.” PEG parsers memoize the result of parsing a given expression at a given input position. This means parsers operate in linear time, even if the parser has to backtrack, because given a grammar with a fixed number of expressions N and an input of length M the parser will compute at most N·M results.

Guruguru uses a modified PEG algorithm that can parse left-recursive rules by repeatedly evaluating left-recursive expressions. This means Guruguru parsers aren’t guaranteed to operate in linear time; instead they may spend time going “around and around.”

Getting Guruguru

  1. git clone git://github.com/coonsta/guruguru.git See also Source.
  2. Add guruguru to your load-path. For example, add this to your .emacs file: (push "~/guruguru" load-path)

Example

The following example demonstrates evaluating simple arithmetic expressions with Guruguru:

(require 'guruguru)

(defvar expressions
  (gg-grammar
    (start x := term eof -> x)
    (term
       x := term ?+ y := fact -> (+ x y)
     | x := term ?- y := fact -> (- x y)
     | fact)
    (fact
       x := fact ?* y := num -> (* x y)
     | x := fact ?/ y := num -> (/ x y)
     | num)
    (num
       ?0 -> 0 | ?1 -> 1 | ?2 -> 2 | ?3 -> 3 | ?4 -> 4
     | ?5 -> 5 | ?6 -> 6 | ?7 -> 7 | ?8 -> 8 | ?9 -> 9)))

(defun expression-eval ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (let ((result (gg-parse expressions)))
      (message "%s" (if result (cdr result) "parse failed")))))

gg-grammar lets you define a grammar in lispy-syntax-BNF. gg-parse interprets the grammar against the current buffer and returns (t . result) if the parse succeeded or nil if it failed.

After you evaluate the above definitions (say, by putting them in the scratch buffer and typing M-x eval-buffer) you can evaluate simple arithmetic expressions by opening an empty buffer, entering 2*3+7, and then typing M-x expression-eval.

See Defining Grammars.

Limitations

Guruguru has no built-in support for error reporting or error recovery. You can add error recovery to your parsers by adding low-priority alternatives that consume arbitrary input up to some resynchronization token.

See also Issues.

Clone this wiki locally