2

I have this example c file I want to parse:

StrcutWithinStruct.c
// simple struct within a struct example

struct A {
 int a;
};

struct B {
 A a;
 int b;
};

I'm running pcyparser to parse it, with the following code

exploreStruct.py
#parse StructWithinStruct

from pycparser import parse_file
ast = parse_file(filename='..\StructWithinStruct.c')
ast.show()

As a result, I got the following:

Tracback (most recent call last):
  File "exploreStruct.py", line 3, in <module>
   ast = parse_file(filename='...\StructWithinStruct.c')
  File "D:\...\pycparser\__init__.py", line 93, in parse_file
   return parser.parse(text,filename)
  File "D:\...\pycparser\c_parser.py", line 146, in parse
   debug=debug_level)
  File "D:\...\pycparser\yacc.py", line 331, in parse
   return self.parseropt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "D:\...\pycparser\yacc.py", line 1181, in parseropt_notrack
   tok=call_errorfunc(self.errorfunc, errtoken, self)
  File "D:\...\pycparser\yacc.py", line 193, in call_errorfunc
   r=errorfunc(token)
  File "D:\...\pycparser\c_parser.py", line 1699, in p_error
   column=self.clex.find_tok_column(p)))
  File "D:\...\pycparser\plyparser.py", line 55, in _parse_error
   raise ParseError("%s: %s % (coord, msg))
pycparser.plyparser.ParserError: D:...\StructWithinStruct.c:7:2: Before A

So, is pycparser can handle struct within struct, or not? I thought this is some basic requirement, so I'm pretty sure that the problem lying in my configuration somewhere...

One more thing: I know that pcypareser author, @Eli Bendersky, says that one should use Clang to parse C++, but I will like to know if there's another option nowadays to parse C++ (preferably over Python), and is user-friendly.

Thanks.

1 Answer 1

4

Your struct declarations are not closed with a semicolon:

Additionally A itself is not a type name in C. In C++ A alone would suffice, but in C you need to add the struct keyword.

struct A {
 int a;
};

struct B {
 struct A a;
 int b;
};

Or, you can declare a synonym with a typedef keyword:

struct A {
 int a;
};

typedef struct A A;

or, shorter:

typedef struct A {
 int a;
} A;

From that point the declaration

A a;

should compile properly.

Sign up to request clarification or add additional context in comments.

2 Comments

The semicolon was a typo over here. and existed in the original code. +1 on the difference between C to C++, and I guess that's why I asked about parsing C++ as well... I'm working with C++, but didn't know there's actual difference in the way you declare your structs as a type name...
@BakItzik In C the struct types is a separate space of identifiers, so you must explicitly state is when referring to a type. In C++, however, struct is equivalent to a class, just with the default initial inheritance type private replaced with public. So the type declared with the struct keyword in C++ becomes a globally available type name in the same manner as those declared as class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.