how to make the Pycparser to understand my c function

1.1k views Asked by At

I am using the pycparser for parsing my C code. When I run the script, pycparser could not understand the function and it throws an error like below.

File "C:\Python27\lib\site-packages\pycparser\__init__.py", line 93, in parse_file
return parser.parse(text, filename)
File "C:\Python27\lib\site-packages\pycparser\c_parser.py", line 124, in parse
return self.cparser.parse(text, lexer=self.clex, debug=debuglevel)
File "C:\Python27\lib\site-packages\pycparser\ply\yacc.py", line 265, in parse
return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
File "C:\Python27\lib\site-packages\pycparser\ply\yacc.py", line 1047, in parseopt_notrack
  tok = self.errorfunc(errtoken)
File "C:\Python27\lib\site-packages\pycparser\c_parser.py", line 1423, in p_error
  column=self.clex.find_tok_column(p)))
File "C:\Python27\lib\site-packages\pycparser\plyparser.py", line 54, in _parse_error
  raise ParseError("%s: %s" % (coord, msg))
ParseError: dsc.c:2592:1: before: {

The line number it shows is nothing but an function like this

void dsc (void)
{

Can anyone tell how to make the pycparser to undertand my function?

 static void dsc (void)
 {
 UINT8 j, i;
 static UINT16 jump;
 for (j = 0; j< 10; j++)
 {
 jump = dsc_jump

 for
  (i = 1; i < 10; i++)
 {
 if
 (
 ((jump & 0x50 != 0)
  )
 {
 jump  = dsc_jump
  }
  }

  }
1

There are 1 answers

0
Eli Bendersky On

Your function isn't valid C code. I always suggest to run pycparser only on code that you know compiles. pycparser's error messages are not as good as Clang's or gcc's, so it's harder to figure out where the error is. If I compile your function with gcc, for example, I get:

static void dsc (void)
 {
 UINT8 j, i;
 static UINT16 jump;
 for (j = 0; j< 10; j++)
 {
 jump = dsc_jump

 for
  (i = 1; i < 10; i++)
 {
 if
 (
 ((jump & 0x50 != 0)
  )
 {
 jump  = dsc_jump
  }
  }

  }

UINT8 is a type you need to define in fake headers, and pycparser would not care about the undefinedness of dsc_jump, but the other errors are real problems:

  1. There's no semicolon after dsc_jump
  2. The if condition has an unterminated (

etc.