albertz / pycparser Goto Github PK
View Code? Open in Web Editor NEWC parser and interpreter written in Python with automatic ctypes interface generation
License: BSD 2-Clause "Simplified" License
C parser and interpreter written in Python with automatic ctypes interface generation
License: BSD 2-Clause "Simplified" License
I'd rather not use tabs for indent, but four spaces.
as the title says
I am not familiar with git, I only know how to operate with the github web interface
so please read it at https://github.com/retsyo/albertz_PyCParser_Windows
and test_interpreter.py is in fact in directory demos as your original one
Consider the below structure,
typedef struct
{
int number;
}Number;
struct XYZ
{
Number Number[10];
}xyz;
PyCParser is not able to parse the 2nd structure, its complaining about Number[10]. The above is a valid C declaration.
Octal (\NNN) and hex escape (\xNN) sequences in char and string declarations require further support.
Test C code:
#include <stdio.h>
#define NUM_TEST_SINGLE_CHAR 19
char test_single_char[NUM_TEST_SINGLE_CHAR] = {
'\0', // octal
'\00', // octal
'\000',// octal
'\x0', // hex
'\x00',// hex
'\1', // octal
'\01', // octal
'\001',// octal
'\x1', // hex
'\x01',// hex
'\t',
'\11', // octal
'\011',// octal
'\x9', // hex
'\x09',// hex
'z',
'\172',// octal
'\x7a',// hex
'\x7A',// hex
};
#define NUM_TEST_STR_SINGLE_CHAR 19
char * test_str_single_char[NUM_TEST_STR_SINGLE_CHAR] = {
"\0", // octal
"\00", // octal
"\000",// octal
"\x0", // hex
"\x00",// hex
"\1", // octal
"\01", // octal
"\001",// octal
"\x1", // hex
"\x01",// hex
"\t",
"\11", // octal
"\011",// octal
"\x9", // hex
"\x09",// hex
"z",
"\172",// octal
"\x7a",// hex
"\x7A",// hex
};
#define NUM_TEST_STR_MULTI_CHAR 13
char * test_str_multi_char[] = {
"Tab\tGap",
"Tab\11Gap", // octal
"Tab\011Gap", // octal
"Tab\x9Gap", // hex, barely works as G is not a hex character
"Tab\x9" "Gap", // hex, but more clear and less scary
"Tab\x09Gap", // hex, two characters
"Tab\x9" "animals", // hex, restarting the quoting a nescesity
"zzzAre sleepy time",
"z\x7a\x7A" "Are sleepy time", // nescessary restarting of quoting
"z z z Are sleepy time",
"\172 \x7a \x7A Are sleepy time", // no restart of quoting required
"time for zzzz",
"time for \x7a\172\x7Az",
};
int main(){
int i=0;
for (i=0; i<NUM_TEST_SINGLE_CHAR; i++){
// print single in quotes with trailing new line
// \x for hex and %.2x to print in 2 digit hex format
printf("'\\x%.2x'\n", test_single_char[i]);
}
printf("\n");
for (i=0; i<NUM_TEST_STR_SINGLE_CHAR; i++){
// print in double quotes with trailing new line
// \x for hex and %.2x to print in 2 digit hex format
printf("\"\\x%.2x\"\n", *test_str_single_char[i]);
}
printf("\n");
for (i=0; i<NUM_TEST_STR_MULTI_CHAR; i++){
printf("%s\n", test_str_multi_char[i]);
}
return 0;
}
For hex escape sequences this leads to cparser.simple_escape_char being invoked by cpre2_parse() with 'x' as an argument. Hex escape sequences are not of the simple kind that simple_escape_char is designed for. Handling for '\0' and "\0" doesn't recognize that these particular sequences are octal escapes.
Additional states are required in cpre2_parse().
The output of the above should be:
'\x00'
'\x00'
'\x00'
'\x00'
'\x00'
'\x01'
'\x01'
'\x01'
'\x01'
'\x01'
'\x09'
'\x09'
'\x09'
'\x09'
'\x09'
'\x7a'
'\x7a'
'\x7a'
'\x7a'
"\x00"
"\x00"
"\x00"
"\x00"
"\x00"
"\x01"
"\x01"
"\x01"
"\x01"
"\x01"
"\x09"
"\x09"
"\x09"
"\x09"
"\x09"
"\x7a"
"\x7a"
"\x7a"
"\x7a"
Tab Gap
Tab Gap
Tab Gap
Tab Gap
Tab Gap
Tab Gap
Tab animals
zzzAre sleepy time
zzzAre sleepy time
z z z Are sleepy time
z z z Are sleepy time
time for zzzz
time for zzzz
I have some initial code to address the hex escapes in double quoated strings. After this issue is opened I'll reference the issue number.
The following code generates no warnings or errors when compiled with gcc with a large number of warning options, yet pycparser reports a syntax error. #includes omitted.
int main()
{
int i = 5, j = 6, k = 1;
if ((i=j && k == 1) || k > j)
printf("Hello, World\n");
return 0;
}
Please use a top level package instead of putting the package contents directly into the repository.
A real compiler script with input file options as well as options like -I
, -D
and -o
would be great-
We needed this patch to package PyCParser for Fedora; you might to include it:
tests/all_tests.py: add local paths to the front of sys.path
While doing pycparser development on a machine that already has an
older version of pycparser installed, we want unit tests to run against
the local copy instead of the system wide copy of pycparser.
This patch adds '.' and '..' to the front of sys.path instead of the back.
diff --git a/tests/all_tests.py b/tests/all_tests.py
--- a/tests/all_tests.py
+++ b/tests/all_tests.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
import sys
-sys.path.extend(['.', '..'])
+sys.path[0:0] = ['.', '..']
import unittest
Steps:
git clone https://github.com/albertz/PyCParser.git
cd PyCParser
mkvirtualenv -p python2.7 -r ./requirements.txt pycparser
./runcprog.py ./demos/test_interpreter.c
Result:
EXCEPTION
Traceback (most recent call last):
File "./runcprog.py", line 56, in
line: from cparser import State, parse
locals:
cparser =
State =
parse =
File "/home/roba/work/github/PyCParser/cparser.py", line 14, in
line: from .cparser_utils import unicode, long, unichr
locals:
from =
from.cparser_utils =
unicode = <type 'unicode'>
long = <type 'long'>
unichr =
ValueError: Attempted relative import in non-package
Is there something to do before trying to use the runcprog.py script ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.