Question

Basically, I have 2 files ( .adb and .ads). I am totally new to Ada and also how to compile 2 files. The program is of a basic stack implementation. I got this compile error when I compiled the .adb file.

$ gcc -c test_adt_stack.adb
abstract_char_stack.ads:22:01: end of file expected, file can have only one compilation unit

The 2 files I have are: abstract_char_stack.ads

-----------------------------------------------------------
package Abstract_Char_Stack is
  type Stack_Type is private;
  procedure Push(Stack : in out Stack_Type;
                 Item  : in Character);
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character);
private
  type Space_Type is array(1..8) of Character;
  type Stack_Type is record
    Space : Space_Type;
    Index : Natural := 0;
  end record;
end Abstract_Char_Stack;
-----------------------------------------------------------
package body Abstract_Char_Stack is
----------------------------------------------
  procedure Push(Stack : in out Stack_Type;
                  Item : in Character) is
  begin
    Stack.Index := Stack.Index + 1;
    Stack.Space(Stack.Index) := Item;
  end Push;
--------------------------------------------
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character) is
  begin
    Char := Stack.Space(Stack.Index);
    Stack.Index := Stack.Index - 1;
  end Pop;
--------------------------------------------
end Abstract_Char_Stack;

and the other one is test_adt_stack.adb

-----------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Abstract_Char_Stack; use Abstract_Char_Stack;
procedure Test_ADT_Stack is
  S1 : Stack_Type;
  S2 : Stack_Type;
  Ch : Character;
begin
  Push(S1,'H'); Push(S1,'E');  
  Push(S1,'L'); Push(S1,'L');
  Push(S1,'O');                          -- S1 holds O,L,L,E,H

  for I in 1..5 loop
    Pop(S1, Ch);  
    Put(Ch);                             -- displays OLLEH
    Push(S2,Ch); 
  end loop;                              -- S2 holds H,E,L,L,O

  New_Line;
  Put_Line("Order is reversed");

  for I in 1..5 loop
    Pop(S2, Ch);
    Put(Ch);                             -- displays HELLO
  end loop;

end Test_ADT_Stack;
-----------------------------------------------------------

What am I doing wrong? I just want to have it compile and display what it's supposed to do. This was a study the program kind of assignment. But I can't make it compile or don't know if I am doing it right.

Was it helpful?

Solution

The problem is that GNAT [and FSF GNAT is what GCC uses, IIRC] does not allow multiple compilation-units in a single file. (This is due to how they manage the library, but that is perhaps a bit too detailed for a beginner to worry about.)

Solution, each of these needs its own file:

  • Abstract_Char_Stack Specification (abstract_char_stack.ads)
  • Abstract_Char_Stack Body (abstract_char_stack.adb)
  • Test_ADT_Stack [procedure] body (test_adt_stack.adb)

OTHER TIPS

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
-----------------------------------------------------------

procedure Test_ADT_Stack is

    enter code here

package Abstract_Char_Stack is
  type Stack_Type is private;
  procedure Push(Stack : in out Stack_Type;
                 Item  : in Character);
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character);
private
  type Space_Type is array(1..8) of Character;
  type Stack_Type is record
    Space : Space_Type;
    Index : Natural := 0;
  end record;
  end Abstract_Char_Stack;

   use Test_ADT_Stack.Abstract_Char_Stack ;
    S1 : Stack_Type;
    S2 : Stack_Type;
    Ch : Character;

-----------------------------------------------------------
package body Abstract_Char_Stack is
----------------------------------------------
  procedure Push(Stack : in out Stack_Type;
                  Item : in Character) is
  begin
    Stack.Index := Stack.Index + 1;
    Stack.Space(Stack.Index) := Item;
  end Push;
--------------------------------------------
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character) is
  begin
    Char := Stack.Space(Stack.Index);
    Stack.Index := Stack.Index - 1;
  end Pop;
--------------------------------------------
end Abstract_Char_Stack;


-----------------------------------------------------------
begin
  Push(S1,'H'); Push(S1,'E');  
  Push(S1,'L'); Push(S1,'L');
  Push(S1,'O');                          -- S1 holds O,L,L,E,H

  for I in 1..5 loop
    Pop(S1, Ch);  
    Put(Ch);                             -- displays OLLEH
    Push(S2,Ch); 
  end loop;                              -- S2 holds H,E,L,L,O

  New_Line;
  Put_Line("Order is reversed");

  for I in 1..5 loop
    Pop(S2, Ch);
    Put(Ch);                             -- displays HELLO
  end loop;

end Test_ADT_Stack;
-----------------------------------------------------------
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top