![]() Previous |
![]() Contents |
![]() Next |
This appendix gives an informal summary of the syntax of Ada 95. Ive 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).
[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.
[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.
with list-of-library-units ; use list-of-packages ; -- packages in use clauses can't be generic use type list-of-types ;
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.
procedure name [( parameter-list )] ; function name [( parameter-list )] return type-name ;
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;
procedure name [( parameter-list )] is abstract; function name [( parameter-list )] return type-name is abstract;
Abstract subprograms must be primitive operations of abstract types.
package name is sequence-of-declarations [private sequence-of-declarations] end name ;
package body name is sequence-of-declarations [begin sequence-of-statements [exception sequence-of-exception-handlers]] end name ;
null;
variable-name := expression ;
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.
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
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).
[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.
exit [loop-name] ; -- unconditional exit [loop-name] when condition ; -- when condition is true
[loop-name :] while condition loop -- repeat while condition is true sequence-of-statements end loop [loop-name] ;
[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]
return; -- in procedures and accept statements return expression ; -- in functions; result is expression
[declare sequence-of-declarations] begin sequence-of-statements [exception sequence-of-exception-handlers] end;
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.
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).
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 ;
type name is type-specification ;
See A.4 below for more details about type declarations.
subtype name is type-name [range expression .. expressioni] ;
type name is range expression .. expression ;
type name is mod expression ;
type name is digits expression [range expression .. expression] ;
type name is delta expression range expression .. expression ; -- note that range is required
type name is delta expression digits expression [range expression .. expression] ;
type name is ( list-of-enumeration-literals ) ;
An enumeration-literal can be either a name or a character literal.
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
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>/I>]
type name is [[abstract] tagged] [limited] private; type name ( discriminant-part ) is [[abstract] tagged] [limited] private; type name (<>) is [[abstract] tagged] [limited] private;
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
type name ; type name ( discriminant-part ); type name (<>);
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;
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 a name is specified, it declares an object of the type Ada.Exceptions.Exception_Occurrence which is initialised with details of the exception that was raised.
list-of-names : exception;
raise [exception-name] ;
The exception-name can only be omitted inside an exception handler, in which case the original exception is reraised.
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
expression [not] in expression .. expression expression [not] in subtype-name
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
expression -- select a single array element expression .. expression -- select a slice (subarray) of an array
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 (|).
parent-value with record-aggregate parent-value with null record
generic [sequence-of-generic-parameters] subprogram-or-package-specification
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] ;
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 )] ;
list-of-names : [mode] type-name [:= default-value] ;
The mode must be in or in out. If it is omitted, in is assumed.
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
task [type] name [( list-of-discriminants )] ; task [type] name is sequence-of-entry-declarations [private sequence-of-entry-declarations] end name ;
entry name [( parameter-list )] ;
task body name is [sequence-of-declarations] begin sequence-of-statements [exception sequence-of-exception-handlers] end name ;
delay expression ; -- expression is of type Duration delay until expression ; -- expression is of type Ada.Calendar.Time
accept entry-name ; accept entry-name [( parameter-list )] do sequence-of-statements end entry-name ;
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.
[when condition =>] accept-statement [sequence-of-statements]
[when condition =>] delay-statement [sequence-of-statements]
[when condition =>] terminate;
A select statement may not contain more than one terminate alternative.
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.
select entry-call-or-delay-statement [sequence-of-statements] then abort sequence-of-statements end select;
abort list-of-task-names ;
protected [type] name [( list-of-discriminants )] is sequence-of-subprogram-or-entry-declarations [private sequence-of-declarations] end name ;
protected body name is sequence-of-subprogram-or-entry-bodies end name ;
entry name [( parameter-list )] when condition is [sequence-of-declarations] begin sequence-of-statements [exception sequence-of-exception-handlers] end name ;
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.