*** empty log message ***

original commit: f98d94e1e4359e7ee4798a3a9f05a0173bfcc9fd
tokens
Scott Owens 21 years ago
parent 596cdd18f7
commit 41597427bb

@ -4,7 +4,7 @@
;; Import the parser and lexer generators. ;; Import the parser and lexer generators.
(require (lib "yacc.ss" "parser-tools") (require (lib "yacc.ss" "parser-tools")
(lib "lex.ss" "parser-tools") (lib "lex.ss" "parser-tools")
(lib "readerr.ss" "syntax")) (prefix : (lib "lex-sre.ss" "parser-tools")))
(define-tokens value-tokens (NUM VAR FNCT)) (define-tokens value-tokens (NUM VAR FNCT))
(define-empty-tokens op-tokens (newline = OP CP + - * / ^ EOF NEG)) (define-empty-tokens op-tokens (newline = OP CP + - * / ^ EOF NEG))
@ -13,30 +13,29 @@
(define vars (make-hash-table)) (define vars (make-hash-table))
(define-lex-abbrevs (define-lex-abbrevs
(lower-letter (- "a" "z")) (lower-letter (:/ "a" "z"))
(upper-letter (- #\A #\Z)) (upper-letter (:/ #\A #\Z))
;; (- 0 9) would not work because the lexer does not understand numbers. (- #\0 #\9) is ok too. ;; (:/ 0 9) would not work because the lexer does not understand numbers. (:/ #\0 #\9) is ok too.
(digit (- "0" "9"))) (digit (:/ "0" "9")))
(define calcl (define calcl
(lexer (lexer
[(eof) 'EOF] [(eof) 'EOF]
;; recursively call the lexer on the remaining input after a tab or space. Returning the ;; recursively call the lexer on the remaining input after a tab or space. Returning the
;; result of that operation. This effectively skips all whitespace. ;; result of that operation. This effectively skips all whitespace.
[(: #\tab #\space) (calcl input-port)] [(:or #\tab #\space) (calcl input-port)]
;; The parser will treat the return of 'newline the same as (token-newline) ;; (token-newline) returns 'newline
[#\newline 'newline] [#\newline (token-newline)]
[(: "=" "+" "-" "*" "/" "^") (string->symbol lexeme)] ;; Since (token-=) returns '=, just return the symbol directly
[(:or "=" "+" "-" "*" "/" "^") (string->symbol lexeme)]
["(" 'OP] ["(" 'OP]
[")" 'CP] [")" 'CP]
["sin" (token-FNCT sin)] ["sin" (token-FNCT sin)]
[(+ (: lower-letter upper-letter)) (token-VAR (string->symbol lexeme))] [(:+ (:or lower-letter upper-letter)) (token-VAR (string->symbol lexeme))]
[(+ digit) (token-NUM (string->number lexeme))] [(:+ digit) (token-NUM (string->number lexeme))]
;; Strings which dr/mzscheme does not think of as symbols (such as . or ,) must be [(:: (:+ digit) #\. (:* digit)) (token-NUM (string->number lexeme))]))
;; entered as a string or character. "." would also be ok.
[(@ (+ digit) #\. (* digit)) (token-NUM (string->number lexeme))]))
(define calcp (define calcp
@ -85,3 +84,5 @@
(printf "~a~n" result) (printf "~a~n" result)
(one-line))))))) (one-line)))))))
(one-line))) (one-line)))
(calc (open-input-string "(1 + 2 * 3 3) 3)\n(1.2 + 3.3) / 44 \n !"))

@ -16,7 +16,7 @@
;; A token is either ;; A token is either
;; - symbol ;; - symbol
;; - (make-token symbol any) ;; - (make-token symbol any)
(define-struct token (name value)) (define-struct token (name value) (make-inspector))
;; token-name*: token -> symbol ;; token-name*: token -> symbol
(define (token-name* t) (define (token-name* t)

@ -207,7 +207,7 @@
(define (extract-no-src-pos ip) (define (extract-no-src-pos ip)
(extract-helper ip #f #f)) (extract-helper ip #f #f))
(define-struct stack-frame (state value start-pos end-pos)) (define-struct stack-frame (state value start-pos end-pos) (make-inspector))
(define (make-empty-stack i) (list (make-stack-frame i #f #f #f))) (define (make-empty-stack i) (list (make-stack-frame i #f #f #f)))
@ -253,7 +253,7 @@
start-pos start-pos
end-pos) end-pos)
stack)) stack))
(remove-input)) (remove-input tok val start-pos end-pos))
(else (else
;; (printf "discard state:~a~n" (car stack)) ;; (printf "discard state:~a~n" (car stack))
(cond (cond

Loading…
Cancel
Save