推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
dimingxuan
V2EX  ›  Python

想实现 c 语言分析的功能,请教各位

  •  
  •   dimingxuan · Sep 15, 2015 via iPhone · 4538 views
    This topic created in 4069 days ago, the information mentioned may be changed or developed.
    想通过简单的方式,获取 c 文件中函数的输入输出变量,想请教一下有没有什么路径,或者现成的工具包
    9 replies  •  2015-09-16 22:10:01 +08:00
    445141126
    445141126
    Sep 15, 2015❤️1
    1
    类似 lint 的功能?
    首先要解析 c 文件生成语法树 ast ,根据语法树构造符号表作用域之类的,再分析就好了。
    推荐这本《编程语言实现模式》
    445141126
    445141126
    Sep 15, 2015❤️1
    2
    python 有 c 代码的解析库 https://github.com/eliben/pycparser
    dimingxuan
    dimingxuan
    OP
    Sep 15, 2015 via iPhone
    3
    @445141126 是一个简化的 lint ,谢谢提供的资料
    YuJianrong
    YuJianrong
    Sep 15, 2015❤️1
    4
    为什么不直接用 ctags ……
    dimingxuan
    dimingxuan
    OP
    Sep 15, 2015
    5
    @YuJianrong 没用过,能区别出输入输出变量吗?
    dialox
    dialox
    Sep 15, 2015❤️1
    6
    用 lex 可以实现
    YuJianrong
    YuJianrong
    Sep 15, 2015❤️1
    7
    @dimingxuan 好吧那大概不行……
    exch4nge
    exch4nge
    Sep 16, 2015❤️1
    8
    如果你要有完整支持的话,还是比较恶心的,你得做 preprocessor ,用词法语法分析器(可选 flex/bison (lex/yacc ))分析出 AST ,然后找出所哦有函数定义,做到你要的功能……

    想通过简单的方式的话,你得忍受很多特殊情况不支持的问题,可以不用 preprocessor 、语法分析器,只用词法分析器+自己实现的只能满足你的要求的语法分析器来做……

    更想简单更省事的话,可以像楼上说的那样,用 ctags 或 cscope 获取到所有函数定义所在的位置,然后自己去分析出参数名字
    dimingxuan
    dimingxuan
    OP
    Sep 16, 2015
    9
    @exch4nge 谢谢,看来坑很深啊, preprocessor 做起来挺费劲的吧,没有接触过相关的知识
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1313 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 20:51 · PVG 04:51 · LAX 12:51 · JFK 15:51
    ♥ Do have faith in what you're doing.