1

I'm trying to parse this fragment of C code:

void foo(const int *bar, int * const baz);

using GnuCParser, part of pycparserext.

Based on this answer I expected to see some PtrDecls, but here's what I get from ast.show() on the resulting parse tree:

FileAST:
  Decl: foo, [], [], []
    FuncDecl:
      ParamList:
        Decl: bar, ['const'], [], []
          TypeDeclExt: bar, ['const']
            IdentifierType: ['int']
        Decl: baz, [], [], []
          TypeDeclExt: baz, []
            IdentifierType: ['int']
      TypeDecl: foo, []
        IdentifierType: ['void']

Notice how baz, a "const pointer to int", doesn't have a trace of const (or "pointeryness") in the data printed by ast.show(). Is this difference because of GnuCParser?

How do I figure out the type of baz from the AST? My actual C code needs the GNU parser. I'm using pycparserext version 2016.1.

UPDATE: I've created a pycparserext issue on GitHub.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606

2 Answers2

1

I'm not sure what's the issue with pycparserext here, but if I run it through the vanilla pycparser I get:

FileAST: 
  Decl: foo, [], [], []
    FuncDecl: 
      ParamList: 
        Decl: bar, ['const'], [], []
          PtrDecl: []
            TypeDecl: bar, ['const']
              IdentifierType: ['int']
        Decl: baz, [], [], []
          PtrDecl: ['const']
            TypeDecl: baz, []
              IdentifierType: ['int']
      TypeDecl: foo, []
        IdentifierType: ['void']

Which looks entirely reasonable for this input.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • Right, that's along the lines of what I was expecting. It seems it's something with GnuCParser, then. Strange. – unwind Oct 18 '16 at 08:13
1

This was due to a bug in pycparserext, it has been fixed now by the maintainer and the issue mentioned in the question has been closed. The fix is in release 2016.2.

The output is now:

>>> from pycparserext.ext_c_parser import GnuCParser
>>> p=GnuCParser()
>>> ast=p.parse("void foo(const int *bar, int * const baz);")
>>> ast.show()
FileAST:
  Decl: foo, [], [], []
    FuncDecl:
      ParamList:
        Decl: bar, ['const'], [], []
          PtrDecl: []
            TypeDecl: bar, ['const']
              IdentifierType: ['int']
        Decl: baz, [], [], []
          PtrDecl: ['const']
            TypeDecl: baz, []
              IdentifierType: ['int']
      TypeDecl: foo, []
        IdentifierType: ['void']

Clearly this contains more Ptr nodes, which is a very good sign. The pycparserext code has also had a test added to catch this in the future.

Very impressive. :)

unwind
  • 391,730
  • 64
  • 469
  • 606