I have a language defined in BNF form. For instance:
<rule1> = foo bar <rule2>
<rule2> = quux munge <rule3> | quux munge <rule4>
<rule3> = argl
<rule4> = bargl
I get a string, like "foo bar quux bargl", and I want to get from this
to something like "(<rule1> foo bar (<rule2> quux munge (<rule4>
bargl)))". There are no recursive rules --- all sentences are finite
length. Each sentence will have a unique parse. I believe this
implies the language is "regular" in CS terms, and could in theory be
recognized by a regular expression/finite state machine. Is there a
freely-available Lisp tool that makes this easy?
My sense is that CL-PPCRE is too "low-level", in that I don't think I
can specify patterns recursively. Something like META or ZEBU seem
like overkill in that they're designed to parse a larger class of
languages. Any thoughts?
Cheers,
rif
On 01 Sep 2004 18:20:13 -0400, rif <···@mit.edu> wrote:
>
>I have a language defined in BNF form. For instance:
>
><rule1> = foo bar <rule2>
><rule2> = quux munge <rule3> | quux munge <rule4>
><rule3> = argl
><rule4> = bargl
>
>I get a string, like "foo bar quux bargl", and I want to get from this
>to something like "(<rule1> foo bar (<rule2> quux munge (<rule4>
>bargl)))". There are no recursive rules --- all sentences are finite
>length. Each sentence will have a unique parse. I believe this
>implies the language is "regular" in CS terms, and could in theory be
>recognized by a regular expression/finite state machine. Is there a
>freely-available Lisp tool that makes this easy?
>
>My sense is that CL-PPCRE is too "low-level", in that I don't think I
>can specify patterns recursively. Something like META or ZEBU seem
>like overkill in that they're designed to parse a larger class of
>languages. Any thoughts?
>
>Cheers,
>
>rif
Look at combinatorial parsing.
The reply-to email address is ··········@yahoo.com.
This is an address I ignore.
To reply via email, remove 2002 and change yahoo to
interaccess,
**
Thaddeus L. Olczyk, PhD
There is a difference between
*thinking* you know something,
and *knowing* you know something.
rif <···@mit.edu> writes:
> I have a language defined in BNF form. For instance:
>
> <rule1> = foo bar <rule2>
> <rule2> = quux munge <rule3> | quux munge <rule4>
> <rule3> = argl
> <rule4> = bargl
>
> I get a string, like "foo bar quux bargl", and I want to get from this
> to something like "(<rule1> foo bar (<rule2> quux munge (<rule4>
> bargl)))". There are no recursive rules --- all sentences are finite
Personally I don't find zebu overkill for this task. The
foo-domain.lisp file created from the below example is 588 bytes, the
foo.tab file is 1251 bytes. The zebu grammar descriptions maps pretty
well into your BNF:
foo.zb
(:name "foo"
:grammar "null-grammar"
:package "foo")
(defrule RULE1
:= ("foo" "bar" RULE2)
:build (list '<rule1> 'foo 'bar RULE2))
(defrule RULE2
:= ("quux" "munge" RULE3)
:build (list '<rule2> 'quux 'munge RULE3)
:= ("quux" "munge" RULE4)
:build (list '<rule2> 'quux 'munge RULE4))
(defrule RULE3
:= ("argl")
:build (list '<rule3> 'argl))
(defrule RULE4
:= ("bargl")
:build (list '<rule4> 'bargl))
foo.cl
(unless (find-package "ZEBU")
(asdf:operate 'asdf:load-op :zebu-compiler))
(unless (find-package "foo")
(make-package "foo"))
(defparameter *load-dirname* (pathname-directory *load-truename*))
(zb:zebu-compile-file
(make-pathname :name "foo" :type "zb" :directory *load-dirname*))
(zb:zebu-load-file
(make-pathname :name "foo" :type "tab" :directory *load-dirname*))
(princ (zb:read-parser "foo bar quux munge bargl" :grammar (zb:find-grammar "foo")))
Loading this results in:
(<RULE1> FOO BAR (<RULE2> QUUX MUNGE (<RULE4> BARGL)))
Petter
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?