Compiler and interpreter (based on construction and design)

After looking at a large number of messages about the difference between compilers and interpreters, I still cannot understand the difference in their construction and internal mechanism.

The most common difference that I read is that the compiler creates a target program that is executable (meaning machine code as its output) that can work on the system and be served using input. While the interpreter simply starts entering line by line {what exactly is happening here?} And it produces the result.

My main doubts:

1) The compiler consists of a lexical analyzer, analyzer, intermediate code generator and code generator, but what are the parts of the interpreter?

2) Who provides support during the execution of interpreted languages, I mean, who manages the heap and stacks for recursive functions?

3) This is specific to the Python language:
Python includes a compiler stage, as well as an interpreter stage, the compiler creates some bytecode, and this bytecode is interpreted by its virtual machine. if I were to create only a compiler for Python (Python -> bytecode)

a) will I need to manage the memory {write code to control the stack and heap} for it?

b) how does this compiler differ from the traditional compiler or does the interpreter say?

I know this is a lot to ask here, but I really want to understand these smallest details.

I mean the compiler book Alfred W. Aho


Based on reviews and some additional research, I think I should change my question

The compiler should not output only machine code as output

But one question is still listening to me. Let's say I want to create a compiler (Python-> bytecode), and then the bytecode will be interpreted by the virtual machine. (Correct me if I am wrong).
Then I will have to write a lexical analyzer for Python, and then a parser that will generate some kind of abstract syntax tree. After that, do I need to generate some intermediate code (the 3-address code mentioned in the book of dragons) or a direct bytecode (which, I believe, will be given in the VM documentation)?

Do I have to write code to handle the stack to provide support for recursion and scope?

+6
source share
1 answer

First, the "compiler" does not mean "displays machine code." You can compile from any language to any other language, be it a high-level programming language, some intermediate format, code for a virtual machine (byte code) or code for a physical machine (machine code).

  • Like the compiler, the interpreter must read and understand the language that it implements. Thus, you have the same foreground code (although at present interpreters usually use a much simpler language - only bytecode, so these interpreters need only a very simple interface). Unlike the compiler, the interpreter back-end does not generate code, but executes it. Obviously, this is a completely different problem, and therefore the interpreter is clearly different from the compiler. It emulates a computer (often one that is much higher level than real-time machines) instead of creating a representation of an equivalent program.

  • Assuming today several higher-level virtual machines, this is the interpreter's work - there are special instructions, in particular, for calling functions and creating objects, and garbage collection is baked in the virtual machine. When you target lower-level computers (for example, an x86 instruction set), many of these details need to be baked into the generated code, either directly (system calls or something else) or by calling the standard C library in the implementation.

3.

  • a) Probably not, since a virtual machine designed for Python will not require this. It would be very easy to drown out the unnecessary and possibly incompatible with the semantics of Python, since it allows manual memory management. On the other hand, if you were aiming for something low level, for example LLVM, you had to be especially careful - the details depend on the target language. This is part of why no one does this.

  • b) It would be a great compiler and obviously not an interpreter. You will probably have a simpler backend than the machine code for the compiler, and due to the nature of the input language you will not have so much analysis and optimization, but there is no fundamental difference.

+4
source

Source: https://habr.com/ru/post/901951/


All Articles