tags:

views:

89

answers:

2

I am following the "Programming Languages :Application and Interpretation"

http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/2007-04-26/plai-2007-04-26.pdf (Page 21)

Now I am working on Page 21,with all the test cases. I could only pass the first one,while all test cases that having "with" fails.I realized that my parser doesnt have statements to cope with "with".

This is my parser at moment:

(define (parse sexp)
  (cond
    [(number? sexp)(num sexp)]
    [(list? sexp)
     (case (first sexp)
       [(+) (add (parse (second sexp))
                 (parse (third sexp)))]
       [(-) (sub (parse (second sexp))
                 (parse (third sexp)))])]))

So could you please help is it because this parse function that causes my above tests fail? Thanks.

+2  A: 

Actually, if you read further on,

Just when we thought we were done, we find that several of the test cases above (can you determine which ones?) generate a free-identifier error

He explains why the "with" expressions fail.

Don
No, that's not the problem that Robert has -- that part talks about problems in the implementation of the substitution function, but in the above parser there is no cases that produces such expressions in the first place.
Eli Barzilay
Oops! I should have read his code more clearly before looking to see the reference he was using...
Don
+4  A: 

Your parser is far from a complete one, so trying to deal with the actual implementation of with is not something that you should try yet. If you're taking this in the context of some class, you definitely need to consult the course staff. If you're trying this yourself, then you need to take things slowly and make sure you have a working parser before you get to the rest of the code.

One resource that can help you with that is my class notes, which contain an example for such parsers. (I just got to cover that exact point in the material today, btw.) But if you do use it, then you should note that it is diverging from the PLAI code in a number of aspects -- the language is a typed language, not plain scheme; the parsers that we write use match, and the whole organization of programs is slightly different. Still, it should give you a rough idea how to proceed if you're doing this on your own, and get stuck.

Eli Barzilay