Answer the question
In order to leave comments, you need to log in
Why is it so difficult to implement C?
I don't know C. It's too ugly and uninteresting for me to bring myself to learn it.
But to my cursory glance, C seems to be an extremely simple language. There are not many built-in types, good typing, library and everything else. It seems that creating compilers or translators should be easy enough.
But, as far as I understand, this is not the case. They write that Clang still cannot compile some complex code; utility developers often use existing frontends, apparently due to the complexity of development.
Questions arise:
1. Why so?
2. Are there any ways/utilities to translate complex C-code into simple one?
3. Are there any workable translators from C to another language?
Answer the question
In order to leave comments, you need to log in
The C language is very complex and writing its normal implementation is a very non-trivial task.
When writing a frontend, the first task is to parse it correctly. And from the point of view of building a parser, C is far from the easiest language. You will not find a ready-made BNF grammar for it. Plus, you need to support half a dozen different standards.
Next, you need to think about what and how YOU will give to the midland. Those. we need to design a view that is easy to work with for optimizations, that will display as many features of the language as possible without being overly complex.
It is necessary to do error handling and output. For example, in edg (industrial frontend) there are 2500 different error messages.
In fact, C (I'm not talking about C ++) is an extremely simple and understandable language. It has built-in types, soft static typing is also present, almost everything, except for the execution logic of the program itself, works just through libraries.
You can translate to other languages, but many things that are easily done in C in other languages will not look the best, try writing an analogue in your favorite language:
unsigned char buf[1024];
read(f, buf, 1024);
unsigned long int *p = (unsigned long int *)(buf+*((unsigned long int *)(buf+2)));
unsigned long int *q = (unsigned long int *)(buf+*((unsigned long int *)(buf+10)));
for (p < q)
*(p++) = 0;
C is a very simple language and writing a compiler for it is very easy.
Actually they are written in mass quantities. Every processor has a C compiler. Other languages are pulled up later or not pulled up at all.
About Clang. Well, it's open source. Such projects in most cases are developed very slowly.
And in general, there are a lot of cockroaches in such projects. People do what they want and what they want, not what is necessary and important for users.
An example about gcc. Let's say there is a code with a function and a pointer to it. We turn this function pointer into another type (for example, into a function that takes one more argument). And call the function by this pointer.
gcc - will issue a warning and WILL NOT CREATE code. Just insert a plug - fall here.
msvc - will create a working code (well, yes, the stack will be slightly warped there, but that's what we wanted)
The C compiler is easy to write. It is difficult to write it in such a way that it generates high-quality code. The vast majority of gcc bugs are related to optimizations.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question