• PLAI那些事01 AE


    AE : Concrete syntax

    <AE> ::= <num>
           | {+ <AE> <AE>}
           | {- <AE> <AE>}
    

    AE : Abstract syntax

    (define-type AE
      [num (n number?)]
      [add (lhs AE?)
           (rhs AE?)]
      [sub (lhs AE?)
           (rhs AE?)])
    

    parse : sexp -> AE

    (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)))])]))
    
    (define (parse sexp)
      (cond 
        [(number? sexp) (num sexp)] 
        [(and (= 3 (length sexp)) 
              (eq? (first sexp) ’+)) 
         (add (parse (second sexp))
              (parse (third sexp)))] 
        [(and (= 3 (length sexp)) 
              (eq? (first sexp) ’-)) 
         (sub (parse (second sexp))
              (parse (third sexp)))] 
        [else (error ’parse "bad syntax: ~a" sexp)]))
    
    (define (parse sexp)
      (cond
        [(number? sexp) (num sexp)]
        [(and (= 3 (length sexp)) (eq? (first sexp) '+))
         (add (parse (second sexp)) (parse (third sexp)))]
        [(and (= 3 (length sexp)) (eq? (first sexp) '-))
         (sub (parse (second sexp)) (parse (third sexp)))]
        [else (error 'parse "bad syntax: ~a" sexp)]))
    
    (define (parse sexp)
      (match sexp
        [(? number?) (num sexp)]
        [(list '+ l r) (add (parse l) (parse r))]
        [(list '- l r) (sub (parse l) (parse r))]
        [else (error 'parse "bad syntax: ~a" sexp)]))
    

    interp : AE -> number

    (define (interp ae)
      (type-case AE ae
        [num (n) n]
        [add (l r) (+ (interp l) (interp r))]
        [sub (l r) (- (interp l) (interp r))]))
    
  • 相关阅读:
    javascript重点笔记
    我的CSS架构
    排行榜妙用——CSS计数器
    回归基础从新认识——HTML+CSS
    前端开发工具(安装及常用技巧)——sublime text 3
    手机访问php环境移动端静态页面
    H5前端面试题及答案(2)
    H5前端面试题及答案(1)
    python笔记--学会使用Fiddler
    python进程/线程/协成
  • 原文地址:https://www.cnblogs.com/lastk/p/12832532.html
Copyright © 2020-2023  润新知