arbno

> I'm trying to use arbno for the questions on the let extensions 
> (chapter 3). However, the behaviour of "arbno" is nowhere specified in 
> the book, and I also didn't find the code for it. For example, how can 
> a value be extracted from an arbno? 

Here is an example to show how an arbitrary number of things are 
 specified in the grammar using arbno, 
 specified in the datatype using list-of, 
 parsed, 
 extracted as lists. 

; <expression> := <number> 
;              := <identifier> 
;              := let { <identifier = <expression> }* in <expression> 

(define scanner 
  '((white-sp (whitespace) skip) 
    (number (digit (arbno digit)) number) 
    (identifier (letter (arbno (or letter digit "?"))) symbol))) 

(define grammar 
  '((expression (number) lit-exp) 
    (expression (identifier) var-exp) 
    (expression ("let" (arbno identifier "=" expression) "in" 
expression) let-exp))) 

(sllgen:list-define-datatypes scanner grammar) 
((define-datatype 
   expression 
   expression? 
   (lit-exp (lit-exp1 number?)) 
   (var-exp (var-exp2 symbol?)) 
   (let-exp (let-exp3 (list-of symbol?)) (let-exp4 (list-of 
expression?)) (let-exp5 expression?)))) 

(sllgen:make-define-datatypes scanner grammar) 

(define scan-parse (sllgen:make-string-parser scanner grammar)) 

(define sp (sllgen:make-string-parser scanner grammar)) 
(define exp (sp "let x = 1 y = 2 z = 3 in x")) 

(cases expression exp 
  (lit-exp (datum) datum) 
  (var-exp (id) id) 
  (let-exp (ids rands body) ids)) 
;=> (x y z) 

(cases expression exp 
  (lit-exp (datum) datum) 
  (var-exp (id) id) 
  (let-exp (ids rands body) rands)) 
;=> (#<struct:lit-exp> #<struct:lit-exp> #<struct:lit-exp>) 

;---------- 
; a peek inside the Scheme implementation 
(require (only mzscheme print-struct)) 
(print-struct #t) 

exp 
;=> #(struct:let-exp (x y z) (#(struct:lit-exp 1) #(struct:lit-exp 2) 
#(struct:lit-exp 3)) #(struct:var-exp x)) 

(cases expression exp 
  (lit-exp (datum) datum) 
  (var-exp (id) id) 
  (let-exp (ids rands body) rands)) 
;=> (#(struct:lit-exp 1) #(struct:lit-exp 2) #(struct:lit-exp 3)) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值