[plt-scheme] How best to abstract parser actions?

From: Grant Rettke (grettke at acm.org)
Date: Sun Sep 2 01:08:48 EDT 2007


I've got a simple language for specifying document; I can generate
either plain text or html. I would only like to write the parser once,
and abstract out its actions. Here is the existing code; the parser is
written twice which is unnecessary.

(define mydoc
    (title "Hello, world.")))

(define htmldoc
  (λ (doc)
    (match doc
      (('doc args ...)
         (printf "<html>~n")
         (map htmldoc args)
         (printf "</html>~n")))
      (('title title)
       (printf "<head><title>~a</title></head>~n" title)))))

(htmldoc mydoc)

(define txtdoc
  (λ (doc)
    (match doc
      (('doc args ...)
         (printf "Start document~n")
         (map txtdoc args)
         (printf "End document~n")))
      (('title title)
       (printf "Title: ~a~n" title)))))

(txtdoc mydoc)

What is the "best way" to abstract out the parser actions so that the
parser only has to be written once?

Units? Modules? Classes?

Best wishes,

Grant Rettke

Posted on the users mailing list.