1 Assignment 1 Question 1 Write in detail about translators and its types Answer Translator: A translator or programming language processor is a computer program that performs the translation of a program written in a given programming language into a functionally equivalent program in another computer language, without losing the functional or logical structure of the original code. A program written in high-level language is called as source code. To convert the source code into machine code, translators are needed. Types of Translator 1. Compiler 2. Interpreter 3. Assembler Compiler: Compiler is a translator which is used to convert programs in highlevel language to low-level language. It translates the entire program and also reports the errors in source program encountered during the translation. A compiler is a program that can read a program in one language the source language and translate it into an equivalent program in another language the target language. An important role of the compiler is to report any errors in the source program that it detects during the translation process. Interpreter: Interpreter is a translator which is used to convert programs in high-level language to low-level language. Interpreter translates line by line and reports the error once it encountered during the translation process. An interpreter is another common kind of language processor. Instead of producing a target program as a translation, an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the user. Assembler: Assembler is a translator which is used to translate the assembly language code into machine language code. An assembler is a type of computer program that interprets software programs written in assembly language into machine language, code and instructions that can be executed by a computer. An assembler enables software and application developers to access, operate and manage a computer's hardware architecture and components. An assembler is sometimes referred to as the compiler of assembly language. It also provides the services of an interpreter. Question 2 Compare the translators in detail DAYAN AHMED 2 Assignment 1 Answer Comparison between translators are as follows: 1. Complier and Interpreter. S. No Compiler Interpreter 1 Performs the translation of a program as a whole. Performs statement by statement translation. 2 Execution is faster. Execution is slower. 3 Requires more memory as linking is needed for the generated intermediate object code. Memory usage is efficient as no intermediate object code is generated. 4 Debugging is hard as the error messages are generated after scanning the entire program only. It stops translation when the first error is met. Hence, debugging is easy. 5 Programming languages like C, C++ uses compilers. Programming languages like Python, BASIC, and Ruby uses interpreters. 2. Interpreter and Assembler. S. No Interpreter Assembler 1 Software that translates high language program into machine language. Software that converts program written in assembly language into machine language. 2 Converts the high language program to machine language line by line. Converts assembly language program to machine language. Most interpreters can complete the execution of a program by themselves. Assemblers produce an object code, which might have to be linked using linker programs in order to run on a machine Used by Ruby, Perl, Python, PHP Used by assembly language. 3 4 3. Assembler and Compiler. DAYAN AHMED 3 Assignment 1 S. No COMPILER ASSEMBLER 1 Generates the assembly language code or directly the executable code. Generates the relocate-able machine code. 2 Preprocessed source code. Assembly language code. 3 The compilation phases are lexical analyzer, Assembler makes two passes syntax analyzer, semantic analyzer, over the given input. intermediate code generation, code optimization, code generation. 4 The assembly code generated by the compiler is a mnemonic version of machine code. The relocate-able machine code generated by an assembler is represented by binary code. Question 3 What are the Techniques and Methodology Used by compiler? Answer If we have treated a compiler as a single box that maps a source program into a semantically equivalent target program. If we open up this box a little, we see that there are two parts to this mapping: analysis and synthesis. The analysis part breaks up the source program into constituent pieces and imposes a grammatical structure on them. It then uses this structure to create an intermediate representation of the source program. If the analysis part detects that the source program is either syntactically ill formed or semantically unsound, then it must provide informative messages, so the user can take corrective action. The analysis part also collects information about the source program and stores it in a data structure called a symbol table, which is passed along with the intermediate representation to the synthesis part. The synthesis part constructs the desired target program from the intermediate representation and the information in the symbol table. The analysis part is often called the front end of the compiler; the synthesis part is the back end. If we examine the compilation process in more detail, we see that it operates as a sequence of phases, each of which transforms one representation of the source program to another. A typical decomposition of a compiler into phases is shown below. DAYAN AHMED 4 Assignment 1 In practice, several phases may be grouped together, and the intermediate representations between the grouped phases need not be constructed explicitly. The symbol table, which stores information about the entire source program, is used by all phases of the compiler. Some compilers have a machineindependent optimization phase between the front end and the back end. The purpose of this optimization phase is to perform transformations on the intermediate representation, so that the back end can produce a better target program than it would have otherwise produced from an unoptimized intermediate representation. Since optimization is optional, one or the other of the two optimization phases may be missing. Question 4 Why understanding Theory of automata is necessary to understand/develop a compiler? Answer Importance of Theory of Automata to understanding a Complier: Theory of Automata plays a vital role in understanding the compiler construction. Many concepts of Automata recalls to understand the compiler construction. Here is the example Finite Automata. Finite Automata are used two of the three front-end phases of the compiler. The first phase, Lexical Analysis, uses Regular Expressions to tokenize the input. Regular expressions are usually implemented with Finite Automata. DAYAN AHMED