A compilation unit contains either the declaration or the body of a program unit, preceded by the necessary context clause (`with' or `use' clauses). Thus a compilation unit can be a package declaration, a package body, a subprogram declaration, or a subprogram body along with its context clause.
An Ada compiler only compiles collections of one or more compilation units. That's why it's important to understand compilation units - to compile something, it must be a part of a legal compilation unit.
Here's a simplified BNF syntax for a compilation unit. The full definition is in section 10.1 of the RM:
Note that compilation units start with "with" and "use" clauses, followed by a program unit declaration or body. We've already seen three compilation units - the simplified package declaration for Text_IO and two versions of the "Hello" program (in two different forms). Let's go back and look at the "Hello2" program to see that it really is a valid compilation unit:
-- Print a simple message to demonstrate a trivial Ada program. with Ada.Text_IO; use Ada.Text_IO; -- use clause - automatically search Ada.Text_IO. procedure Hello2 is begin Put_Line("Hello, world!"); -- Note: No longer has "Ada.Text_IO" in front. end Hello2;
Ada compilers will only compile a compilation_unit, and according to the BNF definition above a compilation_unit is a context_clause followed by a library_item. The BNF also says that a context_clause is a set of zero or more context_items, and a context_item is either a "with" clause or "use" clause. Since Hello2 starts off with a "with" clause and a "use" clause, we have two context_items that make up the context_clause. I haven't shown the definition of a library_item, but it turns out that the rest of Hello2 is a valid library_item, so we have a valid compilation unit.
Although most Ada compilers permit multiple compilation units in a single file, it's usually better to put separate compilation units in separate files. One Ada compiler (GNAT) requires different compilation units to be in different files.
Informally, when people say `show me X's package declaration' they really mean `show me the compilation unit that includes the package declaration of package X'.
Which of the following cannot be part of a compilation unit after the context clause?
![]() |
![]() |
![]() |
---|
David A. Wheeler (dwheeler@dwheeler.com)
The master copy of this file is at
"http://www.adahome.com/Tutorials/Lovelace/s2s4.htm".