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

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

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