Is there a known implementation of the C pre-processor tool implemented either in Python or JavaScript? I am looking for a way to robustly pre-process C (and C like) source code and want to be able to process, for example, conditional compilation and macros without invoking an external CPP tool or native code library. Another potential use case is pre-processing within a web application, within the web browser.
So far, I have found implementations in Java, Perl, and of course, C and C again. It may be plausible to use one of the C to JavaScript compilers now becoming available.
The PLY (Python Lex and Yacc) tools include a cpp implemented in Python.
#Macros for logical operators import sys #Macro definitions #in python, function definitions are used as macros #There is no preprocessors in python #Variable Initialization a = int(raw_input("Enter Three Numbers : ")) b = int(raw_input("Enter Three Numbers : ")) c = int(raw_input("Enter Three Numbers : ")) if a > b ...
The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control. In many C implementations, it is a separate program invoked by the compiler as the first part of translation.
The C Preprocessor. The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.
Preprocessor directives are lines of the source file where the first non-whitespace character is # , which distinguishes them from other lines of text. The effect of each preprocessor directive is a change to the text and the result is a transformation of the text that does not contain the directives nor comments.
The Firefox build system does something similar for its chrome. A source file named jar.mn lists all the XUL/JS files needed, and optionally these files are preprocessed before they are shipped. Thus the script that ships the files also invokes a mini-preprocessor.
Check out PLY, an implementation of Lex and Yacc on Python which contains a cpp tool implemented in python using the PLY lexer. There is also pycparser, a parser built on top of PLY for the C language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With