• Stars
    star
    345
  • Rank 122,468 (Top 3 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 13 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C parser and interpreter written in Python with automatic ctypes interface generation

PyCParser

https://github.com/albertz/PyCParser

A C parser and interpreter written in Python. Also includes an automatic ctypes interface generator.

It is looser than the C grammar, i.e. it should support a superset of the C language in general.

Some of the support may a bit incomplete or wrong at this point because I didn't really strictly followed the language specs but rather improved the parser by iteration on real-world source code.

Similar projects

Parsers / ctypes interface generators:

  • Eli Bendersky's pycparser. Complete C99 parser in pure Python. It depends on Python Lex-Yacc (PLY). (I didn't tested it yet. Seems to be the most complete and most professional project. If you don't want a C interpreter, this is probably the project you should use.)
  • pyclibrary (Github fork). Is quite slow and didn't worked that well for me.
  • ctypesgen. Also uses Lex+Yacc.
  • codegen. Uses GCC-XML. See below about the disadvantages of such an aproach.

Interpreters:

  • CInterpreter. Python.
  • CINT. Not in Python. Probably the most famous one.
  • Ch. Not in Python. Is not really free.
  • ups debugger. Not in Python.
  • PicoC. Not in Python. "A very small C interpreter."
  • BIC. Not in Python.

Why this project?

  • Be more flexible. It is much easier now with a hand-written parser to do operations on certain levels of the parsing pipe.
  • I wanted to have some self-contained code which can also easily run on the end-user side. So the end-user can just update the lib and its headers and then some application using this Python lib will automatically use the updated lib. This is not possible if you generated the ctypes interface statically (via some GCC-XML based tool or so).
  • I wanted to implement PySDL and didn't wanted to translate the SDL headers by hand. Also, I didn't wanted to use existing tools to do this to avoid further maintaining work at some later time. See the project for further info.
  • This functionality could be used similarly for many other C libraries.
  • A challenge for myself. Just for fun. :)

Examples

  • PySDL. Also uses the automatic ctypes wrapper and maps it to a Python module.
  • PyCPython. Interpret CPython in Python.
  • PyLua. Interpret Lua in Python.

Also see the tests/test_interpreter.{c,py} 'Hello world' example.

Also try out ./demos/interactive_interpreter.py --debug.

Current state

  • Many simple C programs should be parsed and interpret correctly now.
  • I'm quite sure that function pointer typedefs are handled incorrectly. E.g. typedef void f(); and typedef void (*f)(); are just the same right now. See cpre3_parse_typedef and do some testing if you want to fix this.
  • Many functions from the standard C library are still missing.
  • There might be some bugs. :)
  • C++ isn't supported yet. :)
  • The code style does not conform to PEP8 and standard Python conventions in many places, as it is quite old. Also, it probably should be restructured, as it has grown too much in single files. I'm slowly fixing this.

How does the interpreter work

This is probably a bit unusual. We wrap the most important standard C library functions directly to the native libc, via ctypes. We translate the parsed C code to a equivalent Python AST (via ast), which makes heavy use of ctypes. Then we just run this generated Python code. But we can also dump it. Thus we can compile C code to an equivalent Python program.

demo

--- Albert Zeyer, http://www.az2000.de

More Repositories

1

music-player

Music player - endlessly plays your music
Python
489
star
2

openlierox

OpenLieroX - Liero clone / Worms realtime / 2D shooter
C++
261
star
3

mouse-scroll-wheel-acceleration-userspace

Mouse scroll wheel acceleration in user space
Python
129
star
4

pydbattach

attach to running Python process
Python
105
star
5

python-embedded

Python build script to create a small embeddable static Python lib
C
82
star
6

music-player-core

Music player core Python module
C++
75
star
7

RandomFtpGrabber

Random FTP grabber - downloads all the interesting stuff
Python
59
star
8

playground

some space to try things out
Python
50
star
9

py_better_exchook

nice Python exception hook replacement
Python
40
star
10

commandergenius

Commander Genius - Commander Keen compatible clone -- main repo here: https://github.com/gerstrong/Commander-Genius
C++
40
star
11

python-preloaded

Bundle Python executable with preloaded modules
Python
40
star
12

Pyjector

inject PyTerminal into any running application via SIMBL
Objective-C
36
star
13

simbl

SIMBL is the SIMple Bundle Loader.
C
34
star
14

helpers

some small helper tools
Python
34
star
15

timecapture

captures your current topmost application and the URL/file with timestamps
Python
32
star
16

PySDL

PySDL is a pure Python SDL binding via on-the-fly ctypes generation
Python
29
star
17

wiki

some useful information
28
star
18

PythonHotswap

Hotswap Python functions. And persistence of runtime.
Python
28
star
19

grub-fuse

GRUB filesystem support <-> FUSE bridge
C
26
star
20

Xee

lightweight, fast and convenient image viewer and browser for MacOSX
C
24
star
21

png-db

Database for PNG images
C
23
star
22

dotfiles

my dotfiles
Shell
23
star
23

google-books-export

export PDF from Google Books preview
Python
21
star
24

Coding-Game-Intro

