[antlr-interest] Anyone has a LISP ANTLR3 grammar and can con tribute it to Drools?

Stuart Watt SWatt at infobal.com
Thu Jan 24 06:40:53 PST 2008


Yes, CL is definitely the way to go.

However, with Lisp, the easy stuff is:

expression =
  '(' expression* ('.' expression)? ')'
  | symbol
  | number

which is more or less the whole "natural" syntax of Lisp - it was designed
to be trivial parse on 1950s hardware!

The language Lisp then uses "special forms" which define the semantics.
These include examples like (if (= a b) (print c)), where "if" the
conditional. However, special forms are not syntactic, really. 

However, you very quickly get into additional stuff which is done by
"readtables", which define associations between characters, and which can
strictly (in Common Lisp) be manipulated at run-time, or even at compile
time. These extensions are mostly simple, but a few (such as backquote) are
not. Essentially, you can associate a character with a function then then
gets called as part of the reader, which is what takes text and turns it
into a Lisp internal representation. 

In ANTLR terms, these character associations can recursively call the parser
(or even the raw input stream) to do other stuff. Comments, for example, can
be handled by the read-table, both the regular ";" and the balanced "#| ....
|#" type.  "#" is an extension to the read-table for all sorts of nasty
stuff, and uses the second character to invoke a function (i.e., readtables
can be cascaded). For example, "#'" is a readtable macro that reads a Lisp
expression (as above) and conveniently wraps the (FUNCTION ...) special form
around it, just as "'" is a convenience for (QUOTE ...). ANTLR can do all
this stuff nicely with ASTs, if you put all these characters in the lexer,
and then match them at the left of the expression rule. 

i.e., pick a subset of CL, or another Lisp, and when you have it, the
grammar will probably be trivial. Don't even think about trying to do all of
CL in ANTLR (or any other parser) - it was designed in a way that allows
Lisp to take control of the input process at certain stages; ideal for
higher-level language processing. You probably don't need some of the
oddities of CL, like some of the rarer # readtable macros, not that they are
hard to do.

This is the issue with Lisp - there is a standard, but it would be
inappropriate to implement much of it in ANTLR as it is largely dynamic and
intimately connected with the core language; yet subsets may vary depending
on context of use. 

All the best
Stuart

-----Original Message-----
From: Ola Bini [mailto:ola.bini at gmail.com]
Sent: Thursday, January 24, 2008 8:52 AM
To: Edson Tirelli
Cc: antlr-interest at antlr.org; Mark Proctor
Subject: Re: [antlr-interest] Anyone has a LISP ANTLR3 grammar and can
contribute it to Drools?


Edson Tirelli wrote:
>
>     All,
>
>    We need a Lisp ANTLR3 grammar for the Drools open source project 
> (http://labs.jboss.com/drools/). We were going to build one from 
> scratch, but I was wondering if anyone has one ready and can 
> contribute to the project. All proper credits would be given, as 
> usual. Project's license is ASF.
>    Alternatively, an (E)BNF Lisp grammar that we can use as a base to 
> build an ANTLR3 would also be helpful. Our main worry is how to define 
> the function name token for Lisp, since lisp accepts almost anything 
> as a function name.
>
Actually, you could make it really easy for yourself and make a lisp 
that doesn't take everything as a function name. The easiest you could 
do is probably just use the CL conventions and parse everything as 
atoms, separated by spaces. Spaces in atom names can be escaped with \. 
You can also have an atom name inside bars: |foo bar|, and then strings. 
That's really all you need.

Cheers

-- 
 Ola Bini (http://ola-bini.blogspot.com) 
 JRuby Core Developer
 Developer, ThoughtWorks Studios (http://studios.thoughtworks.com)
 Practical JRuby on Rails (http://apress.com/book/view/9781590598818)

 "Yields falsehood when quined" yields falsehood when quined.



More information about the antlr-interest mailing list