School of Engineering and Applied Science
Department of Computer Science
CSci 190 -- Real Time Computer Systems
http://www.seas.gwu.edu/~csci190
Prof. Michael B. Feldman
mfeldman@gwu.edu

Getting (Back) Up to Speed with Ada 95

One of your programming exercises in this course is a "sort race" in Ada 95. Since most students here either haven't done Ada 95 before, or studied it in course a while ago, here is a guide to some materials designed to help you out, in addition to the Ada material in Burns & Wellings.

Getting Started

The website http://www.seas.gwu.edu/~adagroup contains a wealth of information on Ada and the GNU Ada 95 (GNAT) compiler, including searchable versions of
  • Ada 95 Language Reference Manual
  • Ada 95 syntax charts
  • GNAT user guide
  • GNAT reference manual

Setting up to use GNU Ada 95 (GNAT)

Using GNAT requires setting up a few path variables and aliases. This is all handled by a shell script, which you invoke by typing
. ~csada/setup-gnat
Note the dot and the tilde. If you add this line to the .profile file in your home directory, it's executed automatically every time you log in.

WARNING: both GNAT and GNU C++ (g++) are members of the GNU open-source language family, but there are, unfortunately, some incompatibilities between their libraries. This makes it nearly impossible to use GNAT and g++ in the same login session. That's why we have a setup script for GNAT. So if you are also using g++ this semester, do NOT add the setup line to your .profile, but execute it "manually" when you want to use GNAT. See below on using GNAT with the course programs.

The Ada source code distribution

This resides in a zip archive on hobbes, ~csci190/adasource.zip. You can unzip it directly from the archive by typing
unzip -a ~csci190/adasource
This will produce a directory adasource, containing 3 subdirectories, each with a README.txt file:
  • handbook - all the source code from the handbook chapter
  • diners - Feldman's implementation of Dining Philosophers in Ada 95
  • sorting - generic sort routines and other supporting software that you can use as a basis for your sort race.

Using GNAT on hobbes with the CSci 190 programs

We assume that you've run the setup script, and unzipped the source programs.

There are two approaches:

  1. using the simple GW compilation, link, and execution scripts, gcompile, glink, and gexecute
  2. using the gnatmake program that comes with the compiler
The simple scripts allow you to compile a single source file at a time. The advantages of this are:
  • you know exactly what you're compiling
  • you get a listing file with interspersed error messages
  • you get an easy-to-find executable with an .exe file type
  • your code is set up to provide a traceback in case an unhandled exception is propagated beyond your main program.
The disadvantage is that you need to compile one file at a time.

The advantage of using gnatmake is that it works like an Ada-savvy make facility. You need no makefile; you just give the name of the main program, and it compiles and links everything it needs, automatically. But it's hard to get listings and the executable has no file extension at all. (File extensions are irrelevant in Unix.)

To compare, try this:

  1. Copy the file ~csci190/distance.adb into your home directory:

  2. cp ~csci190/distance.adb .
    This is a beginner's program that finds the distance traveled by a car.
  3. First build it with gnatmake:

  4. gnatmake -v distance
  5. and execute it

  6. distance
    first enter proper values when prompted, then execute it again and enter xyz or another nonnumeric value. The message indicates that Data_Error was propagated out of your program, but doesn't say how it happened.
  7. List the files in your directory

  8. ls -l (letter ell, not number 1!)
    The .o is your object code, the .ali is a dependency file (examine it - it's interesting!), and distance is the excutable. It's LARGE because the compiler is installed to do static linking of all the system and other libraries.
  9. Now use the scripts: compile it with gcompile:

  10. gcompile distance.adb
  11. Now bind and link it

  12. glink distance.ali
  13. Execute it:

  14. gexecute distance.exe
    first enter proper values when prompted, then run the program again and enter xyz or another nonnumeric value. Now the traceback shows you the line in your code where the exception was raised but not handled (in Java/C++ terminology, thrown but not caught).
  15. List the files in your directory

  16. ls -l (letter ell, not number 1!)
    note the .lsb file, a source listing, with numbered lines, etc. Examine the file in an editor.
  17. Now bring distance.adb up in an editor and doctor it up with a syntax error or two. Compile it again with gcompile, and examine the listing file.
Now try something in the program distribution:
  1. Move to the directory adasource/handbook
  2. List the directory to see what's there
  3. Use gcompile to compile rationals.ads, rationals.adb, rationals-io.ads, rationals-io.adb, and show_package.adb.
  4. Use glink to bind and link show_package.ali.
  5. Now execute show_package.exe with gexecute. Enter 1/2 and 3/4, and observe the result.
  6. Execute it again and enter a rational with a zero denominator. The helpful message was generated by this program, which handled (caught) the package-provided exception.
  7. Bring the file show_package.adb into an editor. Go to the bottom of the file, and comment out the 4 lines beginning with exception. This logically removes the exception handler.
  8. Compile, bind, and run the program again. Enter a bogus value and observe the traceback.
  9. If you know, or feel like learning, the GNU debugger, gdb, you can try it here.

Working on your own computer

Here are some things you can download:
  • a PDF file, handbook.pdf, containing Feldman's Ada 95 tutorial, Ada 95 in Context, which is adapted from his chapter in the 1998 Macmillan publication, Handbook of Programming Languages
  • 2 PDF files, generics.pdf, and concurrency.pdf, containing, respectively, Chapters 5, Generic Subprograms and Packages,and Chapter 16, Introduction to Concurrent Programming,from Feldman,Software Construction and Data Structures, published 1996 by Addison-Wesley. This chapter contains a lot of examples of generic templates in Ada 95, and will help you understand the software in the sorting directory (see below).
  • the software distribution, adasource.zip.
If you have a Windows or Linux (Redhat) PC and wish to install GNAT on it, you can download from the "official" public distribution site:
ftp://cs.nyu.edu/pub/gnat/3.15p

The Linux version is in this directory; for Windows, move down into winnt. Be sure to read the README before downloading; you do NOT need gnatwin unless you're interested in producing applications that use the Windows APIs. You do NOT need it for this course.

If you have a Mac running OS X and feel adventurous, there's a nice project that's bringing GNAT to OS X. Visit
http://www.adapower.com
for more information.

A very nice Windows-only GNAT-savvy multiwindow editor, AdaGIDE, can be downloaded from
http://www.usafa.af.mil/dfcs/bios/mcc_html/adagide.html

Another great Windows, Linux, and Mac (OS X) editor is jGRASP, which you can download from its home site at
http://www.eng.auburn.edu/grasp/

This editor is "smart" and "understands" the syntax of many languages, including both Ada 95 and Java.