Newbie Generic Reg Exp Pattern Matching Question 
Author Message
 Newbie Generic Reg Exp Pattern Matching Question

I'm having trouble getting a generic Regular Expression Matching.
This code fragment is taken out of Booch's Software Components book.

       23  generic
       24     type ITEM is private;
       25     type INDEX is (<>);
       26     type ITEMS is array (INDEX range <>) of ITEM;
       27     Any_Item       : in ITEM;
       28     Escape_Item    : in ITEM;
       29     Not_Item       : in ITEM;
       30     Closure_Item   : in ITEM;
       31     Start_Class    : in ITEM;
       32     Stop_Class     : in ITEM;
       33     with function "=" (  Left  : in ITEM;
       34                          Right : in ITEM) return Boolean;
       **
    *****E equality parameters must be same limited type [LRM 6.7/4]
       **
       35

Now when I change "type ITEM is private" to  "... is limited private",
the compiler complains about Any_Items .. Stop_Class "cannot be
limited for in mode".

I've tried reading the LRM, but am confused by the reference.

I've gotten the other matching algorithms to run (debugging some typos
out of the book) but like the idea of using wildcards, etc.  Thanks for
any pointers.

- Jim
---------------------------------------------------------------------
Under-Gradual Student, Swinburne University of Technology, Australia



Fri, 21 Mar 1997 22:54:52 GMT  
 Newbie Generic Reg Exp Pattern Matching Question

          33     with function "=" (  Left  : in ITEM;
          34                          Right : in ITEM) return Boolean;
          **
       *****E equality parameters must be same limited type [LRM 6.7/4]
          **
          35

    This certainly looks like a compiler bug.  It is illegal to
explicitly declare an equality function for a private type, (except by
using the "Goodenough" workaround), but as a generic subprogram
parameter it is legitimate.

    First report the bug to your compiler vendor, then try one (or
more) of the following workarounds:

   1) It may be the case that if you provide a default the compiler
will let this through. Try:

        with function "=" (  Left  : in ITEM;
                             Right : in ITEM) return Boolean is <>;

   Can't hurt to try, and it really should be written this way anyway.

   2) Change the name of the equality subprogram in your generic formal
parameter list, and modify the body of the package to use the renamed
operation. (Ugh! Not very maintianable.)

   3) Declare a generic as follows:

    generic
      type LP is limited private;
      with function Equal(L,R: LP) return Boolean;
    function "="(L,R: LP) return Boolean;

    function "="(L,R: LP) return Boolean is
    begin return Equal(L,R); end "="

    Now modify your code to pass an equality function in under some
other name, then instantiate this function in the package
specification.  This is the Goodenough "trick."

  4) As in one above but hide the instantiation in the body of your
generic.

--

                                        Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...



Sat, 22 Mar 1997 19:29:39 GMT  
 Newbie Generic Reg Exp Pattern Matching Question

       generic
         type LP is limited private;
         with function Equal(L,R: LP) return Boolean;
       function "="(L,R: LP) return Boolean;

The name of a generic must be an identifier, so if your compiler accepts
this, report a bug to the vendor ;-)

       generic
         type LP is limited private;
         with function Equal(L,R: LP) return Boolean;
       package GOODENOUGH_TRICK is
         function "="(L,R: LP) return Boolean;
       end;
--
Stef VAN VLIERBERGHE            Eurocontrol - Central Flow Management Unit

Tel: +32 2 729 33 42            B-1040 BRUSSELS
Fax: +32 2 729 32 16            Belgium



Sun, 23 Mar 1997 06:03:58 GMT  
 Newbie Generic Reg Exp Pattern Matching Question


Quote:


>      33     with function "=" (  Left  : in ITEM;
>      34                          Right : in ITEM) return Boolean;
>      **
>       *****E equality parameters must be same limited type [LRM 6.7/4]
>      **
>      35

>    This certainly looks like a compiler bug.  It is illegal to
>explicitly declare an equality function for a private type, (except by
>using the "Goodenough" workaround), but as a generic subprogram
>parameter it is legitimate.

Sorry, I don't agree.  The compiler looks right.  The John Goodenough
"workaround" takes advantage of the fact that a formal limited type
can be instantiated with an actual non-limited type.  But the restriction
about "=" being defined only for limited types applies in generic
formal parts as well as outside.

