Subject: Re: Lex/Yacc
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 1 Jun 2001 02:19:38 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9f6u3q$cq975$1@fido.engr.sgi.com>
Manuel Giraud  <giraud@sor.inria.fr> wrote:
+---------------
| I'm in the first stage to create a small language and I want to know :
| 1. Is there something like Lex/Yacc for Scheme. I mean a lexer and a
|    parser that have a scheme-like syntax...
+---------------

Look in R5RS for the built-in procedure "read" <URL:http://www.schemers.org/
Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.6.2>:

	"Read" converts external representations of Scheme objects into
	the objects themselves. That is, it is a parser for the nonterminal
	<datum> (see sections 7.1.2 and 6.3.2). Read returns the next object
	parsable from the given input port, updating port to point to the
	first character past the end of the external representation of the
	object.

Since all Schemes (and Lisps) have "read" built-in, you don't need
a "lexer" per se for a Scheme-like syntax, or even a "parser" as far
as the nesting of objects goes (lines marked [*] typed manually):

	> (define foo (read))
[*]==>	(this is a list containing a #(vector of sumbols) and
[*]==>	"a string" and some numbers 1 2 3 ... and some sub-lists
[*]==>	(such as (these (these (and these too)))))
	> (pretty-print foo)
	(this
	 is
	 a
	 list
	 containing
	 a
	 #3(vector of sumbols)
	 and
	 "a string"
	 and
	 some
	 numbers
	 1
	 2
	 3
	 ...
	 and
	 some
	 sub-lists
	 (such as (these (these (and these too)))))
	> 

Now as far as attaching some semantics to the output of "read", that's
up to you, but most Schemes support some sort of pattern-matching library
(since it's needed internally to perform R5RS's hygenic macros), e.g.,
Andrew Wright's "match" <URL:http://www.cs.rice.edu/CS/PLT/packages/doc/
match/index.htm>, also available in MzScheme as the "match.ss" library
<URL:http://www.cs.rice.edu/CS/PLT/packages/doc/mzscheme/node175.htm>.

But for simple stuff, defining macros might be all you need...


-Rob

-----
Rob Warnock, 31-2-510		rpw3@sgi.com
SGI Network Engineering		<URL:http://reality.sgi.com/rpw3/>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA