9.2. The Structure Of Pascal

Most texts for Pascal include a BNF or “railroad-track” definition of the language. Here are the first few lines of one:

     <program> ::= <program-header> <block> '.'
     <program-header> ::= PROGRAM <ident>
     <block> ::= <declarations> <statements>

We can write recognizers to deal with each of these elements, just as we've done before. For each one, we'll use our familiar single-character tokens to represent the input, then flesh things out a little at a time. Let's begin with the first recognizer: the program itself.

To translate this, we'll start with a fresh copy of the Cradle. Since we're back to single-character names, we'll just use a 'p' to stand for 'PROGRAM.'

To a fresh copy of the cradle, add the following code, and insert a call to it from the main program:

{ Parse and Translate A Program }
procedure Prog;
var  Name: char;
   Match('p');            { Handles program header part }
   Name := GetName;

The procedures Prolog and Epilog perform whatever is required to let the program interface with the operating system, so that it can execute as a program. Needless to say, this part will be very OS-dependent. Remember, I've been emitting code for a 68000 running under the OS I use, which is SK*DOS. I realize most of you are using PC's and would rather see something else, but I'm in this thing too deep to change now!

Anyhow, SK*DOS is a particularly easy OS to interface to. Here is the code for Prolog and Epilog:

{ Write the Prolog }
procedure Prolog;
   EmitLn('WARMST EQU $A01E');

{ Write the Epilog }
procedure Epilog(Name: char);
   EmitLn('DC WARMST');
   EmitLn('END ' + Name);

As usual, add this code and try out the “compiler.” At this point, there is only one legal input:



where x is any single letter, the program name

Well, as usual our first effort is rather unimpressive, but by now I'm sure you know that things will get more interesting. There is one important thing to note: THE OUTPUT IS A WORKING, COMPLETE, AND EXECUTABLE PROGRAM (at least after it's assembled).

This is very important. The nice feature of the top-down approach is that at any stage you can compile a subset of the complete language and get a program that will run on the target machine. From here on, then, we need only add features by fleshing out the language constructs. It's all very similar to what we've been doing all along, except that we're approaching it from the other end.