Previous

Contents

Next

Appendix A: Language summary


A.1 Compilation units
A.2 Statements
A.3 Declarations
A.4 Type declarations
A.5 Exceptions
A.6 Expressions
A.7 Generics
A.8 Multitasking features
A.9 The Ada type hierarchy

This appendix gives an informal summary of the syntax of Ada 95. I’ve deliberately tried to keep it informal to make it easier to understand at a glance, but because of its informality it is incomplete and not everything is spelled out in full. You should consult the Ada 95 Reference Manual if you need a more detailed syntax of the language.

In the descriptions below, items in italics refer to syntactic categories, items in bold must be entered as shown and items enclosed in square brackets [like this] are optional and may be omitted. Unless otherwise noted, sequence-of-X means one or more Xs (so sequence-of-statements means one or more statements) while list-of-X means one or more Xs separated by commas (so list-of-names means one or more names separated by commas).


A.1 Compilation units

A.1.1 Compilation units (chapter 2, chapter 4)

    [sequence-of-context-clauses]
    [private] package-or-subprogram-declaration

The package-or-subprogram-declaration can be a package specification, a package body, a procedure declaration or a function declaration. Only child packages and child subprograms can be specified as private.

A.1.2 Separate units (chapter 4)

    [sequence-of-context-clauses]
    separate ( parent-unit-name )
    body-declaration

The body-declaration can be the body of a subprogram, package, task or protected record.

A.1.3 Context clauses (chapter 2, chapter 9)

    with list-of-library-units ;
    use list-of-packages ;     -- packages in use clauses can't be generic
    use type list-of-types ;

A.1.4 Subprograms (chapter 2, chapter 4)

    procedure name [( parameter-list )] is   -- see below for parameter-list
        [sequence-of-declarations]
    begin
        sequence-of-statements
    [exception
        sequence-of-exception-handlers]
    end name ;

    function name [( parameter-list )] return type-name is
        [sequence-of-declarations]
    begin
        sequence-of-statements    -- must include at least one return statement
    [exception
        sequence-of-exception-handlers]
    end name ;

A parameter-list consists of one or more parameter declarations separated by semicolons. Each parameter declaration looks like this:

    list-of-names : [mode] type-name [:= default-value]

For a procedure, mode is either in, out, in out or access. For a function, mode is either in or access. If it is omitted, in is assumed.

A.1.5 Subprogram specifications (chapter 2, chapter 4)

    procedure name [( parameter-list )] ;
    function name [( parameter-list )] return type-name ;

A.1.6 Separate subunits (chapter 4, chapter 10)

    procedure name [( parameter-list )] is separate;
    function name [( parameter-list )] return type-name is separate;
    package body name is separate;
    task body name is separate;
    protected body name is separate;

A.1.7 Abstract subprograms (chapter 15)

    procedure name [( parameter-list )] is abstract;
    function name [( parameter-list )] return type-name is abstract;

Abstract subprograms must be primitive operations of abstract types.

A.1.8 Package specifications (chapter 4, chapter 9)

    package name is
        sequence-of-declarations
    [private
        sequence-of-declarations]
    end name ;

A.1.9 Package bodies (chapter 4, chapter 9)

    package body name is
        sequence-of-declarations
    [begin
        sequence-of-statements
    [exception
        sequence-of-exception-handlers]]
    end name ;

A.2 Statements

A.2.1 The null statement (chapter 3)

    null;

A.2.2 Assignment statements (chapter 3)

    variable-name := expression ;

A.2.3 Procedure call statements (chapter 2)

    procedure-name [( list-of-parameters )] ;

Each parameter in the list-of-parameters takes the following form:

    [parameter-name =>] expression

Named parameters must come after any parameters which do not specify a name.

A.2.4 If statements (chapter 3)

    if condition then
        sequence-of-statements
    [sequence-of-elsif-parts]
    [else
        sequence-of-statements]
    end if;

An elsif-part looks like this:

    elsif condition then
        sequence-of-statements

A.2.5 Case statements (chapter 3)

    case expression is
        sequence-of-alternatives
    end case;

Each alternative in the sequence-of-alternatives looks like this:

    when choice-list =>
        sequence-of-statements
    when others =>          -- if present, must come last
        sequence-of-statements

