parsing a c project

A

Aitor Garcia

Hi,

I need to know the memory locations of all variables in a C project including
variables allocated inside structs.

What I want to do in to expand the structs into its basic elements (floats,
int16 and int8).


In a header file (example.h) I have the following definitions.

struct house{
float area;

int8 rooms;
int16 visits;
};


struct car{
float price;
int8 color;
};

I have been able to extract from the project the name of every struct, the type of the struct and the beginning address of each struct.


example_list=[]
example_list.append(['house1','struct house','000082d0')
example_list.append(['house2','struct house','00003000')
example_list.append(['car1','struct car','00004000')

I need an output like this.

house1_struct_house_area float 000082d0
house1_struct_house_rooms int8 000082d4
house1_struct_house_visits int16 000082d5
house2_struct_house_area float 00003000
house2_struct_house_rooms int8 00003004
house2_struct_house_visits int16 00003005
car1_struct_car_price float 00004000
car1_struct_car_color int8 00004004

How can I efficiently do this in Python ?

I do not have very clear which element of Python should I use
to store the struct list or class

I would be very grateful if someone could give me some pointers.

Aitor
 
J

Jon Clements

Hi,

I need to know the memory locations of all variables in a C project including
variables allocated inside structs.

Pray tell us why?
What I want to do in to expand the structs into its basic elements (floats,
int16 and int8).

In a  header file (example.h) I have the following definitions.

struct house{
  float area;

  int8 rooms;
  int16 visits;

};

struct car{
  float price;
  int8 color;

};

I have been able to extract from the project the name of every struct, the type of the struct and the beginning address of each struct.

How have you done this? What compiler and debugger/profiler are you
using? etc...
The memory address is going to possibly change every time, unless
you're talking static variables within an applications own address
space (even then I'm not 100% sure -- I haven't had to touch C in 2
years, so I'd declare myself rusty).
example_list=[]
example_list.append(['house1','struct house','000082d0')
example_list.append(['house2','struct house','00003000')
example_list.append(['car1','struct car','00004000')

I need an output like this.

house1_struct_house_area float 000082d0
house1_struct_house_rooms int8 000082d4
house1_struct_house_visits int16 000082d5
house2_struct_house_area float 00003000
house2_struct_house_rooms int8 00003004
house2_struct_house_visits int16 00003005
car1_struct_car_price float 00004000
car1_struct_car_color int8 00004004

How can I efficiently do this in Python ?

Well using the pyparsing library, it's extremely easy to parse the C
grammar. But that's not what you're asking.
I do not have very clear which element of Python should I use
to store the struct list or class

I would be very grateful if someone could give me some pointers.

An un-intended C pun :) ?

If you let the list know the use-case, then we might stand a chance of
giving you some reference (C++ pun?)

Jon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,851
Latest member
CristineKo

Latest Threads

Top