Analyzing a c program

ritesh2190

Member
Joined
Jun 7, 2011
Messages
15
Programming Experience
1-3
i need to write a vb.net program which analyses a given c program and gives the list of the following
-functions
-variable with the type
-macros
-pointers
all these shud be given as a output so that i can use them in other parts of the code.
anyway particular way to go about this????
 
Hi - to clarify you mean a text file of an un-compiled c program right?

I would read the file to a string then count the number of times you find "Function" etc, skip one space and store the text in between as a function name.

The gist of reading a file is below.

VB.NET:
'Read the file to a holding string varible
'Class declaration
    Private MySFD As New SaveFileDialog


Public Sub Example
    Try
        
        Dim myFileAsText As String
        Dim myCFile As StreamReader


        MySFD.DefaultExt = ".c"
        MySFD.FileName = Main.SaveFileName
        MySFD.ShowHelp = False
        MySFD.CheckFileExists = False
        MySFD.CheckPathExists = False
        MySFD.SupportMultiDottedExtensions = True
        MySFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
        If MySFD.ShowDialog <> DialogResult.OK Then Return


        'Read the code from the file to a holding string varible
        Try
            myCFile = File.OpenText(file_path.ext)
            myFileAsText = myCFile.ReadToEnd
            myCFile.Close()
    
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return
    End Try



Here is a link to count words.
http://www.vbdotnetforums.com/vb-net-general-discussion/45487-count-occurance-words-txt-file-visual-basic-net.html
 
i know how to read the code and count the number of lines and stuff,how do i recognise whether the word i have read is a function or not?also remember i have to make a list of all the integer,float and char variables!!!
 
Its basically pattern matching -

I do not know c - so I have used come c# example text, but i have listed pseudo code - i don't have time at work to go into detail - i apologise.

The list of the c syntax is here C syntax - Wikipedia, the free encyclopedia


c variables are like
private static Bitmap bmpScreenshot;
private static Graphics gfxScreenshot;
int smallStride = smallData.Stride;
int bigStride = bigData.Stride;



C subs are like


static void Main(){...}
private void GrabTheScreen(){}
private void btnCapture_Click(object sender, EventArgs e){...}

So you would look for a "private void " then find the position of the "(" then anything in between is the name of a function /sub.

Pseudo code


Sub to find subs...
sub foundsubnames[500] as string


for i = 0 to 500

match "static void " copy string in between to "(" ; save as sub name foundsubnames = string copied text
match "private void " copy string in between to "(" ; save as sub name foundsubnames = string copied text
match "....etc...more examples" copy string in between to "(" ; save as sub name foundsubnames = string copy
match "....etc...more examples" copy string in between to "(" ; save as sub name foundsubnames = string copy

next i

end sub

Sub to find varibles...
sub foundvarnames[500] as string

Like wise for varibles -

int smallStride = smallData.Stride;

for i = 0 to 500

match "int smallStride" copy string in between 'int' to "=" ; save- foundvarnames = string copied text
match "bool matchFound" copy string in between 'bool' to "=" ; save- foundvarnames = string copied text
matchFound = true;

next i
end sub

Do not forget to trim whitespace by using the string.trim method.

Also this may make interesting reading - It may not do what you want - some programs may be open source i do not know - but it may help analyse
List of tools for static code analysis - Wikipedia, the free encyclopedia
 
Back
Top