A choice-list is a list of one or more choices separated by vertical bars (‘|’). Choices can take either of the following forms:

    expression                -- execute this choice if the controlling
                              -- expression is equal to expression
    expression .. expression  -- execute this choice if the controlling
                              -- expression is in the specified range

The expressions must be able to be evaluated at compile time. There must be a choice for every possible value of the type of the controlling expression (or an others choice).

A.2.6 Loop statements (chapter 3)

    [loop-name :]               -- if present, must be given after end loop
    loop
        sequence-of-statements  -- exit statement required!
    end loop [loop-name] ;

If a loop-name is specified at the beginning of the loop, it must also be repeated after end loop.

A.2.7 Exit statements (chapter 3)

    exit [loop-name] ;                  -- unconditional
    exit [loop-name] when condition ;   -- when condition is true

A.2.8 While loops (chapter 3)

    [loop-name :]
    while condition loop                -- repeat while condition is true
        sequence-of-statements
    end loop [loop-name] ;

A.2.9 For loops (chapter 6)

    [loop-name :]
    for name in [reverse] subtype-specification loop
        sequence-of-statements
    end loop [loop-name] ;

A subtype-specification takes either of the following forms:

    expression .. expression        -- treated as a subtype of Integer
    type-name [range expression .. expression]

A.2.10 Return statements (chapter 4)

    return;                         -- in procedures and accept statements
    return expression ;             -- in functions; result is expression

A.2.11 Blocks (chapter 3, chapter 4, chapter 7)

    [declare
        sequence-of-declarations]
    begin
        sequence-of-statements
    [exception
        sequence-of-exception-handlers]
    end;

A.3 Declarations

A.3.1 Object declarations (chapter 2, chapter 3)

    list-of-names : [aliased] [constant] type-name [:= initial-value] ;
    list-of-names : [aliased] [constant] array ( list-of-index-subtypes ) of type-name [:= initial-value] ;

The initial-value is any expression of the appropriate type and is assigned to all the variables defined in the list-of-names.

A.3.2 Named numbers (chapter 4)

    list-of-names : constant := numeric-value ;

The numeric-value is any expression with a real or integer value which must be static (i.e. it must be able to be evaluated at compile time). The list-of-names will be defined as universal values (universal real or universal integer).

A.3.3 Renaming declarations (chapter 4, chapter 7)

    name : type-name renames object-name ;
    name : exception renames exception-name ;
    [generic] package name renames package-name ;
    [generic] procedure name [( parameter-list )] renames procedure-name ;
    [generic] function name [( parameter-list )] return type-name renames function-name ;

A.3.4 Type declarations (chapter 4)

    type name is type-specification ;

See A.4 below for more details about type declarations.

A.3.5 Subtype declarations (chapter 4)

    subtype name is type-name [range expression .. expression] ;

A.4 Type declarations

A.4.1 Signed integer types (chapter 5)

    type name is range expression .. expression ;

A.4.2 Modular integer types (chapter 5)

    type name is mod expression ;

A.4.3 Floating point types (chapter 5)

    type name is digits expression [range expression .. expression] ;

A.4.4 Fixed point types (chapter 5)

    type name is delta expression range expression .. expression ;
                                  -- note that range is required

A.4.5 Decimal types (chapter 5)

    type name is delta expression digits expression [range expression .. expression] ;

A.4.6 Enumeration types (chapter 5)

    type name is ( list-of-enumeration-literals ) ;

An enumeration-literal can be either a name or a character literal.

A.4.7 Record types (chapter 6, chapter 14)

    type name [( discriminant-part )] is
        [[abstract] tagged] [limited] record
            sequence-of-component-declarations
        end record;

A sequence-of-component-declarations which does not contain any components must be specified as null.

    type name [( discriminant-part )] is
        [[abstract] tagged] [limited] null record;

    type name ( discriminant-part ) is
        [[abstract] tagged] [limited] record
            [sequence-of-component-declarations]
            case discriminant-name is
                variant-part
            end case;
        end record;

A component-declaration looks like an object declaration (but may not be a constant or an anonymous array).

A discriminant-part has the same format as the parameter list in a subprogram declaration except that the mode must be either access or omitted, and the type of a discriminant must be either a discrete type or an access type.

A variant-part looks like this:

    when choice-list =>
        sequence-of-component-declarations

A.4.8 Array types (chapter 6)

    type name is array ( list-of-index-subtypes ) of type-name ;

