From c486f0006b36a2488516124e0ae46fa838a7f35f Mon Sep 17 00:00:00 2001 From: Utz-Uwe Haus Date: Thu, 2 Oct 2008 10:43:58 +0200 Subject: [PATCH] Flesh out examples. Signed-off-by: Utz-Uwe Haus --- example.lisp | 3 ++- example.peg | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/example.lisp b/example.lisp index 59a62da..6305bd8 100644 --- a/example.lisp +++ b/example.lisp @@ -34,13 +34,14 @@ ;; load the opossum package (asdf:operate 'asdf:load-op :opossum) +;; => NIL ;; Create a parser for the grammar defined in example.peg, in a fresh package (setq *p* (opossum:make-string-parser "example.peg")) ;; => ... ;; use the parser to parse a string -(setq *t* (funcall *p* "14+7*(1+1)^2")) +(setq *t* (funcall *p* "14+7*(1+1)*2")) ;; => ... ;; and try out the code generated by the grammar diff --git a/example.peg b/example.peg index 38db164..9d7476d 100644 --- a/example.peg +++ b/example.peg @@ -1,3 +1,29 @@ # Sample PEG file to demonstrate CL-Opossum use -*- mode:peg -*- # $Id$ # +# Every successfully matched rule makes a list named DATA available to the action code + + +Expr <- Sum { data } + +Sum <- Product (('+' / '-') Product)* { + `(+ ,(first data) + ,(reduce #'(lambda (op/p tail) + (if (char= #\+ (car op/p)) + `(+ ,(second op/p) ,old) + `(- ,(second op/p) ,old)))) + (cdr data) :from-end T :initial-value 0)) + } + +Product <- Value (('*' / '/') Value)* { + `(* ,(first data) + ,(reduce #'(lambda (op/p tail) + (if (char= #\* (car op/p)) + `(* ,(second op/p) ,old) + `(/ ,(second op/p) ,old)))) + (cdr data) :from-end T :initial-value 1)) + } + +Value <- [1-9] [0-9]+ { (reduce #'(lambda (x y) (+ y (* x 10))) data :initial-value 0) } + / '(' Expr ')' { (second data) } + -- 2.11.4.GIT