Regex来解析C / C ++函数声明 [英] Regex to parse C/C++ functions declarations

查看:88
本文介绍了Regex来解析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).

m从标头或列表中工作,其中签名采取以下形式:

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 * )

我有以下regex,它适用于大多数函数:

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.

我想知道是否有一个标准的regex来解析所有的函数,或者如何改进我的处理奇怪的异常。

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

推荐答案

p> 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.

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

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