C解析乐趣 [英] C parsing fun

查看:63
本文介绍了C解析乐趣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helo ppl!


在工作中,我被赋予了根据我老板的概念制作脚本来分析C ++代码

的任务。要做到这一点,我需要以某种方式在Python中表示C ++

代码结构。我读了Yapps的文档,pyparsing

和其他类似的东西,然后我提出了一个非常简单的想法。我b $ b意识到括号中的代码几乎就像一个Python列表,除了我

必须用方括号替换大括号并包围

剩余带引号的东西。这个过程不会调用递归或节点

对象,只有纯粹的字符串操作,所以我相信它真的很快。

最后我可以通过调用eval得到结果列表( )用

字符串。


例如当我需要解析类定义时,我只需要

寻找包含模式* class *的列表项,下一个

项将是该类的内容作为另一个列表。


你可以在以下网址获取代码:

http://kiri.csing.hu/stack/python/bloppy-0.1.zip

(包括测试脚本[test.py])

Helo ppl!

At the job I was given the task to make a script to analyze C++ code
based on concepts my boss had. To do this I needed to represent C++
code structure in Python somehow. I read the docs for Yapps, pyparsing
and other stuff like those, then I came up with a very simple idea. I
realized that bracketed code is almost like a Python list, except I
have to replace curly brackets with squared ones and surround the
remaining stuff with quotes. This process invokes no recursion or node
objects, only pure string manipulations so I believe it''s really fast.
Finally I can get the resulting list by calling eval() with the
string.

For example when I need to parse a class definition, I only need to
look for a list item containing the pattern "*class*", and the next
item will be the contents of the class as another list.

You can grab the code at:

http://kiri.csing.hu/stack/python/bloppy-0.1.zip

(test script [test.py] included)

推荐答案

,最棒的是该算法可以与任何使用括号构造代码的

语言一起使用,例如PHP和许多

其他。

and the great thing is that the algorithm can be used with any
language that structures the code with brackets, like PHP and many
others.


基于概念我的老板。要做到这一点,我需要以某种方式在Python中表示C ++
based on concepts my boss had. To do this I needed to represent C++

代码结构。我读了Yapps的文档,pyparsing

和其他类似的东西,然后我提出了一个非常简单的想法。我b $ b意识到括号中的代码几乎就像一个Python列表,除了我

必须用方括号替换大括号并包围

剩余带引号的东西。这个过程不会调用任何递归或节点
code structure in Python somehow. I read the docs for Yapps, pyparsing
and other stuff like those, then I came up with a very simple idea. I
realized that bracketed code is almost like a Python list, except I
have to replace curly brackets with squared ones and surround the
remaining stuff with quotes. This process invokes no recursion or node



是的,这是一个不错的解决方案

有时候它还不够(赢了'''无论如何你需要更复杂的东西然后我会推荐

gccxml或者是它的python绑定:

http://www.language-binding.net/pygccxml/pygccxml.html

yes that''s a nice solution
sometimes it''s not enough though (won''t work on code obfuscated with
macros)

anyway if you need something more sophisticated then i''d recommend
gccxml or it''s python binding:

http://www.language-binding.net/pygccxml/pygccxml.html


响应的Thx,Szabolcs!我已经尝试过了,但是没有b $ b设法让它上班。我试图解析的源是一个巨大的MSVC

7.1解决方案,包含大约38个项目,我相信代码是如此复杂,以至于它有太多不同的依赖关系和GCC只是

无法处理它们。顺便说一下,我对C ++编译器不是很熟悉,所以

也许是因为编译器配置错误,但我真的不知道... b $ b知道...


SzabolcsNagyírta:
Thx for responding, Szabolcs! I''ve already tried that, but couldn''t
manage to get it to work. The source I tried to parse is a huge MSVC
7.1 solution containing about 38 projects, and I believe the code is
so complex that it has too many different dependencies and GCC just
can''t handle them. Btw I''m not deeply familiar with C++ compilers, so
maybe it was because of compiler misconfiguration, but I really don''t
know...

Szabolcs Nagy írta:

基于我老板的概念。要做到这一点,我需要以某种方式在Python中表示C ++

代码结构。我读了Yapps的文档,pyparsing

和其他类似的东西,然后我提出了一个非常简单的想法。我b $ b意识到括号中的代码几乎就像一个Python列表,除了我

必须用方括号替换大括号并包围

剩余带引号的东西。这个过程不会调用任何递归或节点
based on concepts my boss had. To do this I needed to represent C++
code structure in Python somehow. I read the docs for Yapps, pyparsing
and other stuff like those, then I came up with a very simple idea. I
realized that bracketed code is almost like a Python list, except I
have to replace curly brackets with squared ones and surround the
remaining stuff with quotes. This process invokes no recursion or node



是的,这是一个不错的解决方案

有时候它还不够(赢了'''无论如何你需要更复杂的东西然后我会推荐

gccxml或者是它的python绑定:

http://www.language-binding.net/pygccxml/pygccxml.html


这篇关于C解析乐趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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