[No scan&parse]Any easy way to testing the code in page 72 ?

305 views
Skip to first unread message

Li Weijian

unread,
Jul 8, 2013, 11:46:25 PM7/8/13
to eo...@googlegroups.com
Hey, guys

I want to testing the code in page 72 in DrRacket, but I can't find the definition of scan&parse function before page 72.

After download the EOPL3's source code, I found scan&parse defined in chapter3\let-lang\lang.scm.

I tried to require the lang.scm in my code, but it seems that some code in lang.scm conflict with my testing code. 

There's a 'program' in lang.scm:
(define the-grammar
    '((program (expression) a-program)
...

But there's another similar definition in page 69:
(define-datatype program program?
  (a-program
    (exp1 expression?)))


I think I can solve this problem by spending a lot of time on modify and merge these files, but I don't think that's a good way to lean EOPL.

So I was wondering is there any easy way to debug the code in page 72?

Any help would be appreciated:)

My testing code was:

#lang eopl

(define-datatype program program?
  (a-program
   (exp1 expression?)))

(define-datatype expression expression?
  (const-exp
   (num number?))
  (diff-exp
   (exp1 expression?)
   (exp2 expression?))
  (zero?-exp
   (exp1 expression?))
  (if-exp
   (exp1 expression?)
   (exp2 expression?)
   (exp3 expression?))
  (var-exp
   (var identifier?))
  (let-exp
   (var identifier?)
   (exp1 expression?)
   (body expression?)))

(define-datatype expval expval?
  (num-val
   (num number?))
  (bool-val
   (bool boolean?)))

