tags:

views:

100

answers:

2

Hi all, I am trying to write a regex to solve the below C struct for one of our requirement. I am parsing the C structure file. In that I will have child and parent struct. The child will inherit parent struct members. I want the output, which consists of struct members and there length for further processing

INPUT:

#define  Len1  10;
#define  Len2  20;
#define  Len3   3;
#define  Len4   4;

typedef struct
{
char CHAR1           [Len1];
char CHAR2           [Len2];
} MyParent;


typedef struct
{

MyParent base;
char CHAR3        [Len3];
char CHAR4          [Len4];

} MyChild;

In the above I have to get: below OUTPUT:

 CHAR1 10
 CHAR2 20
 CHAR3 3
 CHAR4 4

The Perl Script will be really helpfull;

+4  A: 

You changed the problem after I gave my answer. It's not big of a deal though because the answer is not that much more complicated. You remember what you see in the #defines and use them later:

while( <DATA> ) {
    if( /^#define\s+(\w+)\s+(\d+)/ ) {
        $Len{$1} = $2;
        }
    elsif( /^char\s/ ){
        s/(?<=CHAR\d)\s+//;
        s/;$//;
        s/(?<=\[)(\w+)(?=])/ $Len{$1} || $1 /e;
        print;
        }
    }

This is a pretty trivial problem. Are you sure there's something that you aren't telling us? You don't need to do anything fancy. Skip all the lines that you want to ignore, and fixup the remaining lines to match your output needs:

while( <DATA> ) {
    next unless /^char\s/;
    s/(?<=CHAR\d)\s+//;
    s/;$//;
    print;
    }

__DATA__
#define  Len1  10;
#define  Len2  20;
#define  Len3   3;
#define  Len4   4;

typedef struct
{
char CHAR1           [Len1];
char CHAR2           [Len2];
} MyBase;


typedef struct
{

MyBase base;
char CHAR3          [Len3];
char CHAR4          [Len4];

} MyStruct;
brian d foy
I imagine the OP wants more, but since he's not telling us what, this is the best he'll get :)
Philip Potter
Sorry for not giving the complete requirement:I am parsing the C structure file.In that I will have child and parent struct.The child will inherit parent struct members.I want the output, which consists of struct members and there length for further processing.I have edited the OUTPUT as the exact needThanks all for quick reply
@user430294: If this is not all you need, then regular expressions are not what you are looking for; you would need a real parser.
Svante
Upvoted, for being a good original answer. brain d foy++ (this time)
Evan Carroll
+3  A: 

What about this :

#!/usr/bin/perl
use strict;
use warnings;

my %len;
while( <DATA> ) {
    if (/^#define\s+(\w+)\s+(\d+)\s*;/) {
        $len{$1} = $2;
    } elsif (s/^\s*char\s+(\w+)\s+\[(\w+)\]\s*;/$1 $len{$2}/) {
        print;
    }
}

__DATA__
#define  Len1  10;
#define  Len2  20;
#define  Len3   3;
#define  Len4   4;

typedef struct
{
char CHAR1           [Len1];
char CHAR2           [Len2];
} MyBase;


typedef struct
{

MyBase base;
char CHAR3          [Len3];
char CHAR4          [Len4];

} MyStruct;

output :

CHAR1 10
CHAR2 20
CHAR3 3
CHAR4 4
M42
I am really glad that I am getting quick reply's in this forum. Thanks a lot for all. <:0)
You're welcome.
M42
Who did he copy? Don't accuse people without proof because you're retaliating because you're mad that M42 bested you in another question.
brian d foy