Quote:
>    First report the bug to your compiler vendor...

Don't bother.  The compiler is enforcing the Ada 83 rules.
By the way, in Ada 9X, there is no such limitation.

Quote:
> ...
>                                    Robert I. Eachus


Intermetrics, Inc.


Sun, 23 Mar 1997 10:21:14 GMT  
 Newbie Generic Reg Exp Pattern Matching Question
  Stef said:

 >  The name of a generic must be an identifier, so if your compiler accepts
 >  this, report a bug to the vendor ;-)

   Oops!  Blush.  I knew this, and it is always a mistake to post code
without compiling it first.  The syntax is permits it but there is a
semantic restriction in 12.1(4).  You have to make the generic unit a
package, with the equality function inside.  (You can rename the
function after instantiation to get rid of the qualified name.)


 > Sorry, I don't agree.  The compiler looks right.  The John
 > Goodenough "workaround" takes advantage of the fact that a formal
 > limited type can be instantiated with an actual non-limited type.
 > But the restriction about "=" being defined only for limited types
 > applies in generic formal parts as well as outside.

   Here I don't agree with Tucker, but I doubt it will ever be
resolved by the ARG.  Paragraph 6.7(4) says: "The explicit declaration
of a function that overloads the equality operator "=", other than by
a renaming declaration, is only allowed if both parameters are of the
same limited type."  The explicit is there since most type
declarations implicitly declare an equality operation.  Renaming
declaration is a syntactic term defined in 8.5, so the question is
whether a generic formal subprogram declaration declares a function.

    Paragraph 12.1.3(1) says: "A generic parameter declaration that
includes a subprogram specification declares a generic formal
subprogram."

    Paragraph 12.1.3(3) says: "A generic formal subprogram DENOTES the
subprogram, enumeration literal, or entry supplied as the
corresponding generic actual parameter in a generic instantiation..."
(Emphasis mine.)

    So a generic formal subprogram declaration does not declare a
subprogram, it denotes one, to be named later.  The restriction on
declarations does not apply. ;-)  (The other restrictions on
declarations of equality do apply, because they are defined for
overloadings of equality.)

    Of course, others may not agree, and this would probably lead to
an interesting and spirited discussion in the ARG.  Fortunately, or
unfortunately depending on your perspective, that debate will probably
not occur, since the restriction is dropped in Ada 9X, and the
explicit direction to compiler vendors is to allow Ada 9X features
where possible.

--

                                        Robert I. Eachus

with Standard_Disclaimer;
use  Standard_Disclaimer;
function Message (Text: in Clever_Ideas) return Better_Ideas is...



Sun, 23 Mar 1997 18:35:01 GMT  
 Newbie Generic Reg Exp Pattern Matching Question

Here's a message from my brother.  His site seems to have missed *all* the
followups.

--------------------------------------------------------------------------

--|     Thanks for the pointers.

--|     I have used the Goodenough "hack" to get the package going.
--|     Is John Goodenough a real person, or just a made up name?
--|     (ie Good Enough?)

--|     I don't think I'll ever be a competent Ada language-lawyer
--|     like Robert Eachus, Stef Van Vlierberghe or Tucker Taft.
--|     If I was a {*filter*} man, I'll have to place an each way bet on
--|     the proper interpretation, as the code by Booch suggests he
--|     came to the same conclusion as Robert (assuming that Grady Booch
--|     did compile his code ;)  But the Meridian compiler seems to support
--|     Tucker and Stef.

--|     Jim

----------------------------------------------------------------------------



Sun, 30 Mar 1997 00:10:59 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Newbie Question Pattern match

2. pattern matching - question from newbie

3. Newbie: searching an English dictionary for a reg exp

4. Generic functions + Pattern Matching

5. iss-matching - the free Regular Expression / Pattern Matching cluster

6. Reg Exp Search in Asm?

7. Is // a reg exp?

8. reg exp help needed

9. Reg Exp in awk

10. Why no reg exp in Forth?

11. variables in reg exp repetition

12. Reg-exp library?

 

 
Powered by phpBB® Forum Software