我正在嘗試使用parser-tools
在Racket
中編寫一個簡單的解析器。我得到了一個我無法解釋的行爲(我是一個Racket新手,也許這是微不足道的)。等字符串之間的差異
考慮下面的代碼:
#lang racket
(require parser-tools/yacc
parser-tools/lex
(prefix-in : parser-tools/lex-sre))
(define-tokens value-tokens ;;token which have a value
(STRING-VALUE))
(define-empty-tokens op-tokens ;;token without a values
(EOF))
(define-lex-abbrevs ;;abbreviation
[STRING (:+ (:or (:/ "a" "z") (:/ "A" "Z") (:/ "0" "9") "." "_" "-"))]
)
(define lex-token
(lexer
[(eof) 'EOF]
;; recursively call the lexer on the remaining input after a tab or space. Returning the "1+1")
;; result of that operation. This effectively skips all whitespace.
[(:or #\tab #\space #\newline)
(lex-token input-port)]
[(:seq STRING) (token-STRING-VALUE lexeme)]
))
(define test-parser
(parser
(start query)
(end EOF)
(tokens value-tokens op-tokens)
(error (λ(ok? name value) (printf "Couldn't parse: ~a\n" name)))
(grammar
(query [(STRING-VALUE) $1])
)))
(define s (open-input-string
"abcd123"))
(define res
(test-parser (lambda() (lex-token s))))
(define str "abcd123")
這些定義後,res
是一個字符串:
> (string? res)
#t
,所以是str
。
如果我嘗試執行與"abcd123"
字符串比較,我得到兩個不同的結果:
> (eq? res "abcd123")
#f
> (eq? str "abcd123")
#t
這是爲什麼?我在這裏錯過了什麼?
可能重複[eq?,eqv?,equal ?,和=在方案中有什麼區別?](http://stackoverflow.com/questions/16299246/what-is-the-difference-between- EQ-當量相等和 - 在-方案) – Sylwester