;; C311 Programming Laguages ;; Fall 2011 ;; Dana Vrajitoru ; The following are temporarily defined to test the other parse ; functions. (defun parse-T () '(T)) (defun parse-TT () '(TT)) ; Consume one token from the input string and return true. (defun move-input () (pop input) t) ; E => T TT (defun parse-E () (let ((Tres nil) (TTres nil)) (setq Tres (parse-T) TTres (parse-TT)) (if (and Tres TTres) (list 'E Tres TTres) nil))) ; Testing only: (setq input '()) (parse-E) ;(E (T) (TT)) ; F => ( E ) | id | number (defun parse-F () (let ((token (car input)) (Exp nil)) (cond ((not input) nil) ((or (symbolp token) (numberp token)) (move-input) (list 'F token)) ((equal token "(") (move-input) (if (and (setq Expr (parse-E)) (equal (car input) ")") (move-input)) (list 'F "(" Expr ")") nil)) (t nil)))) ; Testing only (setq input '(2)) ;(2) (parse-F) ;(F 2) (setq input '(a)) ;(a) (parse-F) ;(F a) (setq input '( "(" ")" )) ;("(" ")") (parse-F) ;(F "(" (E (T) (TT)) ")") ; Parses an entire expression. If the expression is correct, it ; returns the parse tree. (defun parse-input (L) (setq input L) (if (not L) nil (let ((T (parse-E))) (if (and T (not input)) T nil))))