Re: Lex, Yacc, and Lisp...
- From: Sven-Olof Nystr|m <svenolof@xxxxxxxxxxxxxx>
- Date: Sat, 24 Sep 2005 10:47:34 +0200
Ray Dillinger <bear@xxxxxxxxx> writes:
> I had a random thought the other day. Lisp could easily
> be the "back end" or "executable intermediate representation"
> for most language compilers.
>
> Consider this: some student in a CS course designs a language.
> He uses flex or lex to create a lexer, yacc or bison to parse
> the tokens into a syntax tree according to a grammar, and
> then does a traversal of the syntax tree where each node is
> preceded by an open-paren and the name of the node type (from
> the grammar), and followed by a close-paren.
Why not write the compiler in Lisp? There are parser generator
available. Besides, writing a recursive descent parser in Lisp is
straight-forward.
> The output of this traversal can be considered as a lisp
> expression. The final step in implementing the student's
> language is to implement lisp definitions (probably macros)
> for each grammar node type that perform the semantic action
> of that node type according to the language design.
Sounds like an elegant solution. However, doing a simple translation
from a tree-structured intermediate language to a lower level tends to
be pretty straight-forward. Using macros would allow you to avoid the
explicit case analysis, but the case analysis is the easy part, so you
wouldn't gain much.
> This seems like an easier method of implementing most
> languages, and Lisp, with its syntactic flexibility, is
> uniquely even more suited to being a "universal" target
> metalanguage than, say, the JVM or .NET to name a couple
> of the usual suspects.
>From a practical point of view, using Lisp as a backend has many
advantages. However, if we are talking about a compilers course and
one of the goals of the course is to teach how machine code is
generated, you would miss out on that.
> Have I missed important things?
You haven't explained why you want to do this :-)
> What would be hard to do with this approach?
If the goal is to implement an experimental language as easily as
possible, but with reasonable performance, then I think that a
translation to Lisp would be the easiest route. This is of course
under the assumption that the libraries you need may be accessed from
Lisp. (And if interfacing with the libraries is hard, then that might
be one of the main obstacles.)
Sven-Olof
--
Sven-Olof Nystrom
Comp Sci Dept, Uppsala University, Box 337, S-751 05 Uppsala SWEDEN
svenolof@xxxxxxxxx, http://www.csd.uu.se/~svenolof
phone: +46 18 471 76 91, fax: +46 18 51 19 25
.
- Follow-Ups:
- Re: Lex, Yacc, and Lisp...
- From: Peter Seibel
- Re: Lex, Yacc, and Lisp...
- From: Ray Dillinger
- Re: Lex, Yacc, and Lisp...
- Prev by Date: Re: Preventing GC stalls
- Next by Date: Common Lisp flet
- Previous by thread: Preventing GC stalls
- Next by thread: Re: Lex, Yacc, and Lisp...
- Index(es):
Relevant Pages
|