An index-subtype takes either of the following forms:

    expression .. expression        -- treated as a subtype of Integer
    type-name [range expression .. expression]

A.4.9 Private types (chapter 9)

    type name is [[abstract] tagged] [limited] private;
    type name ( discriminant-part ) is [[abstract] tagged] [limited] private;
    type name (<>) is [[abstract] tagged] [limited] private;

A.4.10 Access types (chapter 11)

    type name is access name ;            -- pool-specific access type
    type name is access all name ;        -- general access type
    type name is access constant name ;   -- access-to-constant type

A.4.11 Incomplete declarations (chapter 11)

    type name ;
    type name ( discriminant-part );
    type name (<>);

A.4.12 Derived types (chapter 5, chapter 14)

    type name is new type-name [range expression .. expression];
    type name is [abstract] new tagged-type-name with private;
    type name is [abstract] new tagged-type-name with null record;
    type name is [abstract] new tagged-type-name with
        record
            sequence-of-components
        end record;

A.5 Exceptions

A.5.1 Exception handlers (chapter 3, chapter 7)

    when [name :] exception-list =>
        sequence-of-statements
    when [name :] others =>
        sequence-of-statements

The exception-list is a list of one or more exception names separated by bars (‘|’). If there is an others choice it must come last. If there is a name specified, it declares an object of the type Ada.Exceptions.Exception_Occurrence which is initialised with details of the exception that was raised.

A.5.2 Exception declarations (chapter 7)

    list-of-names : exception;

A.5.3 Raise statements (chapter 7)

    raise [exception-name] ;

The exception-name can only be omitted inside an exception handler, in which case the original exception is reraised.


A.6 Expressions

A.6.1 Expressions (chapter 2, chapter 3)

An expression is a sequence of one or more terms separated by operators. The operators are applied to the operands (highest-priority operators first) to yield a value of a specific type.

