;;;-*- Mode: Common-Lisp; Base:10; Package: tiger -*- ;; set up for johnson-yacc parser ;;; ----- Example grammars ----- (def-jyacc week7 (P ([ S ]* $) `(progn . ,%0)) (S (print E \;) `(format t "~a~%" ,%1)) (S (id = E \;) `(setf ,%0 ,%2)) (S (E $) %0) (E (E + Tm or E - Tm) `(,%1 ,%0 ,%2)) (E (Tm)) (Tm (Tm * F or Tm / F) `(,%1 ,%0 ,%2)) (Tm (F)) (F (id or num)) (F (|(| E |)|) %1)) (defun parse-week7 (tokens) ;; parse from a list of tokens, e.g. ( (id x) + (id y) $) (labels ((next() (let ((h (pop tokens))) (cond ((null h) '(*eof* . *eof*)) ; If we're at EOF ((numberp h) `(num . ,h)) ((eq h 'print) '(print . print)) ((and (symbolp h) (alpha-char-p (elt (symbol-name h) 0))) `(id . ,h)) ((atom h) (cons h h)) ; If it's a +, return (+ . +) (t (cons (car h) h))))) (err() (format t "~%error at ~s" tokens))) (week7 #'next #'err))) ;; Example: (parse-week7 '(a = 1 + 7 \; b = a * a \; print b - a \; $))