Beruflich Dokumente
Kultur Dokumente
uk/corner/PicoProlog
PicoProlog
From Spivey's Corner
https://bitbucket.org/Spivey/pprolog,
together with a translator from the Pascal subset used by the interpreter into C. The interpreter source
itself uses macros to overcome some of the drawbacks of Pascal for this kind of systems programming,
and a simple macro processor also written in Pascal is provided also.
Building PicoProlog
You can fetch the interpreter code by cloning the Bitbucket repository with Mercurial.
$ hg clone https://bitbucket.org/Spivey/pprolog
you will see the following. First, the Pascal-to-C translator is built, using Bison and Flex to generate the
parser and lexer.
bison -d -o parse.c parse.y
gcc -g -O2 -c -o main.o main.c
gcc -g -O2 -c -o sem.o sem.c
gcc -g -O2 -c -o expr.o expr.c
gcc -g -O2 -c -o type.o type.c
gcc -g -O2 -c -o symtab.o symtab.c
gcc -g -O2 -c -o emit.o emit.c
gcc -g -O2 -c -o library.o library.c
gcc -g -O2 -c -o parse.o parse.c
flex -t scan.l >scan.c
gcc -g -O2 -c -o scan.o scan.c
gcc -g -O2 -o ptc main.o sem.o expr.o type.o symtab.o emit.o library.o parse.o scan.o
Now the translator is used on the source of the macro processor ppp.p, and the resulting C code is
compiled.
./ptc ppp.p >ppp.c
gcc -g -O2 -o ppp ppp.c
Finally, the macro processor is used on the source code of the Prolog interpreter, the resulting Pascal
code is translated to C, and the C code is compiled into a working interpreter.
(echo "define(ptc)"; cat pprolog.x) | ./ppp >pprolog.p
./ptc pprolog.p >pprolog.c
gcc -g -O2 -o pprolog pprolog.c
Instead of using the suppplied Pascal-to-C translator, it's possible to build picoProlog using the compiler
from the Free Pascal (http://www.freepascal.com) project. You will need the file pplib.inc containing
Free Pascal implementations of some system procedures. On Linux, you can just say
$ make fp-pprolog
Note that with version 3.0.0 of Free Pascal, the spurious message
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
appears a couple of times; it can safely be ignored. The result is called fp-pprolog so that it and the
standard version can coexist in the same Makefile, but can be renamed.
On other machines, it may be simpler to work manually. First, edit the file pprolog.x and add the single
line
define(fpc)
at the top. This will activate conditional code in the rest of the source that adapts it to Free Pascal. Then
issue the following commands.
$ fpc -oppp ppp.p
$ ppp <pprolog.x >pprolog.p
$ fpc -opprolog pprolog.p
About PTC
The Pascal-to-C translater PTC provided with PicoProlog implements only the subset of constructs that
are used in PicoProlog. Since that subset is more-or-less the intersection of Pascal and C, the job is not
too difficult. The following is a (no doubt incomplete) list of features of Pascal that aren't implemented:
Nested procedures
Procedures as parameters
Array parameters passed by value
Arrays indexed by other than integer ranges
Sets
Typed file I/O
Width specifications for write (they're ignored)
Some other Pascal features are implemented especially badly (in that errors in the Pascal source may
result in a bad C program as the output):