2

I am trying to write a simple parser in Racket, using the parser-tools. I got a behaviour which I could not explain (I am a Racket newbie, perhaps that is trivial).

Consider the following code:

#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")

After those definitions, res is a string:

> (string? res)
#t

and so is str.

If I try to execute a comparison with the "abcd123" string I get two different results:

> (eq? res "abcd123")
#f

> (eq? str "abcd123")
#t

Why is that? What am I missing here?

Aslan986
  • 9,984
  • 11
  • 44
  • 75
  • possible duplicate of [What is the difference between eq?, eqv?, equal?, and = in scheme?](http://stackoverflow.com/questions/16299246/what-is-the-difference-between-eq-eqv-equal-and-in-scheme) – Sylwester Feb 08 '14 at 21:07

2 Answers2

5

You should compare strings with equal?.

soegaard
  • 30,661
  • 4
  • 57
  • 106
2

Like many programming languages there is a difference between same object and two objects that look the same. You probably should have a look at the Question about the difference between eq?, eqv?, equal? and =

Racket has string=? which compares strings specifically and might be faster than the less specific equal?.

Community
  • 1
  • 1
Sylwester
  • 47,942
  • 4
  • 47
  • 79