;;; LALR(1) parser generator -- sample program ;;; ;;; (C) 2008 Gergő ÉRDI ;;; http://cactus.rulez.org/ (in-package :common-lisp-user) (eval-when (:compile-toplevel :load-toplevel :execute) (require :lexer)) ;; (require :parser-shiftreduce)) (defpackage :cactus.parser-shiftreduce.example (:use :common-lisp :cactus.parser-shiftreduce :lexer) (:export :test)) (in-package :cactus.parser-shiftreduce.example) (deflexer arithmetics-lexer ("[0-9]+" (return (values 'intlit (int %0)))) ("[+*()]" (return %0)) ("[:space:]+") ) (defparser/actions arithmetics-parser E (E ((T) (op) op) ((E "+" T) (left nil right) (+ left right))) (T ((F) (op) op) ((T "*" F) (left nil right) (* left right))) (F (("(" E ")") (nil op nil) op) ((intlit) (i) (second i)))) (defun test (input) (labels ((terminal-matches-p (terminal token) (equal (car terminal) token))) (let* ((lex (arithmetics-lexer input)) (tokens (loop for foo = (multiple-value-list (funcall lex)) while (car foo) collecting foo))) (pprint (arithmetics-parser tokens #'terminal-matches-p))))) (eval-when (:execute) (with-parser-debug-stream t (test "4 * (3 + 1) * 2 + (3 + 7)")))