Tutorial

General Information on
Ada 95 Language Structure

In order to program using Ada 95, you must have a compiler that will work under your computers
operating system and a general knowledge of Ada 95 language structure.  I used a Gnat compiler to test all of the code examples in this tutorial.
 

Compiler

One of the first things required to write a working program is a compiler.  Figure 1 is a picture
of a Gnat compiler.  The Gnat compiler is free and at the time this was written, can be
downloaded from the following URL:

http://www.acm.org/sigada/resources/sources.html#GNAT.

From this location you can choose from several different mirror sites to download a compiler for
a specific operating systems.  Once a compiler has been installed, programming can begin.  An
example of a simple program on the Gnat compiler is shown in figure 2.
 
 

Structure

There are certain rules to the proper structure for an Ada 95 program.  When using the Gnat
compiler, programmers will find that they are forced to follow these rules.
 
 


Figure 1
GNAT compiler






1.  The first line in the program starts with the key word with. This is followed by the names of
     the packages that are to be used by the program.  Each package name is separated by a
     comma.  The last package name has a semicolon after it.

2.  The second line in the program starts with the key word use. Figure 2 show an example of
     how the format looks in a compiler.  This is followed by the same names of the packages
     listed in the first line of the program.  Just as in line one, each package name is separated by
     a comma, and the last one has a semicolon after it.
 
 


Figure 2
Programming example
using the GNAT compiler






3.  The third line starts with the key word procedure.  The program name follows this key
     word.  When the file is saved, it must have the same as the program name.  If it
     does not have the same name, the compiler will give an error when told to compile the
     program.  The key word is follows the program name indicating to the compiler that
     declarations will follow.

4.  All of the lines between the key words is and begin are declarations.  Each type that is
     declared must be on its own line.  Several variables belonging to the same type may be
     declared on the same line as long as they are separated by commas.  The last variable is
     followed by a colon, then the type.  If the type is followed by a semi-colon, the variables
     are set equal to a null value.  The programmer can set a value in the declaration line by
     placing a colon followed by an equal sign after the type and before the value that is
     going to be set.  Figure 3 shows the format for setting a variable to a specific value.  If
     more than one variable is declared in a line that is being set to a value, all variables
     declared in that line are set to the same value.

5.  The key word begin follows the declaration lines. Begin designates the start of the
     statement lines.  The statement lines describe what the program does when it is
     executed.  Each statement line is ended with a semicolon.

6.  The key word end is used after the last statement line.  The program name follows the
     word end to help the programmer by designating where that procedure terminated.
 

Figure 3 shows the basic structure for a properly formatted program.
 
 

WITH TEXT_IO, BASIC_NUM_IO;
USE TEXT_IO, BASIC_NUM_IO;
PROCEDURE TEST IS
num1, num2: INTEGER := 9;
BEGIN
   PUT_LINE ("This is a test."); -- uses text_io
   PUT (num1); -- uses basic_num_io
   NEW_LINE; -- uses text_io
   PUT_ (num2); -- uses basic_num_io
   NEW_LINE; -- uses text_io
   PUT (num1+num2); -- uses basic_num_io
   NEW_LINE; -- uses text_io
END TEST;
Figure 3
Ada 95 format

The WITH line in Figure 3 could have been written as two seperate statements. The USE line in Figure 3 is not needed.  Figure 4 shows how this program could have been written with these changes.
 
 

WITH TEXT_IO;
WITH BASIC_NUM_IO;
PROCEDURE TEST IS
num1: INTEGER;
num2: INTEGER := 9;
BEGIN
TEXT_IO.PUT_LINE ("This is a test.");
BASIC_NUM_IO.PUT(num1);
TEXT_IO.NEW_LINE;
BASIC_NUM_IO.PUT (num2); 
TEXT_IO.NEW_LINE;
BASIC_NUM_IO.PUT (num1+num2); 
END TEST;
Figure 4  Ada 95 format -
Another method

Go to the Tutorial Table of Contents