#include <regex.h> gives errors

#include <regex.h> gives errors

Post by Daniel Barro » Thu, 31 May 2001 00:35:58



I'm writing a simple regular expression class to use in a project of mine.
When I compile it:

c++  -Wall -c RegExp.cpp
In file included from RegExp.hpp:8,
                 from RegExp.cpp:7:
/usr/include/regex.h:352: syntax error before `;'
/usr/include/regex.h:450: type specifier omitted for parameter
/usr/include/regex.h:450: parse error before `,'
/usr/include/regex.h:521: type specifier omitted for parameter
/usr/include/regex.h:521: parse error before `,'
/usr/include/regex.h:524: syntax error before `('
RegExp.cpp: In method `bool RegExp::match(const char *, const char *)':
RegExp.cpp:23: `struct regex_t' has no member named `re_nsub'
RegExp.cpp:30: `struct regex_t' has no member named `re_nsub'
RegExp.cpp:37: `struct regex_t' has no member named `re_nsub'
make: *** [RegExp.o] Error 1

Looking at line 352 in /usr/include/regex.h:

 RE_TRANSLATE_TYPE translate;

        /* Number of subexpressions found by the compiler.  */
  size_t re_nsub;  <----- THIS IS LINE 352

        /* Zero if this pattern cannot match the empty string, one else.

I wonder if this is a coinsidence?

My platform is RH6.2 upgraded with all the patches from RH.
c++ --version
egcs-2.91.66

I am trying to follow the example on page 411 of Linux Application
Development by Johnson and Troan.

Some of my code:

bool RegExp::match(const char* exp, const char* text) {
    regex_t reg;
    if (regcomp(&reg, exp, REG_ICASE)) {
        regfree(&reg);
        return false;  // need exception or something
    }
    regmatch_t *pmatch;
    pmatch = alloca(sizeof(regmatch_t) * reg.re_nsub);  <--- LINE 23

Shurely there is a problem with regex.h?  And not my code?  This must be a
known problem!  Anyone?

Go on, help an open source project :)  Thanks

--
Daniel Barron - use [at jadeb.com] for personal replys.

 
 
 

#include <regex.h> gives errors

Post by Jens.Toerr.. » Thu, 31 May 2001 00:13:06



> I'm writing a simple regular expression class to use in a project of mine.
> When I compile it:
> c++  -Wall -c RegExp.cpp
> In file included from RegExp.hpp:8,
>                  from RegExp.cpp:7:
> /usr/include/regex.h:352: syntax error before `;'
> /usr/include/regex.h:450: type specifier omitted for parameter
> /usr/include/regex.h:450: parse error before `,'
> /usr/include/regex.h:521: type specifier omitted for parameter
> /usr/include/regex.h:521: parse error before `,'
> /usr/include/regex.h:524: syntax error before `('
> RegExp.cpp: In method `bool RegExp::match(const char *, const char *)':
> RegExp.cpp:23: `struct regex_t' has no member named `re_nsub'
> RegExp.cpp:30: `struct regex_t' has no member named `re_nsub'
> RegExp.cpp:37: `struct regex_t' has no member named `re_nsub'
> make: *** [RegExp.o] Error 1
> Looking at line 352 in /usr/include/regex.h:
>  RE_TRANSLATE_TYPE translate;
>    /* Number of subexpressions found by the compiler.  */
>   size_t re_nsub;  <----- THIS IS LINE 352

What are the lines of *your* program just before the #include?
I don't use C++, but in C most of the times I had these kinds of
problems I had done something stupid in my own program, mostly
a forgotten semicolon in one of my own header files...

                                     HTH, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

#include <regex.h> gives errors

Post by Daniel Barro » Thu, 31 May 2001 01:27:15




[snip]

Quote:

> What are the lines of *your* program just before the #include?

in the RegExp.hpp:

BEGINING OF FILE
#define HPP_REGEXP
#include <regex.h>
#include <string>
#include <deque>
class RegExp {

in the RegExp.cpp:

BEGINING OF FILE
#include "RegExp.hpp"

RegExp::RegExp()
:imatched(false) {}

bool RegExp::match(const char* exp, const char* text) {
    regex_t reg;
    if (regcomp(&reg, exp, REG_ICASE)) {
        regfree(&reg);
        return false;  // need exception
    }
    regmatch_t *pmatch;
    pmatch = alloca(sizeof(regmatch_t) * reg.re_nsub);

Quote:> I don't use C++, but in C most of the times I had these kinds of
> problems I had done something stupid in my own program, mostly
> a forgotten semicolon in one of my own header files...

Can't see a problem with that.  No missing ; or anything.  The error clearly
seems to be in regex.h.  Thanks for responding.

--
Daniel Barron - use [at jadeb.com] for personal replys.

 
 
 

#include <regex.h> gives errors

Post by Daniel Barro » Thu, 31 May 2001 01:38:50




[snip]

Quote:> /usr/include/regex.h:352: syntax error before `;'
[snip]
>   size_t re_nsub;  <----- THIS IS LINE 352
[snip]
> Shurely there is a problem with regex.h?  And not my code?  This must be a
> known problem!  Anyone?

Aha!  A quick websearch on google on the regex.h gave a page:
http://thibs.menloschool.org/help/susv2/xsh/regex.h.html

Which said it contains a size_t, which I've found to be declared in
sys/types.h

The page gave a link to:
http://thibs.menloschool.org/help/susv2/xsh/systypes.h.html

So this failing /must/ be a mistake in regex.h, not including the
sys/types.h?

Well, anyway - solved :)

--
Daniel Barron - use [at jadeb.com] for personal replys.

 
 
 

#include <regex.h> gives errors

Post by Toby Hayne » Thu, 31 May 2001 03:30:37




Quote:> I'm writing a simple regular expression class to use in a project of mine.
> When I compile it:

> c++  -Wall -c RegExp.cpp
> In file included from RegExp.hpp:8,
>                  from RegExp.cpp:7:
> /usr/include/regex.h:352: syntax error before `;'
> /usr/include/regex.h:450: type specifier omitted for parameter
> /usr/include/regex.h:450: parse error before `,'
> /usr/include/regex.h:521: type specifier omitted for parameter
> /usr/include/regex.h:521: parse error before `,'
> /usr/include/regex.h:524: syntax error before `('
> RegExp.cpp: In method `bool RegExp::match(const char *, const char *)':
> RegExp.cpp:23: `struct regex_t' has no member named `re_nsub'
> RegExp.cpp:30: `struct regex_t' has no member named `re_nsub'
> RegExp.cpp:37: `struct regex_t' has no member named `re_nsub'
> make: *** [RegExp.o] Error 1
> Looking at line 352 in /usr/include/regex.h:

>  RE_TRANSLATE_TYPE translate;

>    /* Number of subexpressions found by the compiler.  */
>   size_t re_nsub;  <----- THIS IS LINE 352
>         /* Zero if this pattern cannot match the empty string, one else.
> Shurely there is a problem with regex.h?  And not my code?  This must be a
> known problem!  Anyone?

> Go on, help an open source project :)  Thanks

No - you have just included a file which requires certain definitions defined
before it is called. In this case, you should probably include either <types.h>
or <sys/types.h>.

Hmm. Look in the regex.h header - it tells you this:

/* POSIX says that <sys/types.h> must be included (by the caller) before
   <regex.h>.  */

The problem is in your code :-) Add #include <sys/types.h> before
#include <regex.h> and all should be well.

Cheers,
Toby Haynes
--

Toby Haynes
The views and opinions expressed in this message are my own, and do
not necessarily reflect those of IBM Canada.

 
 
 

#include <regex.h> gives errors

Post by Daniel Barro » Thu, 31 May 2001 07:36:50




[snip]

Quote:> Hmm. Look in the regex.h header - it tells you this:

> /* POSIX says that <sys/types.h> must be included (by the caller) before
>    <regex.h>.  */

> The problem is in your code :-) Add #include <sys/types.h> before
> #include <regex.h> and all should be well.

And so it does.  Can't see the comments for the green bits ;)  Thanks for
the clarification.

--
Daniel Barron - use [at jadeb.com] for personal replys.