[racket] Survey of parsing libraries for Racket?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Mon Mar 12 15:58:56 EDT 2012

> Incidentally, I'm not sure of the performance implications, but I like the
> idea of having the parser for a programming language translate the syntax
> objects to sexp-like syntax objects promptly, and then "syntax-parse" the
> heck out of that newly sexp-encoded language to turn it into Racket code.


That's just crazy talk.

It's completely unreasonable to expect something like this, to have a
file called "calc.rkt"

;; calc.rkt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang planet dyoo/autogrammar/lalr
expr : term ('+' term)*
term : factor ('*' factor)*
factor : INT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


and expect to be able to use that as a language:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket/base
(require "calc.rkt"
         parser-tools/lex
         (prefix-in : parser-tools/lex-sre))

(define lex/1
  (lexer-src-pos [(:+ numeric) (token-INT lexeme)]
                 ["*"          (token-* lexeme)]
                 ["+"          (token-+ lexeme)]
                 [whitespace   (return-without-pos (lex/1 input-port))]
                 [(eof)        (token-EOF eof)]))

(define (tokenize ip)
  (port-count-lines! ip)
  (lambda () (lex/1 ip)))

(parse "a-sample-program"
       (tokenize (open-input-string "3 * 4 + 5 * 6")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


and just have it work and produce syntax objects.

Posted on the users mailing list.