zoukankan      html  css  js  c++  java
  • 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))]))
    
  • 相关阅读:
    POJ 2773 Happy 2006------欧几里得 or 欧拉函数。
    Candies
    subway(dijkstra以最短时间代替最短路)
    昂贵的聘礼
    LightOJ 1074
    SPFA以及各种优化
    Making Huge Palindromes (Manacher)
    Making Huge Palindromes (KMP处理回文串)
    AC自动机基础
    扩展KMP
  • 原文地址:https://www.cnblogs.com/lastk/p/12832532.html
Copyright © 2011-2022 走看看