Newbie python design question

M

Michael

Hi,
I'm trying to write a script to parse a .cpp file and begin to create a
'translational unit'.
To do this i need to:

Go through the file and remove all 'C' comments as
/* Comment 1*/
(can be on multiple lines)

Go through and remove all 'C++' comments, anything between // and '\n' char.

The start at the top, and work the way through, with the following valid
terms:

#include <filename>
#include "filename"
- copy the contents of filename to this point in the file and continue.

#define X Y
-Store the term X,Y in DefineDictionary, then later if X is encountered,
substitute Y.

namespace n
{

};
-a namespace, can contain classes, functions and sub-namespaces

class c
{


};
-a class.


If i were to process this in C++, i would create some form of statemachine,
similar to a regex engine... I would just like some ideas on the easiest way
to implment this in python!!

Regards

Mike
 
P

Philippe C. Martin

Hi,

I C I usually use switch for my FSMs, in Python however I usually use if +
elif ....

Your question makes me realize it would be trivial to use a dictionnary in
case the FSM had too many states, the key being the state and the item the
method to handle the state.

Regards,

Philippe
 
B

Bruno Desthuilliers

Michael a écrit :
Hi,
I'm trying to write a script to parse a .cpp file and begin to create a
'translational unit'.
To do this i need to:

Go through the file and remove all 'C' comments as
/* Comment 1*/
(can be on multiple lines)

Go through and remove all 'C++' comments, anything between // and '\n' char.

The start at the top, and work the way through, with the following valid
terms:

#include <filename>
#include "filename"
- copy the contents of filename to this point in the file and continue.

#define X Y
-Store the term X,Y in DefineDictionary, then later if X is encountered,
substitute Y.

namespace n
{

};
-a namespace, can contain classes, functions and sub-namespaces

class c
{


};
-a class.


If i were to process this in C++, i would create some form of statemachine,
similar to a regex engine... I would just like some ideas on the easiest way
to implment this in python!!

I have no real answer, but well:
- there's a regexp module in the stdlib
- there's a quite nice FSM implementation on the Python Cookbook
- there are many Python-based parser generators

And I would of course recommand the reading of "Text Processing in
Python"...

HTH
Bruno
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top