Terms within an expression can be object names or literal values, or can take any of the following forms:

    unary-operator expression                 -- unary subexpression
    [type-name '] ( expression )              -- subexpression
    [type-name '] ( aggregate )               -- aggregate
    type-name ( expression )                  -- type conversion
    type-name ' attribute-name
                  [( list-of-parameters )]    -- type attribute
    new type-name ['( initial-value )]        -- storage allocator
    array-name ( list-of-subscripts )         -- array element/slice
    record-name . component-name              -- record component
    function-name [( list-of-parameters )]    -- function call

A.6.2 Membership operators (chapter 5)

    expression [not] in expression .. expression
    expression [not] in subtype-name

A.6.3 Array aggregates (chapter 6)

An array aggregate is a comma-separated list of one or more values for specifying the components of an array. Values in an array aggregate take the following form:

    [component-selector-list =>] expression

A component-selector-list consists of one or more component selectors separated by vertical bars (‘|’). Component selectors are as follows:

    expression
    expression .. expression
    others                -- must be used by itself as the last selector

A.6.4 Subscripts (chapter 6)

    expression                      -- select a single array element
    expression .. expression        -- select a slice (subarray) of an array

A.6.5 Record aggregates (chapter 6)

A record aggregate is either a comma-separated list of one or more values for specifying the components of a record, or null record. Values in an aggregate other than a null record take the following form:

    [component-selector-list =>] expression

A component-selector-list consists of one or more component names separated by vertical bars (‘|’).

A.6.6 Extension aggregates (chapter 14)

    parent-value with record-aggregate
    parent-value with null record

A.7 Generics

A.7.1 Generic units (chapter 12)

    generic
        [sequence-of-generic-parameters]
    subprogram-or-package-specification

A.7.2 Generic type parameters (chapter 12)

    type name is [[abstract] tagged] [limited] private;
    type name is (<>);
    type name is range <>;
    type name is mod <>;
    type name is digits <>;
    type name is delta <>;
    type name is delta <> digits <>;
    type name is access [all] type-name ;
    type name is access constant type-name ;
    type name is array ( type-name [range <>] ) of type-name ;
    type name is [abstract] new type-name [with private] ;

A.7.3 Generic subprogram and package parameters (chapter 12)

    with procedure procedure-specification [is <>] ;
    with procedure procedure-specification is name ;
    with function function-specification [is <>] ;
    with function function-specification is name ;
    with package name is new generic-package-name [(<>)] ;
    with package name is new generic-package-name
                        [( list-of-generic-parameters )] ;

A.7.4 Generic object parameters (chapter 12)

    list-of-names : [mode] type-name [:= default-value] ;

The mode must be in or in out. If it is omitted, in is assumed.

A.7.5 Generic instantiations (chapter 5, chapter 12)

    package name is new generic-package-name
                                    [( list-of-generic-parameters )] ;
    procedure name is new generic-procedure-name
                                    [( list-of-generic-parameters )] ;
    function name is new generic-function-name
                                    [( list-of-generic-parameters )] ;

Generic parameters have the following form:

    [parameter-name =>] value

A.8 Multitasking features

A.8.1 Task specifications (chapter 19)

    task [type] name [( list-of-discriminants )] ;

    task [type] name is
        sequence-of-entry-declarations
    [private
        sequence-of-entry-declarations]
    end name ;

A.8.2 Entry declarations (chapter 19)

    entry name [( parameter-list )] ;

A.8.3 Task bodies (chapter 19)

    task body name is
        [sequence-of-declarations]
    begin
        sequence-of-statements
    [exception
        sequence-of-exception-handlers]
    end name ;

A.8.4 Delay statements (chapter 19)

    delay expression ;              -- expression is of type Duration
    delay until expression ;        -- expression is of type Ada.Calendar.Time

A.8.5 Accept statements (chapter 19)

    accept entry-name ;

    accept entry-name [( parameter-list )] do
        sequence-of-statements
    end entry-name ;

A.8.6 Selective accept statements (chapter 19)

    select
        sequence-of-accept-alternatives
    [or
        delay-or-terminate-alternatives]
    [else
        sequence-of-statements]
    end select;

You cannot have both a delay-or-terminate-alternative and an else part. The sequence-of-accept-alternatives consists of one or more accept-alternatives separated from each other by or. The delay-or-terminate-alternatives consist of either a single terminate alternative, or one or more delay alternatives separated by or.

A.8.7 Accept alternatives (chapter 19)

    [when condition =>]
        accept-statement
        [sequence-of-statements]

A.8.8 Delay alternatives (chapter 19)

    [when condition =>]
        delay-statement
        [sequence-of-statements]

A.8.9 Terminate alternatives (chapter 19)

    [when condition =>]
        terminate;

A select statement may not contain more than one terminate alternative.

A.8.10 Selective entry calls (chapter 19)

    select
        entry-call
        [sequence-of-statements]
    [or
        delay-alternative]
    [else
        sequence-of-statements]
    end select;

You can have either a delay-alternative or an else part, but not both.

A.8.11 Abortable select statements (chapter 19)

    select
        entry-call-or-delay-statement
        [sequence-of-statements]
    then abort
        sequence-of-statements
    end select;

A.8.12 Abort statements (chapter 19)

    abort list-of-task-names ;

A.8.13 Protected record specifications (chapter 19)

    protected [type] name [( list-of-discriminants )] is
        sequence-of-subprogram-or-entry-declarations
    [private
        sequence-of-declarations]
    end name ;

A.8.14 Protected record bodies (chapter 19)

    protected body name is
        sequence-of-subprogram-or-entry-bodies
    end name ;

A.8.15 Entry body (chapter 19)

    entry name [( parameter-list )] when condition is
        [sequence-of-declarations]
    begin
        sequence-of-statements
    [exception
        sequence-of-exception-handlers]
    end name ;

A.9 The Ada type hierarchy

    Elementary types
        - scalar types
            - discrete types
               - enumerations
               - integers                           \
                  - signed integers                 |
                  - modular integers                |
               - real types                         |    numeric types
                  - floating point                  |
                  - fixed point                     |
                     - ordinary fixed point         |
                     - decimal fixed point          /
        - access types
           - access-to-object
           - access-to-subprogram

    Composite types
        - array types
        - record types
           - untagged records
           - tagged records
        - tasks
        - protected records



Previous

Contents

Next

This file is part of Ada 95: The Craft of Object-Oriented Programming by John English.
Copyright © John English 2000. All rights reserved.
Permission is given to redistribute this work for non-profit educational use only, provided that all the constituent files are distributed without change.
$Revision: 1.2 $
$Date: 2002/02/22 01:47:17 $