pycparser is a parser for the C language, written in pure Python. It is a module designed to be easily integrated into applications that need to parse C source code.
Anything that needs C code to be parsed. The following are some uses for pycparser, taken from real user reports:
One of the most popular uses of pycparser is in the 1.3 Which version of C does pycparser support?
pycparser aims to support the full C99 language (according to the standard ISO/IEC 9899). Some features from C11 are also supported, and patches to support more are welcome.
pycparser supports very few GCC extensions, but it's fairly easy to set things up so that it parses code with a lot of GCC-isms successfully. See the 1.4 What grammar does pycparser follow?
pycparser very closely follows the C grammar provided in Annex A of the C99 standard (ISO/IEC 9899).
For reporting problems with pycparser or submitting feature requests, please open an 2 Installing
pycparser/ply. The current PLY version is
3.10, retrieved from 2.2 Installation process
Installing pycparser is very simple. Once you download and unzip the
package, you just have to execute the standard python setup.py install. The
setup script will then place the pycparser module into site-packages in
your Python's installation library.
Alternatively, since pycparser is listed in the 2.3 Known problems
.pyc files from the older version. If
you see unexplained errors from pycparser after an upgrade, remove it (by
deleting the pycparser directory in your Python's site-packages, or
wherever you installed it) and install again.In order to be compilable, C code must be preprocessed by the C preprocessor -
cpp. cpp handles preprocessing directives like #include and
#define, removes comments, and performs other minor tasks that prepare the C
code for compilation.
For all but the most trivial snippets of C code pycparser, like a C
compiler, must receive preprocessed C code in order to function correctly. If
you import the top-level parse_file function from the pycparser package,
it will interact with cpp for you, as long as it's in your PATH, or you
provide a path to it.
Note also that you can use gcc -E or clang -E instead of cpp. See
the using_gcc_E_libc.py example for more details. Windows users can download
and install a binary build of Clang for Windows 3.2 What about the standard C library headers?
C code almost always #includes various header files from the standard C
library, like stdio.h. While (with some effort) pycparser can be made to
parse the standard headers from any C compiler, it's much simpler to use the
provided "fake" standard includes in utils/fake_libc_include. These are
standard C header files that contain only the bare necessities to allow valid
parsing of the files that use them. As a bonus, since they're minimal, it can
significantly improve the performance of parsing large C files.
The key point to understand here is that pycparser doesn't really care about the semantics of types. It only needs to know whether some token encountered in the source is a previously defined type. This is essential in order to be able to parse C correctly.
See #224).
Take a look at the examples directory of the distribution for a few examples
of using pycparser. These should be enough to get you started. Please note
that most realistic C code samples would require running the C preprocessor
before passing the code to pycparser; see the previous sections for more
details.
The public interface of pycparser is well documented with comments in
pycparser/c_parser.py. For a detailed overview of the various AST nodes
created by the parser, see pycparser/_c_ast.cfg.
There's also a email for help.
There are a few points to keep in mind when modifying pycparser:
_c_ast.cfg, by _ast_gen.py. If you modify the AST
configuration, make sure to re-generate the code.CParser class. For
development you should create the parser without optimizations, so that it
will regenerate the Yacc and Lex tables when you change the grammar.Once you unzip the pycparser package, you'll see the following files and
directories:
Some people have contributed to pycparser by opening issues on bugs they've found and/or submitting patches. The list of contributors is in the CONTRIBUTORS file in the source distribution. After pycparser moved to Github I stopped updating this list because Github does a much better job at tracking contributions.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。