1

I need to parse C++ source code using C#.

I tried CppAst.net. It seems that it can parse header files, but not source files.

using CppAst;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CppParserCppASTdotNet
{
    class Program
    {
        static void Main(string[] args)
        {
            string cppSourceCode = @"
#include <iostream>
using namespace std;

class HelloWorld {
public:
    void greet() {
        cout << ""Hello, World!"" << endl;
    }
};

int main() {
    HelloWorld obj;
    obj.greet();
    return 0;
}
";

            // Parse a C++ files
            var compilation = CppParser.Parse(cppSourceCode);
            // Print diagnostic messages
            foreach (var message in compilation.Diagnostics.Messages)
                Console.WriteLine(message);

            // Print All enums
            foreach (var cppEnum in compilation.Enums)
                Console.WriteLine(cppEnum);

            // Print All functions
            foreach (var cppFunction in compilation.Functions)
                Console.WriteLine(cppFunction);

            // Print All classes, structs
            foreach (var cppClass in compilation.Classes)
                Console.WriteLine(cppClass);

            // Print All typedefs
            foreach (var cppTypedef in compilation.Typedefs)
                Console.WriteLine(cppTypedef);
        }
    }
}

Ouput

C:\Users\pc\source\repos\CppParserCppASTdotNet\CppParserCppASTdotNet\bin\Debug>CppParserCppASTdotNet.exe
cppast.input(2, 10): warning: non-portable path to file '<IOSTREAM>'; specified path differs in case from file name on disk
#include <iostream>
        -^-

C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility(81, 10): error: missing 'typename' prior to dependent type name '_It::iterator_category'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility(82, 10): error: missing 'typename' prior to dependent type name '_It::value_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility(83, 10): error: missing 'typename' prior to dependent type name '_It::distance_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility(224, 32): error: template parameter redefines default argument
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility(231, 10): error: missing 'typename' prior to dependent type name '_Tr::int_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\utility(289, 32): error: template parameter redefines default argument
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(25, 10): error: missing 'typename' prior to dependent type name '_A::size_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(26, 10): error: missing 'typename' prior to dependent type name '_A::difference_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(27, 10): error: missing 'typename' prior to dependent type name '_A::pointer'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(28, 10): error: missing 'typename' prior to dependent type name '_A::const_pointer'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(29, 10): error: missing 'typename' prior to dependent type name '_A::reference'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(30, 10): error: missing 'typename' prior to dependent type name '_A::const_reference'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(31, 10): error: missing 'typename' prior to dependent type name '_A::value_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(32, 10): error: missing 'typename' prior to dependent type name '_A::pointer'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(33, 10): error: missing 'typename' prior to dependent type name '_A::const_pointer'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xstring(599, 8): error: missing 'typename' prior to dependent type name 'basic_string<_E, _Tr, _A>::size_type'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xlocale(296, 15): error: template specialization requires 'template<>'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\xlocale(519, 15): error: template specialization requires 'template<>'
C:\Program Files (x86)\Microsoft Visual Studio\VC98\include\streambuf(16, 32): error: template parameter redefines default argument
(0, 0): error: too many errors emitted, stopping now
cppast.input(1, 1): warning: Compilation aborted due to one or more errors listed above.

C:\Users\pc\source\repos\CppParserCppASTdotNet\CppParserCppASTdotNet\bin\Debug>

How can I parse C++ source code in .NET?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.