; -*- Hen -*- ;;; ;;;; Simple calculator in Scheme ;;; ;; ;; @created "Tue Jan 6 12:47:23 2004" ;; @modified "Mon Oct 25 11:07:24 2004" ;; @author "Dominique Boucher" ;; @copyright "Dominique Boucher" ;; ;; Simple arithmetic calculator. ;; ;; This program illustrates the use of the lalr-scm parser generator ;; for Scheme. It is NOT robust, since calling a function with ;; the wrong number of arguments may generate an error that will ;; cause the calculator to crash. ;;; ;;;; The LALR(1) parser ;;; (require-extension lalr) (define calc-parser (lalr-parser ;; --- Options ;; output a parser, called calc-parser, in a separate file - calc.yy.scm, (output: calc-parser "calc.yy.scm") ;; output the LALR table to calc.out (out-table: "calc.grm.out") ;; --- token definitions (ID NUM NEWLINE LPAREN RPAREN (nonassoc: =) (left: - +) (left: * /) (left: COMMA) (nonassoc: uminus)) (lines (lines line) : (display-result $2) (line) : (display-result $1)) ;; --- rules (line (NEWLINE) : (void) (assign NEWLINE) : $1 (expr NEWLINE) : $1 (error NEWLINE) : #f) (assign (ID = expr) : (add-binding $1 $3)) (expr (NUM) : $1 (ID) : (get-binding $1) (ID LPAREN RPAREN) : (invoke-proc $1 (list)) (ID LPAREN args RPAREN) : (invoke-proc $1 $3) (expr + expr) : (+ $1 $3) (expr - expr) : (- $1 $3) (expr * expr) : (* $1 $3) (expr / expr) : (/ $1 $3) (- expr (prec: uminus)) : (- $2) (LPAREN expr RPAREN) : $2) (args (expr) : (list $1) (args COMMA expr) : (cons $3 $1))))