(define empty-env
  (lambda ()
    '()))

(define extend-env
  (lambda (var val env)
    (list var val env)))

(define apply-env
  (lambda (env search-var)
    (cond
      ((null? env) (report-no-binding-found search-var))
      ((eqv? (car env) search-var) (cadr env)) 
      (else
       (apply-env (caddr env) search-var)))))

(define report-no-binding-found
  (lambda (search-var)
    (eopl:error 'apply-env "No binding for ~s" search-var)))

(define identifier? symbol?)

(define init-env
  (lambda ()
    (extend-env
     'i (num-val 1)
     (extend-env
      'v (num-val 5)
      (extend-env
       'x (num-val 10)
       (empty-env))))))

(define expval->num
  (lambda (val)
    (cases expval val
      (num-val (num) num)
      (else (eopl:error 'expval->num "expval num extractor error:~s" val)))))

(define expval->bool
  (lambda (val)
    (cases expval val
      (bool-val (bool) bool)
      (else (eopl:error 'expval->bool "expval bool extractor error:~s" val)))))

(define run
  (lambda (string)
    (value-of-program (scan&parse string))))

(define value-of-program
  (lambda (pgm)
    (cases program pgm
      (a-program (exp1)
                 (value-of exp1 (init-env))))))

(define value-of
  (lambda (exp env)
    (cases expression exp
      (const-exp (num) (num-val num))
      (var-exp (var) (apply-env env var))
      (diff-exp (exp1 exp2)
                (let ((val1 (value-of exp1 env))
                      (val2 (value-of exp2 env)))
                  (let ((num1 (expval->num val1))
                        (num2 (expval->num val2)))
                    (num-val
                     (- num1 num2)))))
      (zero?-exp (exp1)
                 (let ((val1 (value-of exp1 env)))
                   (let ((num1 (expval->num val1)))
                     (if (zero? num1)
                         (bool-val #t)
                         (bool-val #f)))))
      (if-exp (exp1 exp2 exp3)
              (let ((val1 (value-of exp1 env)))
                (if (expval->bool val1)
                    (value-of exp2 env)
                    (value-of exp3 env))))
      (let-exp (var exp1 body)
               (let ((val1 (value-of exp1 env)))
                 (value-of body
                           (extend-env var val1 env)))))))


Mitchell Wand

unread,
Jul 18, 2013, 3:59:20 PM7/18/13
to eopl3
This code uses the sllgen package. At eopl3.com, download the whole code base (allcode.zip), and look in chapter3/let-lang .  You will see that scan&parse is defined in lang.scm .

Does that help?

--Mitch




--
You received this message because you are subscribed to the Google Groups "EOPL3" group.
To unsubscribe from this group and stop receiving emails from it, send an email to eopl3+un...@googlegroups.com.
To post to this group, send email to eo...@googlegroups.com.
Visit this group at http://groups.google.com/group/eopl3.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Li Weijian

unread,
Jul 19, 2013, 10:04:24 AM7/19/13
to eo...@googlegroups.com
Nope:(

I had test the code with lang.scm, but conflicted with the code I wrote.

Read the detail I just posted:)

在 2013年7月19日星期五UTC+8上午3时59分20秒,Mitch写道:

Mitchell Wand

unread,
Jul 24, 2013, 7:38:56 AM7/24/13
to eopl3
Dear Li,

Have you solved this problem yet?

--Mitch

Luca

unread,
Aug 1, 2013, 11:08:51 AM8/1/13
to eo...@googlegroups.com
Hi, it's very easy to test every program in the book.
That's how I do it:
1 - make sure you have this at the top of you definitions
#lang eopl

2 - Add the following:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Sacanner and parser

(define scanner-spec-1
  '((white-sp (whitespace) skip)
    (comment ("%" (arbno (not #\newline))) skip)
    (identifier (letter (arbno (or letter digit))) symbol)
    (number (digit (arbno digit)) number)))

(define grammar-1
  '(
    (program   
     (expression)
     a-program)
    (expression
     (number)
     const-exp)
    (expression
     ("-(" expression "," expression ")")
     diff-exp)
    (expression
     ("zero?(" expression ")")
     zero?-exp)
    (expression
     ("if" expression "then" expression "else" expression)
     if-exp)
    (expression
     (identifier)
     var-exp)
    (expression
     ("let" identifier "=" expression "in" expression)
     let-exp)
    (expression
     ("proc" "(" identifier ")" expression)
     proc-exp)
    (expression
     ("(" expression expression")")
     call-exp)))

(sllgen:make-define-datatypes scanner-spec-1 grammar-1)

(define list-the-datatypes
  (lambda ()
    (sllgen:list-define-datatypes scanner-spec-1 grammar-1)))

(define just-scan
  (sllgen:make-string-scanner scanner-spec-1 grammar-1))

(define scan&parse
  (sllgen:make-string-parser scanner-spec-1 grammar-1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

This code is int the appendix at the end of the book, you have to try and figure out these things on your own while using the book.
Luca

Li Weijian

unread,
Aug 2, 2013, 6:01:22 PM8/2/13
to eo...@googlegroups.com
Not yet:(

I have downloaded the source code from the website, I can understand the code of the chapter 3 in it.

It''s different from the code from the text book, there're no define-datatype program and define-datatype expression in it, it uses define the-grammar instead.

I think there should be some black magic in it, so I try to run the source code top.scm in racket, unfortunately, failed again:
`data-structures.scm:5:11: all-defined: not a provide sub-form in: (all-defined)`

I can't run the code typed by myself which was copied from the text book, and can't run the code downloaded from the website either, feel like I'm stuck here...
在 2013年7月24日星期三UTC+8下午7时38分56秒,Mitch写道:

Mitchell Wand

unread,
Aug 3, 2013, 5:37:51 PM8/3/13
to eopl3, Dan Friedman, Robby Findler
Hmm, this appears to be an issue with recent versions of Racket.

To run the code in Racket 5.3.5, change all occurrences of (all-defined) to (all-defined-out).

Sorry about that.

I am hoping to have this fixed in Racket 5.3.6 (to be released in August 2013).

--Mitch
Reply all
Reply to author
Forward
0 new messages