栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

使用eclipse CDT解析C/C++文件语法树

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用eclipse CDT解析C/C++文件语法树

maven依赖

        
            org.eclipse.core
            org.eclipse.core.resources
            3.7.100
        
        
            com.github.mhoffrog.attached
            org.eclipse.cdt.core
            5.11.0
        

代码:

public class CDTTest {
    public static void main(String[] args) throws Exception {
        IASTTranslationUnit u = getTranslationUnit(new File("E:\xxx.c"));
        //输出的是⽂件的所有内容
        // System.out.println("simpleDeclaration.getRawSignature():" + u.getRawSignature());
        
        IASTComment[] com = u.getComments();
        for (IASTComment iastComment : com) {
            System.out.println("IASTComment:" + iastComment);
        }
        
        IASTPreprocessorStatement[] ps = u.getAllPreprocessorStatements();
        for (IASTPreprocessorStatement iastPreprocessorStatement : ps) {
            System.out.println(iastPreprocessorStatement.getRawSignature());
        }

        //得到⽂件中定义的声明
        IASTDeclaration[] decs = u.getDeclarations();
        for (IASTDeclaration child : decs) {
            //    ⽅法声明
            if (child instanceof IASTFunctionDefinition) {
                // // 获得函数说明符
                // 例
                // void
                System.out.println(((IASTFunctionDefinition) child).getDeclSpecifier().getRawSignature());
                // 获得函数的函数声明符
                // 例 函数名
                System.out.println(((IASTFunctionDefinition) child).getDeclarator().getRawSignature());
                // 获得函数体的内容
                System.out.println(((IASTFunctionDefinition) child).getBody().getRawSignature());
                // 输出函数的全部内容
                System.out.println(child.getRawSignature());
                // 与函数的起始位置有关
                IASTFileLocation FileLocation = child.getFileLocation();
                int startLine = FileLocation.getStartingLineNumber();
                int endLine = FileLocation.getEndingLineNumber();
                System.out.println("length:" + (endLine - startLine));
            }
        }
    }

    
    static IASTTranslationUnit getTranslationUnit(File source) throws Exception {
        FileContent reader = FileContent.create(source.getAbsolutePath(), getContentFile(source).toCharArray());

        //C++ 用 GPPLanguage 解析
        //C 用 GCCLanguage 解析

        return GPPLanguage.getDefault()
            .getASTTranslationUnit(reader, new ScannerInfo(), IncludeFileContentProvider.getSavedFilesProvider(), null,
                ILanguage.OPTION_IS_SOURCE_UNIT, new DefaultLogService());
        //        return GCCLanguage.getDefault().getASTTranslationUnit(
        //                reader,
        //                new ScannerInfo(),
        //                IncludeFileContentProvider.getSavedFilesProvider(),
        //                null,
        //                ILanguage.OPTION_IS_SOURCE_UNIT,
        //                new DefaultLogService());
    }

    
    static String getContentFile(File file) throws IOException {
        StringBuilder content = new StringBuilder();
        String line;
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
            while ((line = br.readLine()) != null) {
                content.append(line).append('n');
            }
        }
        return content.toString();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968836.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号