1

I am trying to parse the syntax of a C file using pycparser. I send the C file through a preprocessor and then send the output of the preprocessor to be parsed by pycparser. The following code is in one of the C files ...

extern "asm"
{
    extern void ASM_Function(void);
}

pycparser throws and exception telling me this is not valid C syntax. Looking at the C BNF the keyword extern does not allow a string literal to precede it. I am correct in reading the BNF? Was this extern functionality added in a later version of C or is this syntax compiler specific?

Stefan Bossbaly
  • 6,682
  • 9
  • 53
  • 82
  • 4
    Are you sure it isn't [C++ code](http://en.cppreference.com/w/cpp/language/language_linkage)? However there could be a C compiler with such an extension. – cremno Jun 22 '15 at 21:35
  • Doesn't asm stand for assembly level code? `asm volatile(@assembler funtion);`? – DarthSpeedious Jun 22 '15 at 21:39
  • @cremno The compiler program supports both C and C++ code, but I specified a runtime flag only use the C compiler. It might be an extension that allows the programmer to link assembly in – Stefan Bossbaly Jun 22 '15 at 21:45

1 Answers1

0

It looks like a compiler extension. Do you know what compiler the code was originally written for?

Most compilers support declaring a C calling convention by wrapping the function declaration with an:

#ifdef __cplusplus
extern "C" {
#endif

    // functions that use C calling convention.
    // are declared here.

#ifdef __cplusplus
} /* extern "C" */
#endif

The code appears to be declaring an externally defined assembly function called ASM_Function. You may be able to rewrite this if you know what is the calling convention the assembly function is expecting.

extern "C" is a C++ construct to declare functions that will not use name mangling and will use the cdecl calling convention.

EDIT: Corrected my post.

Man Vs Code
  • 1,058
  • 10
  • 14
  • 2
    `extern "C"` is a C++ thing. Header files used with both C and C++ compilers use an ifdef, e.g. `#ifdef __cplusplus` to hide the `extern "C"` from the C compiler. – user3386109 Jun 22 '15 at 22:35
  • Yes, that is correct but C headers that want to be compatible with a C++ compiler still work with C compilers. Modern C compilers are aware of this in their grammars. – Man Vs Code Jun 22 '15 at 22:41
  • @ManVsCode: The thing is that preprocessor hides `extern "C"`, so it's not even visible by a C compiler, that would crash badly with `extern "C"`. – Grzegorz Szpetkowski Jun 22 '15 at 22:46
  • 1
    Ah yes. That's right! I forgot about the #ifdef around it. – Man Vs Code Jun 22 '15 at 22:50
  • @ManVsCode can you give us an example of how this would work? i usually use `asm volatile(@assembler funtion);` if i have to use assembly instruction. – DarthSpeedious Jun 23 '15 at 09:19
  • 2
    @DarthSpeedious There are two ways to mix C and Assembly. The first method is to inline your assembly code inside a C function. This depends on using special compiler extensions and how this is done varies from compiler to compiler. The second way is to compile your assembly code using the same binary format your C binary will use (ELF, COFF, et cetera), link your assembly object with your C objects, and, finally, call it from your C code using the correct calling convention that your assembly code is written for. – Man Vs Code Jun 26 '15 at 16:44