I am currently using Python to write a compiler manager. I have to detect whether given source code is C, even if the code has a few syntax errors. I am currently using the file's extension to tell, but I want a smarter option.Would using regex be the best (and simplest) solution to this problem?I am also looking to minimize dependencies. I don't want to use a 3rd party package for this.Any suggestions?
Detect if code is C
python;c
A regex would be the simplest, but not necessarily the best. A regex might be fooled by C-code residing in a comment of another language. It also might miss perfectly valid C-code that doesn't follow the whitespace convention you expect.A C parser, as suggested, is probably the best you can do. There are tools such as ANTLR that can make this job easier for you (ANTLR can generate Python lexer/parsers for C and other languages). Once you've run the C-code through your parser, you can compare the number of syntax errors to the number of valid tokens and size of the C file. Based on these statistics, you can make a guess.As others have noted--you still won't be able to tell the difference between a valid C++ file and a C file with some syntax errors (unless you also run your C-code through a C++ parser, and an Objective-C parser, and...).If you're already making an auto-compiler, your best bet might be to compile the code in all your supported languages and pick the one that actually compiles. If the input file may actually have syntax errors, pick the language that compiles with the fewest syntax errors. You'll still run into the problem where a given input file compiles just fine in multiple languages (as mentioned, C and C++ is one example). You'll end up with multiple compilers that work just fine for the given input file--the only way you'll be able to tell which one to use is by... ...looking at the file extension.