Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing C ++ source code in java environment [closed]

I have a C++ application and I also have the source code of that and it has been built on .Net framework. what I'm looking for now is a tool (ideally free) that gets my source code as an input, and after some preprocessing or some required programming gives me one intermediate representation of the structural dependency of the source code elements, like an AST or a call graph. I am working with java to inspect this C++ source code so it would be much better if the solution is in java environment like a plugin for eclipse or something. is there any available tool suited my need? thank you all.

like image 542
Salman9 Avatar asked Oct 12 '12 19:10

Salman9


People also ask

What is parse error in Java?

This is a compile error, easily fixed The Java error message Reached End of File While Parsing results if a closing curly bracket for a block of code (e.g, method, class) is missing.

How is parsing done in Java?

Parsing String in java is known as converting data in the String format from a file, user input, or a certain network. Parsing String is the process of getting information that is needed in the String format. String parsing in java can be done by using a wrapper class.

What is C parser?

The C/C++ parser is used for C and C++ language source files. The C/C++ parser uses syntax highlighting to identify language elements, including the following elements: Identifiers. Operators. Punctuation.


2 Answers

I've had success parsing C++ in Java using the CND module from NetBeans. It's still butt ugly, but probably better than using raw ANTLR or what not. First, download the "all-in-one" package from http://netbeans.org/downloads/zip.html for simplicity (CND doesn't actually require all those classes) and unzip that somewhere like in the current directory. Next, here's a toy C++ header file I've been playing with:

namespace foo {

int f(int p);

template<typename A> class bar {
    void run(A) { }
};

}

And here's my attempt at parsing that with CND:

import java.io.*;
import java.util.*;
import org.openide.filesystems.*;
import org.netbeans.modules.cnd.api.model.*;
import org.netbeans.modules.cnd.api.model.services.*;
import org.netbeans.modules.cnd.modelimpl.csm.*;

public class Foo {
    public static void main(String[] args) throws Exception {
        FileObject fo = FileUtil.toFileObject(new File(args[0]));
        CsmStandaloneFileProvider fp = CsmStandaloneFileProvider.getDefault();
        CsmModel model = CsmModelAccessor.getModel();
        CsmModelState modelState = CsmModelAccessor.getModelState();

        CsmFile cf = fp.getCsmFile(fo);
        cf.scheduleParsing(true);
        Collection<CsmOffsetableDeclaration> c = cf.getDeclarations();
        c = ((CsmNamespaceDefinition)c.toArray()[0]).getDeclarations();
        for (CsmOffsetableDeclaration d : c) {
            if (d instanceof CsmFunction) {
                CsmFunction f = (CsmFunction)d;
                System.out.print(f.getQualifiedName() + " " + f.getName() + "(");
                Collection<CsmParameter> pp = f.getParameters();
                for (CsmParameter p : pp) {
                    System.out.print(p.getType().getClassifierText());
                }
                System.out.println(")");
            } else if (d instanceof ClassImpl) {
                ClassImpl cls = (ClassImpl)d;
                System.out.println("Got template? " + cls.isTemplate());
                List<CsmTemplateParameter> lt = cls.getTemplateParameters();
                for (CsmTemplateParameter t : lt) {
                    System.out.println(t.getQualifiedName() + " " + t.getName());
                }
                Collection<CsmMember> cm = cls.getMembers();
                for (CsmMember m : cm) {
                    CsmFunction f = (CsmFunction)m;
                    System.out.print(f.getQualifiedName() + " " + f.getName() + "(");
                    Collection<CsmParameter> pp = f.getParameters();
                    for (CsmParameter p : pp) {
                        System.out.print(p.getType().getClassifierText());
                    }
                    System.out.println(")");
                }
            }
        }
    }
}

The amount of JAR files we need to add to the classpath is quite large, and there must be a better way to deal with this, but this works for now:

$ export CLASSPATH=netbeans/platform/modules/org-netbeans-modules-editor-mimelookup.jar:netbeans/platform/modules/org-netbeans-modules-queries.jar:netbeans/dlight/modules/org-netbeans-modules-dlight-libs-common.jar:netbeans/ide/modules/org-netbeans-modules-projectapi.jar:netbeans/platform/modules/org-netbeans-api-progress.jar:netbeans/platform/modules/org-openide-windows.jar:netbeans/platform/modules/org-openide-text.jar:netbeans/platform/modules/org-openide-awt.jar:netbeans/platform/lib/org-openide-modules.jar:netbeans/platform/modules/org-openide-nodes.jar:netbeans/platform/modules/org-netbeans-modules-masterfs.jar:netbeans/platform/core/org-openide-filesystems.jar:netbeans/platform/lib/org-openide-util.jar:netbeans/platform/lib/org-openide-util-lookup.jar:netbeans/platform/modules/org-openide-loaders.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-api-model.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-api-project.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-model-services.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-modelimpl.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-modelutil.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-utils.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-repository.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-repository-api.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-apt.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-source.jar:netbeans/cnd/modules/org-netbeans-modules-cnd-antlr.jar:.
$ javac Foo.java
$ java Foo Foo.h

And that outputs the following:

foo::f f(int)
Got template? true
foo::bar::A A
foo::bar::run run(A)

BTW, we can do something similar with Eclipse CDT: Parsing / reading C-Header files using Java

like image 97
Samuel Audet Avatar answered Sep 27 '22 19:09

Samuel Audet


You might be interested in doxygen, it's a free tool that generates documentation based on source code. Without any additional work on your part you can generate call graphs, inheritance diagrams, collaboration diagrams, and many other useful tools.

http://www.doxygen.nl/

like image 27
Nick Hartung Avatar answered Sep 27 '22 19:09

Nick Hartung