diff --git a/collects/parser-tools/examples/calc.ss b/collects/parser-tools/examples/calc.ss index 0f067e7480..7741fea757 100644 --- a/collects/parser-tools/examples/calc.ss +++ b/collects/parser-tools/examples/calc.ss @@ -1,16 +1,18 @@ +#lang scheme + ;; An interactive calculator inspired by the calculator example in the bison manual. ;; Import the parser and lexer generators. (require parser-tools/yacc parser-tools/lex - (prefix : parser-tools/lex-sre)) + (prefix-in : parser-tools/lex-sre)) (define-tokens value-tokens (NUM VAR FNCT)) (define-empty-tokens op-tokens (newline = OP CP + - * / ^ EOF NEG)) ;; A hash table to store variable values in for the calculator -(define vars (make-hash-table)) +(define vars (make-hash)) (define-lex-abbrevs (lower-letter (:/ "a" "z")) @@ -61,8 +63,8 @@ [(exp) $1]) (exp [(NUM) $1] - [(VAR) (hash-table-get vars $1 (lambda () 0))] - [(VAR = exp) (begin (hash-table-put! vars $1 $3) + [(VAR) (hash-ref vars $1 (lambda () 0))] + [(VAR = exp) (begin (hash-set! vars $1 $3) $3)] [(FNCT OP exp CP) ($1 $3)] [(exp + exp) (+ $1 $3)] @@ -79,10 +81,9 @@ (letrec ((one-line (lambda () (let ((result (calcp (lambda () (calcl ip))))) - (if result - (begin - (printf "~a~n" result) - (one-line))))))) + (when result + (printf "~a~n" result) + (one-line)))))) (one-line))) -(calc (open-input-string "(1 + 2 * 3) - (1+2)*3")) +(calc (open-input-string "x=1\n(x + 2 * 3) - (1+2)*3")) diff --git a/collects/parser-tools/examples/example-parser.ss b/collects/parser-tools/examples/example-parser.ss index fb96e380fc..6eecc75625 100644 --- a/collects/parser-tools/examples/example-parser.ss +++ b/collects/parser-tools/examples/example-parser.ss @@ -7,8 +7,9 @@ (provide malgol-lexer malgol-parser) - (define-tokens regular (ID LITNUM STRING)) - (define-empty-tokens keywords (PROCEDURE SEMI BEGIN IN END IF ELSE EQUALS EOF)) + (define-tokens regular (ID LITNUM STRING LBRACK RBRACK COMMA LPAREN RPAREN)) + (define-empty-tokens keywords (PROCEDURE SEMI BEGIN IN END IF THEN ELSE EQUALS EOF + ARROW TRUE FALSE DATATYPE CASES)) (display (equal? (token-ID "foo") (token-ID "foo"))) (newline) @@ -19,13 +20,13 @@ (digit (:/ "0" "9")) (id-cont (:or id-start digit))) - (define example-lexer + (define malgol-lexer (lexer ((eof) (token-EOF)) (whitespace (malgol-lexer input-port)) ("=" (token-EQUALS)) (";" (token-SEMI)) ("begin" (token-BEGIN)) - ("end" (token-end)) + ("end" (token-END)) ((:+ digit) (token-LITNUM lexeme)) ((:: id-start (:* id-cont)) (token-ID lexeme)) ((:: #\" (:* (:or (:~ #\") "\\\"")) #\") (token-STRING (substring lexeme 1 (- (string-length lexeme) 1))))))