OT: Recursive descent parsers
From: William Bland (news123_at_abstractnonsense.com)
Date: 06/22/04
- Next message: Dave Ebert: "Re: b432b"
- Previous message: Kenny Tilton: "Re: Why I'm giving up on Lisp for now"
- Next in thread: Antony Blakey: "Re: OT: Recursive descent parsers"
- Reply: Antony Blakey: "Re: OT: Recursive descent parsers"
- Reply: Paul: "Re: OT: Recursive descent parsers"
- Reply: William D Clinger: "Re: OT: Recursive descent parsers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 22 Jun 2004 03:50:33 GMT
Yet another rather off topic question, posted here because Lisp hackers
know everything ;-)
I missed out on a formal computing education, as I was busy doing pure
mathematics, so I never learned about parsers. This has become important
recently because I need to write one at work (unfortunately in Java rather
than Lisp, where it'd be much easier). I already know how to write it, and
I *think* it's what you'd call a "recursive descent parser", but every
time I try to read an article on parsers I get bogged down in the BNF,
which I don't find easy to read at all.
Basically the structure I'm thinking of for my parser is a top-level
function that dispatches on the first character it reads, and calls a
bunch of helper functions, something like:
(defun read-stuff ()
(let ((c (peek-char)))
(cond ((char= c #\Space)
(read-char)
(read-stuff))
((char= c #\()
(read-char)
(read-list))
((digit-char-p c)
(read-number))
(t
"ERROR!"))))
(defun read-list ()
(let ((c (peek-char)))
(cond ((char= c #\))
(read-char)
nil)
(t
(cons (read-stuff) (read-list))))))
(defun read-number ()
(read-number-helper 0))
(defun read-number-helper (acc)
(let ((digit (- (char-code (read-char))
(char-code #\0))))
(setf acc (+ (* 10 acc) digit))
(if (digit-char-p (peek-char))
(read-number-helper acc)
acc)))
but, of course, I'll have to write it in Java (yuck!). I write parsers
this way just because I can't imagine any other way to write them (well, I
suppose I can, but they wouldn't be pretty). I'd be interested to know
though:
1) is this a "normal" way to write a parser? I've seen similar things in
Lisp books, so I know I'm not the *only* person who writes them this way,
but do other people write them like this (e.g. when people write parsers
for languages like Java)?
2) is this what people mean when they say "recursive descent parser"?
Cheers,
Bill.
-- Dr. William Bland. It would not be too unfair to any language to refer to Java as a stripped down Lisp or Smalltalk with a C syntax. (Ken Anderson).
- Next message: Dave Ebert: "Re: b432b"
- Previous message: Kenny Tilton: "Re: Why I'm giving up on Lisp for now"
- Next in thread: Antony Blakey: "Re: OT: Recursive descent parsers"
- Reply: Antony Blakey: "Re: OT: Recursive descent parsers"
- Reply: Paul: "Re: OT: Recursive descent parsers"
- Reply: William D Clinger: "Re: OT: Recursive descent parsers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|