School of Engineering and Applied Science
Department of Computer Science
CSci 131 -- Algorithms and Data Structures I
Prof. Michael B. Feldman
mfeldman@seas.gwu.edu
Using the GNU Ada 95 Translator (GNAT) in CSci 131
January 2000
http://www.seas.gwu.edu/~csci131/gnat-handout-131.html

Using GNAT with the GW Scripts
Copying Programs
Compiling, Linking, and Executing Programs
Creating a Script File to Turn In
Compiling Packages with GNAT



This handout will explain how to use the GNU Ada 95 Translator (GNAT) on the School of Engineering's hobbes computer. We assume you have been introduced to Unix on this machine, and also that you have completed project #0 in this class. This handout depends upon the CSci 131 setup, and will not work if you have not completed project #0 .

All commands you must enter are given in this typeface. The computer's responses are shown in this typeface. You should go to a terminal, log in to hobbes, and follow this handout, step-by-step.


Using GNAT with the GW Scripts

When you run the class setup, you gain access to a set of easy-to-use command scripts for working with GNAT. Once you really become familiar with the compiler, you can start using GNAT's real commands, which are more flexible but require more detailed knowledge. This handout describes only the simplified scripts.


Copying Programs.

All programs from the Feldman book Software Consdtruction and Data Structures with Ada 95, used in this course, are located in the programs131 directory. All programs from the Feldman/Koffman book used in CSci 51, and some others, are located in the programs51 directory. You gain access o both direcories when you run the class setup. These programs are filed according to the GNAT file-naming rule, which prefers a program unit MyProg to be stored in a file myprog.adb. You can copy programs from this subdirectory, but can't store anything into it; it belongs to the instructor. We'll see how to copy a program into your current directory, which Unix calls .(yes, it's just a dot). First log in to hobbes. The CSci 131 setup has created a directory called csci131. Try changing to that directory,
csstud@hobbes:11: cd csci131
then trying:
csstud@hobbes:12: cp programs51/hello.adb . 
csstud@hobbes:13: cat hello.adb
WITH Ada.Text_IO;
PROCEDURE Hello IS
   
BEGIN -- Hello
  
  Ada.Text_IO.Put (Item => "Hello there. ");
  Ada.Text_IO.Put (Item => "We hope you enjoy studying Ada!");
  Ada.Text_IO.New_Line;
  
END Hello;
csstud@hobbes:14:


Compiling, Linking, and Executing Programs

Your next step is to try a compilation.
csstud@hobbes:14: gcompile hello.adb
Compilation of hello.adb started on  Tue Jan 18 15:22:46 EST 2000
Compilation ended   on  Tue Jan 18 15:22:53 EST 2000
If there were compilation errors, a message to that effect appears as well. The compiler produces a "listing file" which gives an "official" record of the compilation. If there were compilation errors, the messages occur interspersed with the lines of Ada.

You can see this file using cat or more; you can also print a hard (paper) copy of it in the usual way. Let's look at the listing file from our compilation:

csstud@hobbes:15: cat hello.lsb
 
GNAT 3.12p1 (19990629) Copyright 1991-1999 Free Software Foundation, Inc.
 
Compiling: hello.adb (source file time stamp: 1996-01-23 03:03:19)
 
     1. WITH Ada.Text_IO;
     2. PROCEDURE Hello IS
     3.    
     4. BEGIN -- Hello
     5.   
     6.   Ada.Text_IO.Put (Item => "Hello there. ");
     7.   Ada.Text_IO.Put (Item => "We hope you enjoy studying Ada!");
     8.   Ada.Text_IO.New_Line;
     9.   
    10. END Hello;


The next step is to link the program. The linker script requires a file with an .ali file type.

csstud@hobbes:16: glink hello.ali
Linking of hello.ali started on  Tue Jan 18 15:23:11 EST 2000
Linking ended   on  Tue Jan 18 15:23:15 EST 2000
Finally, we execute the program. While neither GNAT nor Unix requires a particular file type for executables, our scripts give them an .exe file type, and so should you.
csstud@hobbes:17: gexecute hello.exe
Hello there. We hope you enjoy studying Ada!
You can execute a program any number of times without re-compiling.
csstud@hobbes:18: gexecute hello.exe
Hello there. We hope you enjoy studying Ada!
You can use Unix commands to see which program units you've compiled and linked.
csstud@hobbes:19: ls -l *.ali
-rw-r--r--   1 csstud user         727 Jan 18 15:22 hello.ali
csstud@hobbes:20: ls -l hello.exe
-rwxr-xr-x   1 csstud user      119556 Jan 18 15:23 hello.exe
Let's try another program. We'll compile, link, and execute it, and then enter some "bad" data (alphabetic instead of integer). Note the exception "traceback" telling you in which line of your program the exception was raised.
csstud@hobbes:21: gcompile distance.adb
Compilation of distance.adb started on  Tue Jan 18 15:29:10 EST 2000
gcc: distance.adb: No such file or directory
gcc: No input files
Compilation ended   on  Tue Jan 18 15:29:10 EST 2000
Oops! We forgot to copy the program!
csstud@hobbes:22: cp programs51/distance.adb .
csstud@hobbes:23: gcompile distance.adb
Compilation of distance.adb started on  Tue Jan 18 15:29:29 EST 2000
Compilation ended   on  Tue Jan 18 15:29:36 EST 2000
csstud@hobbes:24: glink distance.ali
Linking of distance.ali started on  Tue Jan 18 15:29:47 EST 2000
Linking ended   on  Tue Jan 18 15:29:53 EST 2000
csstud@hobbes:25: gexecute distance.exe
How many hours will you be driving (integer) ? XYZ
 
raised ADA.IO_EXCEPTIONS.DATA_ERROR
Trace Back Information
    Program Name               File Name                 Line
    ------------               ---------                 ----
    distance                   distance.adb              22
Oops! We entered XYZ instead of a numerical value! Running the program again with correct data input will give a reasonable answer.
csstud@hobbes:26: gexecute distance.exe
How many hours will you be driving (integer) ? 5
At what average speed (miles per hour, integer) ? 55
You will travel about         275 miles


Creating a Script File to Turn In

We require that you submit a "script" of your compilation, link, and test execution(s). The turnin command will allow you to create a transcript of everything you do on the terminal. One you have started turnin, everything that you type or the computer displays, until you type exit, will be copied into your script file. You can then print the file to submit as part of your project. As an example, let's try copying a program hello_name.adb, then compiling, linking, and executing it with the script running.
csstud@hobbes:27: cp programs51/hello_name.adb .
csstud@hobbes:28: turnin
Project name ? just-a-test
Turnin started; type exit to leave turnin.
Script started, file is just-a-test.turnin
script> csstud@hobbes:29: gcompile hello_name.adb
Compilation of hello_name.adb started on  Tue Jan 18 15:33:26 EST 2000
Compilation ended   on  Tue Jan 18 15:33:29 EST 2000
script> csstud@hobbes:30: glink hello_name.ali
Linking of hello_name.ali started on  Tue Jan 18 15:33:37 EST 2000
Linking ended   on  Tue Jan 18 15:33:40 EST 2000
script> csstud@hobbes:31: gexecute hello_name.exe
Enter your first name, exactly 10 letters.
Add spaces at the end if it's shorter.> Jennifer  
 
Hello Jennifer  .  We hope you enjoy studying Ada!
script> csstud@hobbes:32: exit
Script done, file is just-a-test.turnin
csstud@hobbes:33
The file just-a-test.turnin can be printed out to turn it in, and will remain in your file system until you remove it.

Compiling Packages with GNAT

Ada is a language whose proper use depends heavily on the package concept. An ordinary package consists of two files: a specification (sometimes called declaration) and a body (sometimes called implementation). Both must be compiled. GNAT requires that a specification have filetype .ads and a body have filetype .adb. Compiling a specification only checks it for syntactic and semantic correctness, and produces a listing file; code is generated when the body is compiled. To see how to compile and use packages, try this series of compilations of programs from Chapter 2 of the data structures text.

The packages Rationals and Rationals.IO provide for operations on rational numbers (fractions). Note the GNAT-preferred file names: Rationals is in rationals.ads and rationals.adb; Rationals.IO is in rationals-io.ads and rationals-io.adb. Specifically, the dot in the child package name becomes a hyphen in the file name!

csstud@hobbes:25: cp programs131/*rat*.ad* .
csstud@hobbes:26: ls -l *rat*
-rw-r--r--   1 csstud user        1125 Jan 18 14:10 rationals-io.adb
-rw-r--r--   1 csstud user         985 Jan 18 14:10 rationals-io.ads
-rw-r--r--   1 csstud user        3061 Jan 18 14:10 rationals.adb
-rw-r--r--   1 csstud user        2247 Jan 18 14:10 rationals.ads
-rw-r--r--   1 csstud user        1237 Jan 18 14:10 test_rationals_1.adb
-rw-r--r--   1 csstud user        1191 Jan 18 14:10 test_rationals_2.adb
-rw-r--r--   1 csstud user        1574 Jan 18 14:10 test_rationals_3.adb
csstud@hobbes:27: gcompile rationals.ads
Compilation of rationals.ads started on  Tue Jan 18 14:11:05 EST 2000
Compilation ended   on  Tue Jan 18 14:11:08 EST 2000
csstud@hobbes:28: gcompile rationals.adb
Compilation of rationals.adb started on  Tue Jan 18 14:13:19 EST 2000
Compilation ended   on  Tue Jan 18 14:13:23 EST 2000
csstud@hobbes:29: gcompile rationals-io.ads
Compilation of rationals-io.ads started on  Tue Jan 18 14:14:20 EST 2000
Compilation ended   on  Tue Jan 18 14:14:22 EST 2000
csstud@hobbes:30: gcompile rationals-io.adb
Compilation of rationals-io.adb started on  Tue Jan 18 14:24:12 EST 2000
Compilation ended   on  Tue Jan 18 14:24:15 EST 2000
Now we compile, link and execute test_rationals_1, a program that demonstrates the rationals.
csstud@hobbes:31: gcompile test_rationals_1.adb
Compilation of testrat1.adb started on  Tue Jan 18 14:26:45 EST 2000
Compilation ended   on  Tue Jan 18 14:26:47 EST 2000
csstud@hobbes:32: glink test_rationals_1.ali
Linking of test_rationals_1.ali started on  Tue Jan 18 14:26:55 EST 2000
Linking ended   on  Tue Jan 18 14:27:19 EST 2000
csstud@hobbes:33: gexecute test_rationals_1.exe
A = 1/3
B = -1/2
Enter rational number C > 3/6
Enter rational number D > 1/5
 
E = A + B is -1/6
F = C * D is 1/10
A + E * F is 19/60
 








Finally, an important feature of Ada is exceptions. An exception can be system- or programmer-defined, and can be handled within a program. As an exercise, execute test_rationals_1 again, but this time enter 1/0 at the prompt. This will illustrate what happens if an Ada exception is raised, but not handled somewhere in your program. For examples of programs that do handle the exception, try compiling, linking, and running test_rationals_2.adb.

Enjoy CSci 131 and Ada 95 and GNAT!