pycparser AST 节点 pointers/locations

pycparser AST node pointers/locations

我是解析方面的新手,我正在尝试使用 Pycparser 来解析我的 c 文件,我通过预处理文件 gcc -E 按照说明进行操作,然后我使用了示例文件夹中的 using_gcc_E_libc.py 并通过了我的文件,我得到了一些输出,但不确定 AST 是否是这样的? 如果那是正确的 AST,我如何找到每个节点及其依赖项和该节点的指针(位置)?

FileAST: 


Typedef: size_t, [], ['typedef']
    TypeDecl: size_t, []
      IdentifierType: ['int']
  Typedef: __builtin_va_list, [], ['typedef']
    TypeDecl: __builtin_va_list, []
      IdentifierType: ['int']
  Typedef: __gnuc_va_list, [], ['typedef']
    TypeDecl: __gnuc_va_list, []
      IdentifierType: ['int']
  Typedef: va_list, [], ['typedef']
    TypeDecl: va_list, []
      IdentifierType: ['int']
  Typedef: __int8_t, [], ['typedef']
    TypeDecl: __int8_t, []
      IdentifierType: ['int']
  Typedef: __uint8_t, [], ['typedef']
    TypeDecl: __uint8_t, []
      IdentifierType: ['int']
  Typedef: __int16_t, [], ['typedef']
    TypeDecl: __int16_t, []
      IdentifierType: ['int']
  Typedef: __uint16_t, [], ['typedef']
    TypeDecl: __uint16_t, []
      IdentifierType: ['int']
  Typedef: __int_least16_t, [], ['typedef']
    TypeDecl: __int_least16_t, []
      IdentifierType: ['int']
  Typedef: __uint_least16_t, [], ['typedef']
    TypeDecl: __uint_least16_t, []
      IdentifierType: ['int']
  Typedef: __int32_t, [], ['typedef']
    TypeDecl: __int32_t, []
      IdentifierType: ['int']
  Typedef: __uint32_t, [], ['typedef']
    TypeDecl: __uint32_t, []
      IdentifierType: ['int']
  Typedef: __int64_t, [], ['typedef']
    TypeDecl: __int64_t, []
      IdentifierType: ['int']
  Typedef: __uint64_t, [], ['typedef']
    TypeDecl: __uint64_t, []
      IdentifierType: ['int']
  Typedef: __int_least32_t, [], ['typedef']
    TypeDecl: __int_least32_t, []
      IdentifierType: ['int']
  Typedef: __uint_least32_t, [], ['typedef']
    TypeDecl: __uint_least32_t, []
      IdentifierType: ['int']
  Typedef: __s8, [], ['typedef']
    TypeDecl: __s8, []
      IdentifierType: ['int']
  Typedef: __u8, [], ['typedef']
    TypeDecl: __u8, []
      IdentifierType: ['int']
  Typedef: __s16, [], ['typedef']
    TypeDecl: __s16, []
      IdentifierType: ['int']
  Typedef: __u16, [], ['typedef']
    TypeDecl: __u16, []
      IdentifierType: ['int']
  Typedef: __s32, [], ['typedef']
    TypeDecl: __s32, []
      IdentifierType: ['int']
  Typedef: __u32, [], ['typedef']
    TypeDecl: __u32, []
      IdentifierType: ['int']
  Typedef: __s64, [], ['typedef']
    TypeDecl: __s64, []
      IdentifierType: ['int']
  Typedef: __u64, [], ['typedef']
    TypeDecl: __u64, []
      IdentifierType: ['int']
  Typedef: _LOCK_T, [], ['typedef']
    TypeDecl: _LOCK_T, []
      IdentifierType: ['int']
  Typedef: _LOCK_RECURSIVE_T, [], ['typedef']
    TypeDecl: _LOCK_RECURSIVE_T, []
      IdentifierType: ['int']
  Typedef: _off_t, [], ['typedef']
    TypeDecl: _off_t, []
      IdentifierType: ['int']
  Typedef: __dev_t, [], ['typedef']
    TypeDecl: __dev_t, []
      IdentifierType: ['int']
  Typedef: __uid_t, [], ['typedef']
    TypeDecl: __uid_t, []
      IdentifierType: ['int']
  Typedef: __gid_t, [], ['typedef']
    TypeDecl: __gid_t, []
      IdentifierType: ['int']
  Typedef: _off64_t, [], ['typedef']
    TypeDecl: _off64_t, []
      IdentifierType: ['int']
  Typedef: _fpos_t, [], ['typedef']
    TypeDecl: _fpos_t, []
      IdentifierType: ['int']
  Typedef: _ssize_t, [], ['typedef']
    TypeDecl: _ssize_t, []
      IdentifierType: ['int']
  Typedef: wint_t, [], ['typedef']
    TypeDecl: wint_t, []
      IdentifierType: ['int']
  Typedef: _mbstate_t, [], ['typedef']
    TypeDecl: _mbstate_t, []
      IdentifierType: ['int']
  Typedef: _flock_t, [], ['typedef']
    TypeDecl: _flock_t, []
      IdentifierType: ['int']
  Typedef: _iconv_t, [], ['typedef']
    TypeDecl: _iconv_t, []
      IdentifierType: ['int']
  Typedef: __ULong, [], ['typedef']
    TypeDecl: __ULong, []
      IdentifierType: ['int']
  Typedef: __FILE, [], ['typedef']
    TypeDecl: __FILE, []
      IdentifierType: ['int']
  Typedef: ptrdiff_t, [], ['typedef']
    TypeDecl: ptrdiff_t, []
      IdentifierType: ['int']
  Typedef: wchar_t, [], ['typedef']
    TypeDecl: wchar_t, []
      IdentifierType: ['int']
  Typedef: __off_t, [], ['typedef']
    TypeDecl: __off_t, []
      IdentifierType: ['int']
  Typedef: __pid_t, [], ['typedef']
    TypeDecl: __pid_t, []
      IdentifierType: ['int']
  Typedef: __loff_t, [], ['typedef']
    TypeDecl: __loff_t, []
      IdentifierType: ['int']
  Typedef: u_char, [], ['typedef']
    TypeDecl: u_char, []
      IdentifierType: ['int']
  Typedef: u_short, [], ['typedef']
    TypeDecl: u_short, []
      IdentifierType: ['int']
  Typedef: u_int, [], ['typedef']
    TypeDecl: u_int, []
      IdentifierType: ['int']
  Typedef: u_long, [], ['typedef']
    TypeDecl: u_long, []
      IdentifierType: ['int']
  Typedef: ushort, [], ['typedef']
    TypeDecl: ushort, []
      IdentifierType: ['int']
  Typedef: uint, [], ['typedef']
    TypeDecl: uint, []
      IdentifierType: ['int']
  Typedef: clock_t, [], ['typedef']
    TypeDecl: clock_t, []
      IdentifierType: ['int']
  Typedef: time_t, [], ['typedef']
    TypeDecl: time_t, []
      IdentifierType: ['int']
  Typedef: daddr_t, [], ['typedef']
    TypeDecl: daddr_t, []
      IdentifierType: ['int']
  Typedef: caddr_t, [], ['typedef']
    TypeDecl: caddr_t, []
      IdentifierType: ['int']
  Typedef: ino_t, [], ['typedef']
    TypeDecl: ino_t, []
      IdentifierType: ['int']
  Typedef: off_t, [], ['typedef']
    TypeDecl: off_t, []
      IdentifierType: ['int']
  Typedef: dev_t, [], ['typedef']
    TypeDecl: dev_t, []
      IdentifierType: ['int']
  Typedef: uid_t, [], ['typedef']
    TypeDecl: uid_t, []
      IdentifierType: ['int']
  Typedef: gid_t, [], ['typedef']
    TypeDecl: gid_t, []
      IdentifierType: ['int']
  Typedef: pid_t, [], ['typedef']
    TypeDecl: pid_t, []
      IdentifierType: ['int']
  Typedef: key_t, [], ['typedef']
    TypeDecl: key_t, []
      IdentifierType: ['int']
  Typedef: ssize_t, [], ['typedef']
    TypeDecl: ssize_t, []
      IdentifierType: ['int']
  Typedef: mode_t, [], ['typedef']
    TypeDecl: mode_t, []
      IdentifierType: ['int']
  Typedef: nlink_t, [], ['typedef']
    TypeDecl: nlink_t, []
      IdentifierType: ['int']
  Typedef: fd_mask, [], ['typedef']
    TypeDecl: fd_mask, []
      IdentifierType: ['int']
  Typedef: _types_fd_set, [], ['typedef']
    TypeDecl: _types_fd_set, []
      IdentifierType: ['int']
  Typedef: clockid_t, [], ['typedef']
    TypeDecl: clockid_t, []
      IdentifierType: ['int']
  Typedef: timer_t, [], ['typedef']
    TypeDecl: timer_t, []
      IdentifierType: ['int']
  Typedef: useconds_t, [], ['typedef']
    TypeDecl: useconds_t, []
      IdentifierType: ['int']
  Typedef: suseconds_t, [], ['typedef']
    TypeDecl: suseconds_t, []
      IdentifierType: ['int']
  Typedef: FILE, [], ['typedef']
    TypeDecl: FILE, []
      IdentifierType: ['int']
  Typedef: fpos_t, [], ['typedef']
    TypeDecl: fpos_t, []
      IdentifierType: ['int']
  Typedef: cookie_read_function_t, [], ['typedef']
    TypeDecl: cookie_read_function_t, []
      IdentifierType: ['int']
  Typedef: cookie_write_function_t, [], ['typedef']
    TypeDecl: cookie_write_function_t, []
      IdentifierType: ['int']
  Typedef: cookie_seek_function_t, [], ['typedef']
    TypeDecl: cookie_seek_function_t, []
      IdentifierType: ['int']
  Typedef: cookie_close_function_t, [], ['typedef']
    TypeDecl: cookie_close_function_t, []
      IdentifierType: ['int']
  Typedef: cookie_io_functions_t, [], ['typedef']
    TypeDecl: cookie_io_functions_t, []
      IdentifierType: ['int']
  Typedef: div_t, [], ['typedef']
    TypeDecl: div_t, []
      IdentifierType: ['int']
  Typedef: ldiv_t, [], ['typedef']
    TypeDecl: ldiv_t, []
      IdentifierType: ['int']
  Typedef: lldiv_t, [], ['typedef']
    TypeDecl: lldiv_t, []
      IdentifierType: ['int']
  Typedef: sigset_t, [], ['typedef']
    TypeDecl: sigset_t, []
      IdentifierType: ['int']
  Typedef: __sigset_t, [], ['typedef']
    TypeDecl: __sigset_t, []
      IdentifierType: ['int']
  Typedef: _sig_func_ptr, [], ['typedef']
    TypeDecl: _sig_func_ptr, []
      IdentifierType: ['int']
  Typedef: sig_atomic_t, [], ['typedef']
    TypeDecl: sig_atomic_t, []
      IdentifierType: ['int']
  Typedef: __tzrule_type, [], ['typedef']
    TypeDecl: __tzrule_type, []
      IdentifierType: ['int']
  Typedef: __tzinfo_type, [], ['typedef']
    TypeDecl: __tzinfo_type, []
      IdentifierType: ['int']
  Typedef: mbstate_t, [], ['typedef']
    TypeDecl: mbstate_t, []
      IdentifierType: ['int']
  Typedef: sem_t, [], ['typedef']
    TypeDecl: sem_t, []
      IdentifierType: ['int']
  Typedef: pthread_t, [], ['typedef']
    TypeDecl: pthread_t, []
      IdentifierType: ['int']
  Typedef: pthread_attr_t, [], ['typedef']
    TypeDecl: pthread_attr_t, []
      IdentifierType: ['int']
  Typedef: pthread_mutex_t, [], ['typedef']
    TypeDecl: pthread_mutex_t, []
      IdentifierType: ['int']
  Typedef: pthread_mutexattr_t, [], ['typedef']
    TypeDecl: pthread_mutexattr_t, []
      IdentifierType: ['int']
  Typedef: pthread_cond_t, [], ['typedef']
    TypeDecl: pthread_cond_t, []
      IdentifierType: ['int']
  Typedef: pthread_condattr_t, [], ['typedef']
    TypeDecl: pthread_condattr_t, []
      IdentifierType: ['int']
  Typedef: pthread_key_t, [], ['typedef']
    TypeDecl: pthread_key_t, []
      IdentifierType: ['int']
  Typedef: pthread_once_t, [], ['typedef']
    TypeDecl: pthread_once_t, []
      IdentifierType: ['int']
  Typedef: pthread_rwlock_t, [], ['typedef']
    TypeDecl: pthread_rwlock_t, []
      IdentifierType: ['int']
  Typedef: pthread_rwlockattr_t, [], ['typedef']
    TypeDecl: pthread_rwlockattr_t, []
      IdentifierType: ['int']
  Typedef: pthread_spinlock_t, [], ['typedef']
    TypeDecl: pthread_spinlock_t, []
      IdentifierType: ['int']
  Typedef: pthread_barrier_t, [], ['typedef']
    TypeDecl: pthread_barrier_t, []
      IdentifierType: ['int']
  Typedef: pthread_barrierattr_t, [], ['typedef']
    TypeDecl: pthread_barrierattr_t, []
      IdentifierType: ['int']
  Typedef: jmp_buf, [], ['typedef']
    TypeDecl: jmp_buf, []
      IdentifierType: ['int']
  Typedef: rlim_t, [], ['typedef']
    TypeDecl: rlim_t, []
      IdentifierType: ['int']
  Typedef: sa_family_t, [], ['typedef']
    TypeDecl: sa_family_t, []
      IdentifierType: ['int']
  Typedef: sigjmp_buf, [], ['typedef']
    TypeDecl: sigjmp_buf, []
      IdentifierType: ['int']
  Typedef: stack_t, [], ['typedef']
    TypeDecl: stack_t, []
      IdentifierType: ['int']
  Typedef: siginfo_t, [], ['typedef']
    TypeDecl: siginfo_t, []
      IdentifierType: ['int']
  Typedef: z_stream, [], ['typedef']
    TypeDecl: z_stream, []
      IdentifierType: ['int']
  Typedef: int8_t, [], ['typedef']
    TypeDecl: int8_t, []
      IdentifierType: ['int']
  Typedef: uint8_t, [], ['typedef']
    TypeDecl: uint8_t, []
      IdentifierType: ['int']
  Typedef: int16_t, [], ['typedef']
    TypeDecl: int16_t, []
      IdentifierType: ['int']
  Typedef: uint16_t, [], ['typedef']
    TypeDecl: uint16_t, []
      IdentifierType: ['int']
  Typedef: int32_t, [], ['typedef']
    TypeDecl: int32_t, []
      IdentifierType: ['int']
  Typedef: uint32_t, [], ['typedef']
    TypeDecl: uint32_t, []
      IdentifierType: ['int']
  Typedef: int64_t, [], ['typedef']
    TypeDecl: int64_t, []
      IdentifierType: ['int']
  Typedef: uint64_t, [], ['typedef']
    TypeDecl: uint64_t, []
      IdentifierType: ['int']
  Typedef: int_least8_t, [], ['typedef']
    TypeDecl: int_least8_t, []
      IdentifierType: ['int']
  Typedef: uint_least8_t, [], ['typedef']
    TypeDecl: uint_least8_t, []
      IdentifierType: ['int']
  Typedef: int_least16_t, [], ['typedef']
    TypeDecl: int_least16_t, []
      IdentifierType: ['int']
  Typedef: uint_least16_t, [], ['typedef']
    TypeDecl: uint_least16_t, []
      IdentifierType: ['int']
  Typedef: int_least32_t, [], ['typedef']
    TypeDecl: int_least32_t, []
      IdentifierType: ['int']
  Typedef: uint_least32_t, [], ['typedef']
    TypeDecl: uint_least32_t, []
      IdentifierType: ['int']
  Typedef: int_least64_t, [], ['typedef']
    TypeDecl: int_least64_t, []
      IdentifierType: ['int']
  Typedef: uint_least64_t, [], ['typedef']
    TypeDecl: uint_least64_t, []
      IdentifierType: ['int']
  Typedef: int_fast8_t, [], ['typedef']
    TypeDecl: int_fast8_t, []
      IdentifierType: ['int']
  Typedef: uint_fast8_t, [], ['typedef']
    TypeDecl: uint_fast8_t, []
      IdentifierType: ['int']
  Typedef: int_fast16_t, [], ['typedef']
    TypeDecl: int_fast16_t, []
      IdentifierType: ['int']
  Typedef: uint_fast16_t, [], ['typedef']
    TypeDecl: uint_fast16_t, []
      IdentifierType: ['int']
  Typedef: int_fast32_t, [], ['typedef']
    TypeDecl: int_fast32_t, []
      IdentifierType: ['int']
  Typedef: uint_fast32_t, [], ['typedef']
    TypeDecl: uint_fast32_t, []
      IdentifierType: ['int']
  Typedef: int_fast64_t, [], ['typedef']
    TypeDecl: int_fast64_t, []
      IdentifierType: ['int']
  Typedef: uint_fast64_t, [], ['typedef']
    TypeDecl: uint_fast64_t, []
      IdentifierType: ['int']
  Typedef: intptr_t, [], ['typedef']
    TypeDecl: intptr_t, []
      IdentifierType: ['int']
  Typedef: uintptr_t, [], ['typedef']
    TypeDecl: uintptr_t, []
      IdentifierType: ['int']
  Typedef: intmax_t, [], ['typedef']
    TypeDecl: intmax_t, []
      IdentifierType: ['int']
  Typedef: uintmax_t, [], ['typedef']
    TypeDecl: uintmax_t, []
      IdentifierType: ['int']
  Typedef: bool, [], ['typedef']
    TypeDecl: bool, []
      IdentifierType: ['_Bool']
  Typedef: MirEGLNativeWindowType, [], ['typedef']
    PtrDecl: []
      TypeDecl: MirEGLNativeWindowType, []
        IdentifierType: ['void']
  Typedef: MirEGLNativeDisplayType, [], ['typedef']
    PtrDecl: []
      TypeDecl: MirEGLNativeDisplayType, []
        IdentifierType: ['void']
  Typedef: MirConnection, [], ['typedef']
    TypeDecl: MirConnection, []
      Struct: MirConnection
  Typedef: MirSurface, [], ['typedef']
    TypeDecl: MirSurface, []
      Struct: MirSurface
  Typedef: MirSurfaceSpec, [], ['typedef']
    TypeDecl: MirSurfaceSpec, []
      Struct: MirSurfaceSpec
  Typedef: MirScreencast, [], ['typedef']
    TypeDecl: MirScreencast, []
      Struct: MirScreencast
  Typedef: MirPromptSession, [], ['typedef']
    TypeDecl: MirPromptSession, []
      Struct: MirPromptSession
  Typedef: MirBufferStream, [], ['typedef']
    TypeDecl: MirBufferStream, []
      Struct: MirBufferStream
  Typedef: MirPersistentId, [], ['typedef']
    TypeDecl: MirPersistentId, []
      Struct: MirPersistentId
  Typedef: MirBlob, [], ['typedef']
    TypeDecl: MirBlob, []
      Struct: MirBlob
  Typedef: MirDisplayConfig, [], ['typedef']
    TypeDecl: MirDisplayConfig, []
      Struct: MirDisplayConfig
  Typedef: xcb_connection_t, [], ['typedef']
    TypeDecl: xcb_connection_t, []
      Struct: xcb_connection_t
  Typedef: xcb_window_t, [], ['typedef']
    TypeDecl: xcb_window_t, []
      IdentifierType: ['uint32_t']
  Typedef: xcb_visualid_t, [], ['typedef']
    TypeDecl: xcb_visualid_t, []
      IdentifierType: ['uint32_t']
  FuncDef: 
    Decl: main, [], [], []
      FuncDecl: 
        TypeDecl: main, []
          IdentifierType: ['int']
    Compound: 
      Decl: number1, [], [], []
        TypeDecl: number1, []
          IdentifierType: ['int']
      Decl: number2, [], [], []
        TypeDecl: number2, []
          IdentifierType: ['int']
      FuncCall: 
        ID: printf
        ExprList: 
          Constant: string, "Enter two integers: "
      FuncCall: 
        ID: scanf
        ExprList: 
          Constant: string, "%d %d"
          UnaryOp: &
            ID: number1
          UnaryOp: &
            ID: number2
      If: 
        BinaryOp: >=
          ID: number1
          ID: number2
        Compound: 
          If: 
            BinaryOp: ==
              ID: number1
              ID: number2
            Compound: 
              FuncCall: 
                ID: printf
                ExprList: 
                  Constant: string, "Result: %d = %d"
                  ID: number1
                  ID: number2
            Compound: 
              FuncCall: 
                ID: printf
                ExprList: 
                  Constant: string, "Result: %d > %d"
                  ID: number1
                  ID: number2
        Compound: 
          FuncCall: 
            ID: printf
            ExprList: 
              Constant: string, "Result: %d < %d"
              ID: number1
              ID: number2
      Return: 
        Constant: int, 0

这看起来像是一个正确的 AST。顶部的所有 Typedef 节点都来自 pycparser 的 "fake" headers 以帮助它正确解析文件,你可以忽略它们。

AST 只是一个递归的节点树;每个节点都是 Node object. You can iterate its children (which are other nodes), look at its original source location with the .coord attribute, and so on. There are several examples of looking at AST nodes, like explore_ast.

要查找特定节点,您可能需要编写访问者。有几个这样做的例子,比如 func_calls.