×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Syntax or codeing error in ada

Syntax or codeing error in ada

Syntax or codeing error in ada

(OP)
Ok so I am tring to merge two files file1 and file2 into file3 the output file. these files are a presorted list of numbers. There is some problmes when I compile it and I cant figue out how to fix this in ada java ok but ada is hard for me for some reason. :(
Thanks

-- Program that merges two sorted files using concurrent tasks

with Ada.Text_IO; use Ada.Text_IO;
procedure Project3 is
  package Int_IO is new Integer_IO(Integer);
  use Int_IO;

  -- Protected object and tasks specifications

task Merge_Task is
 entry Submit_First(Value: in Integer);
  entry Submit_Second(Value: in Integer);
end Merge_Task;
task Input_Task_1;
task Input_Task_2;
task Output_Task is
  entry Smaller(Smaller_Value: in Integer);
end Output_Task;


  -- Merge_Protected is a protected object that provides
  -- the interface between the input tasks and the output task.

  task body Merge_Task is
    entry Submit_First(Value: in Integer)
      when not First_Present is
    begin
      First_Value := Value;
      First_Present := True;
    end Submit_First;
    entry Submit_Second(Value: in Integer)
      when not Second_Present is
    begin
      Second_Value := Value;
      Second_Present := True;
    end Submit_Second;
    entry Extract_Smaller(Value: out Integer)
      when First_Present and Second_Present is
    begin
      if First_Value < Second_Value then
        Value := First_Value;
        First_Present := False;
      else
        Value := Second_Value;
        Second_Present := False;
      end if;
    end Extract_Smaller;
  end Merge_Task;

  -- Input_Task_1 reads values from the first input file and sends
  -- them to Merge_Protected.

  task body Input_Task_1 is
    Value: Integer;
    File_1: File_Type;
  begin
    Open(File_1, In_File, "file1.txt");
    while not End_Of_File(File_1) loop
      Get(File_1, Value);
      Merge_Protected.Submit_First(Value);
    end loop;
    Merge_Protected.Submit_First(Integer'Last);
    Close(File_1);
  end Input_Task_1;

  -- Input_Task_2 reads values from the second input file and sends
  -- them to Merge_Protected.

  task body Input_Task_2 is
      Value: Integer;
      File_2: File_Type;
   begin
      Open(File_2, In_File, "file2.txt");
      while not End_Of_File(File_2) loop
         Get(File_2, Value);
         Merge_Protected.Submit_Second(Value);
      end loop;

     Merge_Protected.Submit_Second(Integer'Last);
     Close(File_2);
    -- You must supply the body of this task

  end Input_Task_2;

  -- Output_Task obtains values from Merge_Protected and writes
  -- them to the output file.

  task body Output_Task is
    File: File_Type;
    Value: Integer;
  begin
    Create(File, Out_File, "file3.txt");
    loop
      Merge_Protected.Extract_Smaller(Value);
      exit when Value = Integer'Last;
      Put(File, Value);
      New_Line(File);
    end loop;
    Close(File);
  end Output_Task;
begin
  null;
end Project3;

 

RE: Syntax or codeing error in ada

Trying to correct the syntax errors I lightly modifed your source:
merge.adb

CODE

-- Program that merges two sorted files using concurrent tasks

with Ada.Text_IO; use Ada.Text_IO;
procedure merge is
  package Int_IO is new Integer_IO(Integer);
  use Int_IO;

  -- Protected object and tasks specifications

task Merge_Protected is
  entry Submit_First(Value: in Integer);
  entry Submit_Second(Value: in Integer);
  entry Extract_Smaller(Value: out Integer);
end Merge_Protected;
task Input_Task_1;
task Input_Task_2;
task Output_Task;
--task Output_Task is
--  entry Smaller(Smaller_Value: in Integer);
--end Output_Task;


  -- Merge_Protected is a protected object that provides
  -- the interface between the input tasks and the output task.

  task body Merge_Protected is
    First_Value, Second_Value : Integer;
    First_Present, Second_Present : Boolean;
  begin  
    accept Submit_First(Value: in Integer) do
      First_Value := Value;
      First_Present := True;
    end Submit_First;
    accept Submit_Second(Value: in Integer) do
      Second_Value := Value;
      Second_Present := True;
    end Submit_Second;
    accept Extract_Smaller(Value: out Integer) do
      Put("--- Extract_Smaller ---"); New_Line(1);
      Put("First_Value = "); Put(First_Value, 1); New_Line(1);
      Put("Second_Value = "); Put(Second_Value, 1); New_Line(1);
      if First_Value < Second_Value then
        Value := First_Value;
        First_Present := False;
      else
        Value := Second_Value;
        Second_Present := False;
      end if;
      Put("Value = "); Put(Value, 1); New_Line(1);
      Put("-----------------------"); New_Line(1);
    end Extract_Smaller;
  end Merge_Protected;

  -- Input_Task_1 reads values from the first input file and sends
  -- them to Merge_Protected.

  task body Input_Task_1 is
    Value: Integer;
    File_1: File_Type;
  begin
    Open(File_1, In_File, "file1.txt");
    while not End_Of_File(File_1) loop
      Get(File_1, Value);
      Put("File 1 = "); Put(Value, 1); New_Line(1);
      Merge_Protected.Submit_First(Value);
    end loop;
    Merge_Protected.Submit_First(Integer'Last);
    Close(File_1);
  end Input_Task_1;

  -- Input_Task_2 reads values from the second input file and sends
  -- them to Merge_Protected.

  task body Input_Task_2 is
    Value: Integer;
    File_2: File_Type;
  begin
    Open(File_2, In_File, "file2.txt");
    while not End_Of_File(File_2) loop
      Get(File_2, Value);
      Put("File 2 = "); Put(Value, 1); New_Line(1);         
      Merge_Protected.Submit_Second(Value);
    end loop;
    Merge_Protected.Submit_Second(Integer'Last);
    Close(File_2);
    -- You must supply the body of this task
  end Input_Task_2;

  -- Output_Task obtains values from Merge_Protected and writes
  -- them to the output file.

  task body Output_Task is
    File: File_Type;
    Value: Integer;
  begin
    Create(File, Out_File, "file3.txt");
    loop
      Merge_Protected.Extract_Smaller(Value);
      Put("Smaller Value = "); Put(Value, 1); New_Line(1);  
      exit when Value = Integer'Last;
      Put(File, Value);
      New_Line(File);
    end loop;
    Close(File);
  end Output_Task;

begin
  null;
end merge;
It compiles and runs now:

CODE

$ gnat make merge.adb
gcc -c merge.adb
gnatbind -x merge.ali
gnatlink merge.ali
But it doesn' work as expected:
When I have two files
file1.txt

CODE

1 5 7 9
and
file2.txt

CODE

2 4 6 8
then the output is

CODE

$ merge
File 1 = 1
File 2 = 2
--- Extract_Smaller ---
First_Value = 1
Second_Value = 2
Value = 1
-----------------------
File 1 = 5
File 2 = 4
Smaller Value = 1
and the resulting file file3.txt contains only

CODE

          1
It seems, that the program:
1) reads only the first two numbers from every input file
2) calls the Merge_Protected.Extract_Smaller(Value) only once on the first numbers from input files.

I have no idea, how to solve the merge problem with tasks and concurrent programming in Ada, so I would be glad if you post here the improved solution.
 

RE: Syntax or codeing error in ada

(OP)
Ha yes that help out a bit I am having trouble geting the syntax down for ada. I have been the the local public lib and also tried going online with little results for ada no helpful tutorials that I can find what a drag. I think I might know what wrong though. I think the code is looking for the smallest value and not tring to merge the two files togeather, thats what I get for splices three different code examples togeather :) I will try to look up some recursive code in ada to see if I can get it to go through multiple times.
Thanks again and I will try to get this code working in the next couple a days and repost it.

RE: Syntax or codeing error in ada

Here I found interesting article about concurrent programming in Ada:
http://www.adaic.org/whyada/intro5.html
But nevertheless, I have no idea how to use it to solve the merge problem: You can independently read in one task one sorted file into an array and in other task other sorted file into another array and then merge both sorted arrays into a resulting  file, but I have no idea how to do it without arrays smile
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close