0

**whether the interpretation follows compilation phase during the exection of a pgm?**simply what happens when we run a program?if these are different things then what will perform syntax checking before interpretation.As i read python is a interpreted language then checking of statements done by what?

1 Answer 1

2

You have two options:

  • Compiled languages
  • Interpreted languages

In a compiled language, you need a compiler that gets source code as input and generates a binary as output that can run on a given target platform. For example, C, C++ or Java are compiled languages. After compiler generates the binary, you execute that binary on the target platform. The main steps involved in the compilation process to generate a binary are lexic, syntactic and semantic analysis, and code generation.

The compiler is a program (binary) that runs on the native platform and generates code for a given target platform. You have two options:

* target_platform == native_platform (native-compiler)
* target_platform != native_platform (cross-compiler). 

If you have a x86_64 desktop PC, your compiler runs on x86_64 and generates code that runs on x86_64, your have a native compiler. In this case, the compiler generates native machine code.

If you have a x86_64 desktop PC, your compiler runs on x86_64 and generates code that runs on a different platform (such as JVM), you have a cross-compiler. You should understand that Java language uses a cross-compiler that gets java language as input, and generates byte-code that runs on the JVM (not on the x86_64 machine) as output.

Other cross-compilers such as arm-linux-gcc, mips-linux-gcc, ppc-linux-gcc, and more, get C source code as input and generate binary to run on the proper target platform (ARM, MIPS, PPC).

In an interpreted language, you don't need a compiler to generate code, so a binary is not generated at the end of the process. bash and python are interpreted languages. The interpreter of the language (a binary installed in your PC such as /bin/bash or /usr/bin/python) receives input source code, interprets it, and executes it to generate the output. The steps followed to interpret the source code are exactly the same followed by a compiler, except the interpreter doesn't generate code, just executes it after analyzing.

I wrote an article some time ago explaining how you can write an interpreter of a custom-defined language using python. This article is written in spanish, but the whole process is explained step-by-step, so you can learn a lot if you are interested on it. At the end of the articles you can find the source code to download and test. Source code is available in github. The article is available at this link

Hope it helps! :)

Sign up to request clarification or add additional context in comments.

11 Comments

What u mean by this binary?is it bytecode?So interpreter who will check the syntax?if we write a wrong syntax it will show some errors right?
bytecode is machine code that runs on the JVM, so Java generates code for the JVM. Try running byte-code directly on the x86_64 without installing the JVM and see what happen...
If you dump content of a binary file (using hexdump tool), it always contais 0's and 1's... But thoses bits have an internal format... On linux, binaries are encoded using ELF (en.wikipedia.org/wiki/Executable_and_Linkable_Format). Windows has its own binary format.
The compiler steps are: lexic analyzer, syntactic analyzer, semantic analyzer, code optimizer and code generation. The ouput of each step is the input of the next step. Code optimiser (-O with gcc) is responsable to improve input code to generate a more efficiente output. For example, the expression (false AND X) is equivalent to false (you don't need to evaluate X nor generate code for it). Code generator is a step executed after code optimiser. It generates code to run on the target platform (for example assembler code if you are using a C/C++ compiler). Hope it helps! :)
By definition, interpreters don't generate binary code. Interpreters read input source code and generate the result. For example, python or bash work in such a way. Imagine you have this expression a=$(expr 3 + 4) in bash shell script. After semantic analysis, bash knows it has a variable called "a" that has to be assigned with the value of "3+4". The own parser makes the ADD operation (3+4), defines the variable a, and assigns it with the result (7).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.