在python中使用正则表达式确定C ++函数及其参数 [英] Using regular expressions in python to determine C++ functions and their parameters

查看:264
本文介绍了在python中使用正则表达式确定C ++函数及其参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在此python脚本中做错了什么,但它变得令人费解,并且我看不到我在做错什么.

So I'm doing something wrong in this python script, but it's becoming convoluted and I'm losing sight of what I'm doing wrong.

我希望脚本浏览文件,找到所有函数定义,然后提取函数的名称,返回类型和参数,并输出"doxygen"样式的注释,如下所示:

I want a script to go through a file, find all the function definitions, and then pull out the name, return type, and parameters of the function, and output a "doxygen" style comment like this:

/******************************************************************************/
  /*!
    \brief
      Main function for the file

    \return
      The exit code for the program
  */
/******************************************************************************/

但是我在尝试解析参数时正则表达式做错了什么...这是到目前为止的脚本:

But I'm doing something wrong with the regular expression in trying to parse the parameters... Here is the script so far:

import re
import sys

f = open(sys.argv[1])

functions = []

for line in f:
  match = re.search(r'([\w]+)\s+([\S]+)\(([\w+\s+\w+])+\)',line)
  if line.find("\\fn") < 0:
    if match:
      returntype = match.group(1)
      funcname = match.group(2)
      print '/********************************************************************'
      print "  \\fn " + match.group()
      print ''
      print '  \\brief'
      print '    Function description for ' + funcname
      print ''
      if len(match.groups()) > 2:
        params = []
        count = len(match.groups()) - 2
        while count > 0:
          matchingstring = match.group(count + 2)
          if matchingstring.find("void") < 0:
            params.append(matchingstring)
          count -= 1
        for parameter in params:
          print "  \\param " + parameter
          print '    Description of ' + parameter
          print ''
      print '  \\return'
      print '    ' + returntype
      print '********************************************************************/'
      print ''

任何帮助将不胜感激.谢谢

Any help would be appreciated. Thanks

推荐答案

C ++的语法过于复杂,无法通过简单的方式处理 常用表达.您至少需要一个最小的解析器. 我发现在某些情况下,我不关心 通常使用C ++,但只有我自己的风格,我常常可以逃脱 使用基于Flex的令牌生成器和简单的状态机.这 在许多合法的C ++情况下都会失败-对于初学者来说, 当然,如果有人使用预处理程序来修改语法; 也是因为<可以具有不同的含义,具体取决于 它之前的名称是否命名为模板.但是经常 足以胜任特定工作.

The grammar of C++ is far too complex to be handled by simple regular expressions. You'll need at least a minimal parser. I've found that for restricted cases, where I'm not concerned with C++ in general, but only my own style, I can often get away with a flex based tokenizer and a simple state machine. This will fail in many cases of legal C++—for starters, of course, if someone uses the pre-processor to modify the syntax; but also because < can have different meanings, depending on what precedes it names a template or not. But it's often adequate for a specific job.

这篇关于在python中使用正则表达式确定C ++函数及其参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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