Re: OT: Recursive descent parsers
From: Paul (nath5573_at_uidaho.edu)
Date: 06/22/04
- Next message: Albert Reiner: "si(x) in Maxima (Was: Mathematica vs. Lisp)"
- Previous message: Antony Blakey: "Re: OT: Recursive descent parsers"
- In reply to: William Bland: "OT: Recursive descent parsers"
- Next in thread: William D Clinger: "Re: OT: Recursive descent parsers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 22 Jun 2004 01:41:16 -0700 To: William Bland <news123@abstractnonsense.com>
Look into the structure of lex and yacc(flex/bison).
The man pages are not too bad.
If you are familiar with automata theory and computability, a technical
book on parsing should bring you up to speed easily.
There are three general questions:
-> lexical analysis (is this string in the set of permissable words, and
is it [keyword|identifier|etc]?)
->syntactical anlysis (is this string of words in the grammar of this
language or can it be transformed into the grammar of this language?
(here is where bnf shows up)
->semantic analysis (assigning meaning to each construct in the grammar)
This is "annotated grammar"
Look into the compiler book by Aho, if you can find it.
William Bland wrote:
> 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.
- Next message: Albert Reiner: "si(x) in Maxima (Was: Mathematica vs. Lisp)"
- Previous message: Antony Blakey: "Re: OT: Recursive descent parsers"
- In reply to: William Bland: "OT: Recursive descent parsers"
- Next in thread: William D Clinger: "Re: OT: Recursive descent parsers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|