Coding Introduction / Tutorial, mostly by writing small little fun games
21
star
25

PyTerminal

Python Terminal framework - embed a Python GUI Terminal in your app
Objective-C
20
star
26

chrome-ext-google-takeout-downloader

JavaScript
19
star
27

learn-midi

Learn-midi learns to procude MIDI output based on any audio input
Python
17
star
28

background-zmq-ipython

Background ZMQ IPython/Jupyter kernel
Python
17
star
29

PyCPython

interpret CPython in pure Python
Python
16
star
30

IdeaResolveSymlinks

Java
14
star
31

mac-suppress-mouse-keyboard

Suppress mouse & keyboard events on MacOSX. Baby-proof my Mac!
Objective-C
14
star
32

PyLua

interpret Lua (the C source code) in pure Python with PyCParser
C
14
star
33

binstruct

binary structure serialization (like binary JSON or BSON) with cryptography
Python
10
star
34

FScriptAnywhereSIMBL

FScript Anywhere via SIMBL
Objective-C
10
star
35

RemoteControl

secure remote control of your computer from your iPhone
Python
10
star
36

PyOverheadGame

simple Python game with 2D top-down perspective
Scilab
9
star
37

smf-openid-server

SMF OpenID server
PHP
9
star
38

navit

Navit is a car navigation system with routing engine
C
9
star
39

planet_wars-cpp

Google AI challenge: Planet Wars game engine and co, C++ implementation
C++
9
star
40

timidity-macosx

TiMidity++ for MacOSX
C
8
star
41

pyclibrary

pure-python C parser + ctypes automation library
Python
8
star
42

literal-python-to-pickle

Convert literal Python code to binary Pickle -- faster ast.literal_eval
C++
7
star
43

backup_system

Python
6
star
44

TeXShop

Objective-C
5
star
45

system-tools

Python
5
star
46

pynsh

the Python dynamic shell
JavaScript
4
star
47

AutoSleep

automatically suspend not-needed processes - safe battery
Python
4
star
48

iphone-ssh-relay

iPhone port/SSH relay daemon and client for USB connections
C
4
star
49

CPython

fork of CPython - to try out own hacks
Python
4
star
50

chromehacking

Chrome hacking
Python
4
star
51

screenshooting

scripts for handling screenshots and analyzing them
Python
4
star
52

textmate

TextMate is a graphical text editor for OS X 10.7+
C
4
star
53

foreground_app_info

get details about the application and opened url which is in foreground
Python
3
star
54

ChromeWebApps

turn a website into a native-like app on your Mac
Python
3
star
55

SaveTheRock

Save The Rock! is a 2D platformer bundled with a user-friendly in-game level editor
C++
3
star
56

py-tui-editor

Simple Python terminal (TUI) multi-line editor
Python
3
star
57

iphone-backup

iPhone backup scripts
Shell
3
star
58

intelligent-patcher

scripts to patch source code in an intelligent way
Python
3
star
59

clang

copy of git://repo.or.cz/clang.git
C++
3
star
60

WebAudioPlayer

audio player in HTML/JS
JavaScript
3
star
61

getraenke

Getränkeabrechnungssystem fürs Wohnheim
Python
3
star
62

9eyes-fetcher

fetches all images from 9-eyes.com
Python
3
star
63

ImageViewer

view images
C++
2
star
64

play-with-the-brain

PyBrain experiments
Python
2
star
65

openlierox-distrib

OpenLieroX - distrib scripts & files
C++
2
star
66

planetwars

PlanetWars Google AI Challenge 2010
Python
2
star
67

reiserfs-fuse

ReiserFS for FUSE
C
2
star
68

PictureSlider

MacOSX picture screen saver
Objective-C
2
star
69

turingmachine

Non-deterministic Turing machine simulator framework
C++
2
star
70

MathExercisesJavaAppletBase

Java (Applet) code base for math exercises
Java
2
star
71

zinssaetze

Python
2
star
72

tor

C
2
star
73

automatic_object_locking

demonstrating how the GIL could be removed in Python
C++
2
star
74

google-contacts-sync

Google contact syncing and merging
Python
2
star
75

diplom-thesis-automata

Diplom thesis for Computer Science in Automata theory
TeX
1
star
76

memos

manage memos
Python
1
star
77

ParseOggVorbis

C++ OGG Vorbis decoder
C
1
star
78

errordb

error message database - with list of solutions to each error
1
star
79

live-helper

Python
1
star
80

PyJitDev

Just-in-Time Python development
Python
1
star
81

ffmpeg

build tools, Xcode project
C
1
star
82

google-breakpad

OpenLieroX Google Breakpad copy
C++
1
star
83

diplom-thesis-math

Diplom thesis for Mathematics
Python
1
star
84

sdl

copy of SDL svn trunk
C
1
star
85

itunes-scripts

some simple Python scripts for iTunes and the iTunes music library
Python
1
star
86

streamplay

Objective-C
1
star
87

Sport-RWTH-Aachen

exaktes Kursprogramm (nach Zeit) von Sportkursen vom Hochschulsport der RWTH Aachen
1
star