helbertballerina
helbertballerina

Reputation: 31

Writing a parser in Racket #lang plai

I need to write a parser in racket for this abstract syntax:

;; <DE> ::= <num>
;;     | {distribution <num>*}
;;     | {uniform <num> <num>}
;;     | {+ <DE> <DE>}
;;     | {- <DE> <DE>}
;;     | {* <DE> <DE>}
;;     | {with {<id> <DE>} <DE>}
;;     | <id>

(define-type Binding
  [binding (name symbol?) (named-expr DE?)])

(define-type DE
  [distribution (values (listof number?))]
  [id (name symbol?)]
  [binop (op procedure?) (lhs DE?) (rhs DE?)]
  [with (b Binding?) (body DE?)])

What I have so far is:

(define (parse sexp)
  (match sexp
    [(? symbol?) (id sexp)]
    [sexp (distribution (values (list sexp)))]
    [(list '+ l r) (binop + (parse l) (parse r))]
    [(list '- l r) (binop - (parse l) (parse r))]
    [(list '* l r) (binop * (parse l) (parse r))]))

Note that {uniform a b}, is a discrete uniform distribution from a to b (inclusive). If a > b, then it's empty.

This is not working as expected and I can't get it right. There aren't resources on the web to help me, or at least I couldn't find them.

Is anyone able to explain me where I'm wrong and what the solution could be? I really have no clue. Thanks!

Upvotes: 3

Views: 508

Answers (0)

Related Questions