2
votes

I have a txt file "blah.txt" which reads

word 3 + 6

Now consider the following code:

(define c (read-char file))
(define (toke c)
        (cond
         ((null? c)(write "empty"))

        ((equal? (read-char c) #\space)(write 'space)
         ;           (toke (with the next character)
         )
         ((equal? (read-char c) #\+) (write '+)
          ;              (toke (with the next character)
         )
        ((equal? (read-char c) #\-) (write '-)
          ;               (toke (with the next character)
         )
         ((equal? (read-char c) #\*) (write '*)
          ;               (toke (with the next character)
         )
         ((equal? (read-char c) #\/) (write '/)
          ;               (toke (with the next character)
         )
         ((equal? (read-char c) #\=) (write '=)
          ;               (toke (with the next character)
           )           
         ((equal? (read-char c) #\() (write 'LPAREN)
            ;             (toke (with the next character)
         )
         ((equal? (read-char c) #\)) (write 'RPAREN)
          ;               (toke (with the next character)
         )
         ((char-numeric? (read-char c)) (write 'Num)
          ;                  (toke (with the next character)
         )

         ((char-alphabetic? (read-char c))(write 'ID)
          ;                    (toke (with the next character)
         )
         (else
          (write "error"))))

I'm not looking to output to a text file. I simply want to be able to move on to the next character. I've had unsuccessful attempts at solving it. Also for some reason I keep getting errors with char-numeric? and char-alphabetic? telling me that I have given it the eof instead of a what it expects when I save my file as 3 a + r (for example)

I know it involves changing it into a string but I keep getting a string of "word 3 + 6" for which I cannot do (car). let me explain that a little further I know you can do (define list file->list "blah.txt") which will contain '(word 3 + 4) but then doing (define str file->string "blah.txt") it will contain "word 3 + 4" and I cannot do (car str) to get "word" out because the whole (car) is "word 3 + 4" Please forgive me if I can't explain myself correctly as I am new to scheme

3
@ColeJohnson Racket (Scheme), it's in the tags.Óscar López
@ÓscarLópez i've never heard of it. so i didn't knowCole Johnson
Being Lindsay Lohan didn't help much on Stack Overflow, did it? :)Óscar López
@OscarLopez Haha! you should add that as an answer to see how many upvotes you getLiam McInroy

3 Answers

1
votes

Read the contents of the file using (file->lines "blah.txt"), that will return a list, with each line of the file as a string in the list.

After that, it'll be simple to manipulate each line; for instance using the procedure string->list (see the documentation) to convert a line to a list of characters and iterating over each of the characters by cdr-ing over the list.

1
votes

Racket (and more generally, Scheme) provides a port datatype that allows you to get sequential access to a file or stream. You can peek and read individual bytes from it.

There's some reference Racket documentation here: Byte and String input.

As an example of such a scanner, you can look at Section 6 of my mini-tutorial on making a Racket-based language.

Note that a string is not itself a port, but we can create a port whose source comes from a string. Racket provides an open-input-string function to turn strings into input ports. So you should be able to do something like this:

#lang racket
(define myport (open-input-string "hello"))

(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))
(printf "I see: ~s\n" (read-char myport))

to see the individual characters coming out. Change read-char to peek-char and you'll see that peek-char does not take the data out of the port, but allows you to peek into it.

0
votes

I recommend reading a little on how to implement scanners in Scheme. Eric Hilsdale and Christopher T. Haynes has written some very nice notes.

First the scanner: http://www.cs.indiana.edu/eip/compile/scanparse.html

Then the big picture: http://www.cs.indiana.edu/eip/compile/