2

I am a beginner in TCL and trying to write a TCL script to parse and print out all lines in a C++ code file which is used for variable declarations. Till Now I am able to detect and print all statements inside a block {}. Now I understand that the different variable declarations format in Ansi C++ modules could be:

int  x;
int  y=9;
int  x,y;
int  x,y=0;
int  x,y=0,c;
short const x=9;
int* x,y,c=null;
short const *x=NULL;
MyObj aobj();
MyObj aobj(x,y);
Myobj * a = dynamic_cast<A *>(b);
String x = "test";

I am not sure if I missed any.

What I am doing is that I am splitting the line :

if { [ string equal $lineType "statement" ] == 1 } {
        puts $lineValue
        set wordList [regexp -inline -all -- {\S+} $lineValue]
}

to verify the below format by different values at its list index:

qualifier varaiablename = value;
qualifier varaiablename;
qualifier varaiablename = value, variablename;

But I am not able to achieve it. I believe that TCL regexp would help us here to achieve the objective. However I am unable to conclude of what regexp expression I should use / code here.

I want to avoid looking for C++ defined data types / keywords but look for a specific format in string which is actually a C++ statement:

qualifier type name;
qualifier type name, name2;
qualifier type name = value;

or rather

word word word;
word word word, word;
word word word = integer, word = word;

As per the declarations format mentioned above.

Please provide your valuable thoughts / input.

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • 3
    You'd be better off using a proper parser; the language of C++ declarations is non-trivial… – Donal Fellows Oct 31 '12 at 14:08
  • Note that no one prevents you from placing a newline instead of a space in the `int x ;` declaration, so it's more involved. – kostix Oct 31 '12 at 14:27

1 Answers1

0

There is C/C++ tokenizer which uses an LPEG grammar for turning a C/C++ file into an array of tokens, and also has code for pretty printing a token array as HTML.

ltcl, a simple binding of the Tcl interpreter to Lua, can be used to integrate it.

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265