Prev Next Up Home Keys Figs Search New

Pretty Printer

Appeared in Volume 6/3, August 1993

Keywords: printing.

baier@forwiss.uni-passau.de
Joern Baier
11th September 1992

I'm looking for a pretty printer which should read source code and write it out in a nicer format. It should be able to deal with comments and retain variable names.

alufml@purple.fnma.com
Francois-Michel Lang
11th September 1992

A couple of years ago, when I was a heavy-duty Quintus Prolog user, Dave Bowen sent me a C program ppl.c, written by Richard O'Keefe, that does exactly what you want. I used it quite a lot.

csa09@seq1.keele.ac.uk
Paul Singleton
11th September 1992

I want a pretty printer which will allow me to implement my own preferred layout of source code. I think this requires a full parser which keeps variable names and the whitespace (and comments) which are adjacent to each token. I'd be thrilled if someone put one in the public domain.

I think I could make Prolog source code more readable by using different fonts, or font variations (bold, italic etc.), and by using subscripts for related variables, e.g. p(Cs0, Cs1). In fact I am fed up with using ASCII for my programs when I can use lots of goodies to make my other writings more readable.

So in future I shall compose my Prolog source files using FrameMaker or some such. As long as all I/O (including that of the debugger) is user-configurable, I should be able to preserve the illusion of a Prolog system which converses in a script much richer than ASCII. Any takers?

mcovingt@athena.cs.uga.edu
Michael A. Covington
11th September 1992

What we need, as Paul Singleton says, is a parser for Prolog which preserves white space, or at least comments.

I propose: boldface for functors; roman for variable names; Courier for quoted strings ('this' and "this"); italics for comments. Suitable output formats might include: LaTeX; Word Perfect without headers.

Does someone have an appropriate parser? It should be a very minor modification of an ordinary Prolog parser.

spratt@hawk.cs.ukans.edu
Lindsey Spratt
11th September 1992

It is difficult to handle comments "formally" in a parser for Prolog (or most any language), since the comments can appear anywhere whitespace can, yet have some semantic correspondence to the other syntactic element which the parser must maintain. The output formatting of comments can be quite tricky as well. Thus, I found a minor modification of an existing grammar to be inadequate and built a DCTG grammar from scratch. This system allows one to specify the formatting approach desired for handling the various operators (another tricky area), and it (optionally) reformats the comments into "canonical" locations with appropriately indented text. The target of this formatting system was ASCII files, but other kinds of output could easily be added. Another issue here is that using a word processing system for final formatting can be a problem if the word processing system is going to choose the places to introduce new lines, and the way to indent the "new" line. Both of these decisions should be made in a syntax-sensitive fashion which the word processing system is unlikely to be able to do (except, perhaps, for the text of comments).

popx@vax.oxford.ac.uk
Jocelyn Paine
11th September 1992

I don't have a complete pretty printer. However, the code that comes with my "LP Tutor" includes some of what you need to build one. There's a set of predicates (sdo.pl and sdo_output.pl) which read the grammar of a language in a notation that's like DCGs augmented with formatting commands and generate Prolog predicates from it. Portability note: you'll need to be able to 'write' into lists or other structures.

There's also a tokeniser and parser, built from Richard O'Keefe's in the Dec-10 library. This can read variable names. It ignores comments, but you could make it capture them and return them hooked onto the term in some way. I'm not sure what the best way to represent a term augmented in this way would be.

You can FTP the code from:
ftp://src.doc.ic.ac.uk/packages/prolog-pd-software/tutor.tar.Z

Prev Next Up Home Keys Figs Search New