正则表达式解析 C/C++ 函数声明 [英] Regex to parse C/C++ functions declarations

查看:37
本文介绍了正则表达式解析 C/C++ 函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 C 和 C++ 函数解析并拆分为主要组件(返回类型、函数名称/类和方法、参数等).

I need to parse and split C and C++ functions into the main components (return type, function name/class and method, parameters, etc).

我正在使用签名采用以下形式的标题或列表:

I'm working from either headers or a list where the signatures take the form:

public: void __thiscall myClass::method(int, class myOtherClass * )

public: void __thiscall myClass::method(int, class myOtherClass * )

我有以下正则表达式,它适用于大多数功能:

I have the following regex, which works for most functions:

(?<expo>public:|protected:|private:) (?<ret>(const )*(void|int|unsigned int|long|unsigned long|float|double|(class .*)|(enum .*))) (?<decl>__thiscall|__cdecl|__stdcall|__fastcall|__clrcall) (?<ns>.*)::(?<class>(.*)((<.*>)*))::(?<method>(.*)((<.*>)*))((?<params>((.*(<.*>)?)(,)?)*))

它不喜欢解析一些函数,但似乎与模式匹配.我现在不担心匹配不是类成员的函数(以后可以处理).该表达式用于 C# 程序中,因此 <label> 用于轻松检索组.

There are a few functions that it doesn't like to parse, but appear to match the pattern. I'm not worried about matching functions that aren't members of a class at the moment (can handle that later). The expression is used in a C# program, so the <label>s are for easily retrieving the groups.

我想知道是否有标准的正则表达式来解析所有函数,或者如何改进我的以处理奇怪的异常?

I'm wondering if there is a standard regex to parse all functions, or how to improve mine to handle the odd exceptions?

推荐答案

众所周知,C++ 很难解析;编写一个捕获所有情况的正则表达式是不可能的.例如,可以有无限数量的嵌套括号,这表明即使是 C++ 语言的这个子集也不是规则的.

C++ is notoriously hard to parse; it is impossible to write a regex that catches all cases. For example, there can be an unlimited number of nested parentheses, which shows that even this subset of the C++ language is not regular.

但您似乎追求的是实用性,而不是理论上的正确性.只需不断改进您的正则表达式,直到它捕获需要捕获的情况,并尝试使其尽可能严格,以免出现任何错误匹配.

But it seems that you're going for practicality, not theoretical correctness. Just keep improving your regex until it catches the cases it needs to catch, and try to make it as stringent as possible so you don't get any false matches.

如果不知道它没有捕获的奇怪的异常",很难说如何改进正则表达式.

Without knowing the "odd exceptions" that it doesn't catch, it's hard to say how to improve the regex.

这篇关于正则表达式解析 C/C++ 函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