[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Zebu and SableCC (was Re: Simple SableCC Example)

"Etienne M. Gagnon" wrote:
> Andrew Cooke wrote:
> > So, for the record, the URL is
> > http://www.andrewcooke.free-online.co.uk/jara/egg/index.html
> > The code is pretty old, but no-one has told me that it does not work
> > with recent SableCC versions.
> Is there any problem with the current SableCC?  I'd like to know if I
> have introduced any non-backward compatible change.

If anyone complains, I'll let you know!

> > (Using, these days, a similar program written in Lisp, called Zebu -
> > it's interesting to see how the two languages influence the API...)
> Do you have a short summary of the differences?

I am no expert on this, and it is a long time since I used SableCC, so
forgive me if I use the wrong terms or make stupid mistakes...

For those who have not used the language, Lisp types are strong and
dynamic (Java tries to be strong and static, although it fails slightly,
which is why you can have class cast exceptions).  Although Lisp has
sophistcated OO aspects these are "optional" - unlike Java, it is
possible to write programs without using classes or methods.  Also, the
difference between code and text is not as clear as in Java (for
example, the routine that reads a file is also the routine that reads

So, how do those differences make Zebu and SableCC different?  Looking
at an old SableCC grammar file, to refresh my memory:

- in both you can define regexps to help in the inital lexing, although
Zebu's regexp isn't very complete (something I'm working on).

- when building the tree, Zebu can work in two ways:

1. at its simplest, you can specify what action is taken for each
production.  This can be arbitrary Lisp code, but defaults support
creating structures with named slots.  The nearest equivalent in Java
would be classes with public instance variables.

2. alternatively, the grammar can be pre-processed (via Zebu, using a
pre-defined grammar).  This restricts you to structures with slots - the
advantage is that the mapping is reversible, and the equivalent of
toString methods are generated for the structures, so that the AST will
"print itself" (ie the "toString methods" recurse through sub

- there is no tree-walking code provided in Zebu, but a separate
package, called Zebu-RR  lets you define transforms as a
collection of rules.  The rules are applied to the tree (either top-down
or bottom-up) to modify the structure.

So how different are they?

Although structures are used in Zebu rather than classes, the structures
still have a hierarchy (sub structures include the slots defined for
super-structures).  So the main difference between the AST generated by
the two programs is that SableCC has stronger data encapsulation - it is
very difficult to *enforce* encapsulation in Lisp, and apparently there
is little tradition of trying.

Both Zebu and Zebu-RR are more flexible, in that arbitrary code can be
specified and executed.  This reflects the weak division between code
and data in Lisp - even if SableCC wanted to include arbitrary code for
productions in the grammar file (and I presume it does not!), it would
be difficult to program in Java.

So generally, Zebu is more flexible, but SableCC is much safer.  I would
say that reflects the two languages and the different design aims. 
SableCC, as I understand it, is intended to produce reliable results. 
Zebu, I gather, was intended for explorative work in translating formal
languages (hence Zebu-RR, with re-writing rules, and the support for
self-printing code).  

Because they have such different aims, I don't think either has much to
learn from the other, although a version of Zebu-RR for SableCC would be
interesting - but it's difficult to see how to do the equivalent in Java
(that's the price you pay for a more secure language).  Or maybe the 
Java equivalent is the tree-walking support included in SableCC.

I hope that helps.  Zebu is LALR(1) (I think SableCC is too?).  

If anyone is interested I can dig out locations for Zebu and Zebu-RR (at
the moment Zebu is unsupported - I am planning to release a new version
with better regular expression support some time early next year, but
only because no-one else is doing so.  The latest version I know of is