My company is designing a new domain specific scripting language; I have to implement a parser that translates our brand new programming language into a common scripting language so as to be able to enact it.
The usual way I do this is by means of Bison
and Flex
tools that generate the C/C++
code of the translator.
I found other tools, for most of the mainstream programming languages, but none for Lisp
.
Hasn't Lisp
ever been used for that? What is the usual way to write a parser with Lisp
?
Note: to me, any Lisp
implementation / dialect that could help is ok, I do not have any preference.
To cover the Racket part of it:
People often write parsers and there are many ways to do so:
There are two ways to parse non-lispy languages in common-lisp.
1) Use readtables. this is the classic way: the lisp reader algorithm is a simple recursive-decent parser already, which supports character-based dispatch. Vacietis does this here
2) Use a parsing library. I can recommend esrap as a good utility for doing packrat parsing, and smug as a decent one for doing monadic parsing. Both are available in quicklisp
Well, "the usual" way to do this in Common Lisp is … to do it in Lisp.
A lot of domain-specific languages (and Lisp is pretty much notoriously specialized for this purpose!) are simply written as extensions to Lisp itself, using the macro facility. The upside is, it's trivial to write a DSL. The downside is, they often tend to "look like" lisp.
Some examples of DSL's within the Common Lisp standard include the
LOOP
macro's own sub-language and the sub-language of theFORMAT
specifiers.Since Lisp's s-expression notation is nominally a written form of an Abstract Syntax Tree, it's one way to avoid having much of your own lexer or parser; you can just use
READ
.That all being said, you can use some common packages that might be found in
GRAYLEX
orCL-LEXER
or so forth; looking at the parsers for some other language with a similar syntax to yours might help. In Quicklisp, I see: