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
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.
csstud@hobbes:11: cd csci131then 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:
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 2000If 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 2000Finally, 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.exeLet'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 2000Oops! 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 22Oops! 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
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:33The file just-a-test.turnin can be printed out to turn it in, and will remain in your file system until you remove it.
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 2000Now 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/60Finally, 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!