Home > Back-end >  How are features in programming languages implemented, especially in lower level languages such as C
How are features in programming languages implemented, especially in lower level languages such as C

Time:10-18

I was looking at a list of new features in the different C versions, and I was wondering, how are features implemented in a language? For example, if C was to add new features into the language, would they code the features in by using the C language itself? Or is another technique to modifying the language?

CodePudding user response:

As you weren't specific about particular features, I'll give you answer only for the last part of your question

would they code the features in by using the C language itself

Yes, they are. This process is called compiler bootstrapping. The simplest explanation of the process:

  1. Write the first version of a compiler on any programming language (e.g. assembler), which allow you implement basic constructions on your new language (C in your case)

  2. Rewrite this logic on your new language :)

  3. Continue add new features until you're satisfied

CodePudding user response:

I diagree with the "bootstrapping" answer - as Richrad Critten notes in the comments, that hasn't been done in decades. It's entirely obsolete.

Languages like C and C are compiled. That is to say, there is a program called a compiler, which reads source code (text) and emits binary code that can be directly executed on the CPU.

The basics of this are not very hard - languages such as C are built around function calls, and every CPU from the last few decades has direct support for that.

Sure, new standards add new functions to the Standard Library, but that just means that the compiler comes with a set of pre-written functions. For quite a few of those functions, earlier versions were shipped as part of the Boost libraries, instead of the Standard Library, which shows there is nothing special.

But the core part of the language also includes some bits that are not simply function calls. In this case, the compiler authors need to devise a mapping of C source code text to CPU assembly code. That mapping is quite complex, especially for optimizing compilers. But this mapping can still be expressed in any language - there is no problem with a C compiler written in C , or a C compiler written in C. And for the same reason, a compiler for C 20 can easily be written in C 11. Sure, that C 20 compiler cannot use std::string_view itself, useful as it might be, but it can still use a MyOwn::string_view class instead.

  •  Tags:  
  • c
  • Related