[antlr-interest] recursive-descent Lisp example
Terence Parr
parrt at cs.usfca.edu
Sat Jan 14 12:12:05 PST 2006
Howdy. Henry Baker has a nice example:
(defun parse-int (&aux (s +1) d (n 0))
(and
(and (or (match #\+)
(and (match #\-) (setq s -1))
(and))
(match-type digit d) (setq n (ctoi d))
(not (do () ((not (and (match-type digit d)
(setq n (+ (* n 10) (ctoi d)))))))))
(* s n)))
from
http://home.pipeline.com/~hbaker1/Prag-Parse.html
this parses signed integers and returns the integer. I'm no Lisp
expert but I'm pretty sure that since "the operations [], {} and $
correspond to the Common Lisp control structures AND, OR, and DO", I
could go from rule to Lisp defun rather easily.
Rules are just pure-functional functions that return attributes; we
use methods instead in OO languages but Lisp is the king of this sort
of stuff. My paper on ST by the way is called "A Pure Functional
Language for Generating Structured Text". I harp on how the nature
of code gen and enforcing model-view separation tightly brackets the
solution to a pure functional language (with lazy eval)....that is ST :)
Ter
More information about the antlr-interest
mailing list