anagram_1.0.0_49233f56/tests/ts_00022/ts_00022.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
--  Check GLR parser

with Ada.Text_IO;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Wide_Text_IO;

with AST; use AST;

with Anagram.Grammars;
with Anagram.Grammars.Constructors;
with Anagram.Grammars_Convertors;
with Anagram.Grammars_Debug;
with Anagram.Grammars.Reader;
with Anagram.Grammars.LR_Tables;
with Anagram.Grammars.LR.LALR;
with Anagram.Grammars.RNGLR;
with Anagram.Grammars.Lexers;

procedure TS_00022 is

   procedure Print_Action
     (Table : Anagram.Grammars.LR_Tables.Table;
      State : Anagram.Grammars.LR.State_Index;
      T     : Anagram.Grammars.Terminal_Count);

   package Node_Lists is new Ada.Containers.Doubly_Linked_Lists
     (AST.Node_Access);

   procedure Print_Tree
     (Printed : in out Node_Lists.List;
      Tree    : AST.Node_Access;
      Input   : Anagram.Grammars.Grammar;
      Prefix  : Wide_Wide_String := "");

   package RNGLR is new Anagram.Grammars.RNGLR
     (AST.Node_Access, null, AST.Node_Fabric);

   type Lexer is new Anagram.Grammars.Lexers.Lexer with null record;
   function Next (Self : in out Lexer) return Anagram.Grammars.Terminal_Count;

   Last : Natural := 0;
   --  Token list for "2 * 2 + 2"
   List : constant
     array (Positive range <>) of Anagram.Grammars.Terminal_Count :=
     (1,   --  int
      3,   --  star
      1,   --  int
      2,   --  plus
      1,   --  int
      0);  --  EOF

   ----------
   -- Next --
   ----------

   function Next (Self : in out Lexer)
     return Anagram.Grammars.Terminal_Count
   is
      pragma Unreferenced (Self);
   begin
      Last := Last + 1;
      return List (Last);
   end Next;

   ------------------
   -- Print_Action --
   ------------------

   procedure Print_Action
     (Table : Anagram.Grammars.LR_Tables.Table;
      State : Anagram.Grammars.LR.State_Index;
      T     : Anagram.Grammars.Terminal_Count)
   is
      use Anagram.Grammars.LR_Tables;
      use type Anagram.Grammars.Terminal_Count;
      use type Anagram.Grammars.LR.State_Count;

      S : constant Anagram.Grammars.LR.State_Count :=
        Shift (Table, State, T);
      R : Reduce_Iterator := Reduce (Table, State, T);
   begin
      Ada.Text_IO.Put (' ');

      if T = 0  and then Finish (Table, State) then
         Ada.Text_IO.Put ("FINISH   ");
      end if;

      if S /= 0 then
         Ada.Text_IO.Put ("SHIFT ");
         Ada.Text_IO.Put (Anagram.Grammars.LR.State_Count'Image (S));

         if S in 1 .. 9 then
            Ada.Text_IO.Put (' ');
         end if;
      end if;

      if not Is_Empty (R) then
         Ada.Text_IO.Put ("REDU");
         while not Is_Empty (R) loop
            Ada.Text_IO.Put
              (Anagram.Grammars.Production_Index'Image (Production (R)));

            Ada.Text_IO.Put
              (Anagram.Grammars.Part_Index'Image (Part (R)));

            Next (Table, R);
         end loop;
      else
         Ada.Text_IO.Put ("Error    ");
      end if;
   end Print_Action;

   ----------------
   -- Print_Tree --
   ----------------

   procedure Print_Tree
     (Printed : in out Node_Lists.List;
      Tree    : AST.Node_Access;
      Input   : Anagram.Grammars.Grammar;
      Prefix  : Wide_Wide_String := "")
   is
      Node : constant AST.Node := Tree.all;
   begin
      if Printed.Contains (Tree) then
         return;
      else
         Printed.Append (Tree);
      end if;

      AST.Print (Node, Input);
      Ada.Wide_Text_IO.Put (" [");
      for Child of Node.Children loop
         if Child /= null then
            AST.Print (Child.all, Input);
         end if;
      end loop;
      Ada.Wide_Text_IO.Put_Line (" ]");

      for Child of Node.Children loop
         if Child /= null then
            Print_Tree (Printed, Child, Input, Prefix & "  ");
         end if;
      end loop;
   end Print_Tree;

   Fabric : aliased AST.Node_Fabric;

   X : constant Anagram.Grammars.Grammar :=
     Anagram.Grammars.Reader.Read ("test.ag");
   G : constant Anagram.Grammars.Grammar :=
     Anagram.Grammars_Convertors.Convert_With_Empty (X);
   AG : constant Anagram.Grammars.Grammar :=
     Anagram.Grammars.Constructors.To_Augmented (G);
   use Anagram.Grammars;
begin
--   Anagram.Grammars.AYACC.Write (Plain);
   Anagram.Grammars_Debug.Print (AG);
   Ada.Text_IO.Put ("Terminals" & Terminal_Count'Image (G.Last_Terminal));
   Ada.Text_IO.Put
     (" Non_Terminals" & Non_Terminal_Count'Image (G.Last_Non_Terminal));
   Ada.Text_IO.Put
     (" Production" & Production_Count'Image (G.Last_Production));
   Ada.Text_IO.Put_Line (" Parts" & Part_Count'Image (G.Last_Part));

   declare
      use Anagram.Grammars.LR;

      Table   : LR_Tables.Table_Access := LALR.Build (AG, True);
      Tree    : AST.Node_Access;
      Printed : Node_Lists.List;
      L       : Lexer;
   begin
      Ada.Text_IO.Put_Line
        ("Last_State=" & State_Index'Image (LR_Tables.Last_State (Table.all)));

      Anagram.Grammars_Debug.Print_Conflicts (AG, Table.all);

      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put_Line ("Table:");

      for S in 1 .. LR_Tables.Last_State (Table.all) loop
         Ada.Text_IO.Put (State_Index'Image (S));

         if S <= 9 then
            Ada.Text_IO.Put (' ');
         end if;

         for T in 0 .. AG.Last_Terminal loop
            Print_Action (Table.all, S, T);
         end loop;

         Ada.Text_IO.Put (ASCII.HT);

         for NT in 1 .. AG.Last_Non_Terminal loop
            Ada.Text_IO.Put
              (State_Count'Image (LR_Tables.Shift (Table.all, S, NT)));
         end loop;

         Ada.Text_IO.New_Line;
      end loop;

      RNGLR.Parse
        (G => AG, T => Table.all, L => L, F => Fabric'Access, Tree => Tree);

      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put_Line ("Print Tree:");
      Print_Tree (Printed, Tree, G);
      AST.Dereference (Fabric'Access, Tree);

      LR_Tables.Free (Table);
   end;
end TS_00022;