2 Phases of a Compiler. Computer Language Representation. Compiler Construction Tools. Outlines
3 Compiler is a translator that converts high-level language into the machine language. High-level language is written by a developer and machine language can be understood by the processor. Compiler is used to show errors to the programmer. The main purpose of compiler is to change the code written in one language without changing the meaning of the program. When you execute a program which is written in HLL programming language then it executes into two parts . In the first part , the source program compiled and translated into the object program (low level language). In the second part , object program translated into the target program through the assembler. Introduction
4 However writing Programs in Assembler is tedious therefore programmers design another Language translator as follows. Cont.…
5 Languages written in P hp, Rubby, JavaScript and Python requires Interpreter as language translator. However programs written in C, C++ and Erlang can be converted directly into Machine codes. Compilers come into picture since compilers tend to be faster and more efficient than Interpreters . Cont.…
6 Internal Architecture of Language Translator Example :- Consider conversions of the following source code into machines code.
7 Preprocessor :- Converts H igh Level Languages into pure high level language by removing comments and link it into preprocessor directives as shown below. Cont.…
8 Compiler :- Converts Pure high level Languages into Assembly Languages. Cont.…
9 Assembler :- is responsible to convers Assembly Languages to relocatable machine codes . Cont.…
10 Linker/Loader :- Generates A bsolute Machine Codes which is actually loaded into computer RAM and ready for execution. Cont.…
11 Phases of a Compiler : Fig: phases of compiler The compilation process contains the sequence of various phases. Each phase takes source program in one representation and produces output in another representation. Each phase takes input from its previous stage.
12 Cont.…
13 Information's gathered in analysis phase is stored in Symbol table and used by Synthesis phase. Error handlers are used for error detections and recovery. Cont.…
14 Lexical Analysis :- Lexical analyzer phase is the first phase of compilation process. It takes source code as input . It reads the source program one character at a time and converts it into meaningful lexemes . Lexical analyzer represents these lexemes in the form of tokens . Consider the following source codes and various phases. V arious P hases of C ompiler
15 After Lexical Analysis it is converted into Lexemes and tokens.
16 Syntax Analysis :- Syntax analysis is the second phase of compilation process. It takes tokens as input and generates a parse tree as output . In syntax analysis phase, the parser checks that the expression made by the tokens is syntactically correct or not as shown below. Cont.…
17 Cont.…
18 Semantic Analysis :- Semantic analysis is the third phase of compilation process. It checks whether the parse tree follows the rules of language. Semantic analyzer keeps track of identifiers, their types and expressions. The output of semantic analysis phase is the annotated tree syntax . Cont.…
19 Cont.…
20 Intermediate Code Generation :- In the intermediate code generation, compiler generates the source code into the intermediate code. Intermediate code is generated between the high-level language and the machine language. The intermediate code should be generated in such a way that you can easily translate it into the target machine code. Cont.…
21 Cont.…
22 Code Optimization :- Code optimization is an optional phase. It is used to improve the intermediate code so that the output of the program could run faster and take less space. It removes the unnecessary lines of the code and arranges the sequence of statements in order to speed up the program execution. Cont.…
23 Cont.…
24 Code Generation :- Code generation is the final stage of the compilation process. It takes the optimized intermediate code as input and maps it to the target machine language. Code generator translates the intermediate code into the machine code of the specified computer. Cont.…
25 Example: Cont.…
26 Computer Language Representation Language Processors :- A special translator system software is used to translate the program written in a high-level language into machine code is called Language Processor and the program after translated into machine code is object program/object code. Compilers, interpreters, translate programs written in high-level languages into machine code that a computer understands. And assemblers translate programs written in low-level or assembly language into machine code. In the compilation process, there are several stages. To help programmers write error-free code, tools are available.
27 Assembly language is machine-dependent, yet mnemonics used to represent instructions in it are not directly understandable by machine and high-Level language is machine-independent. A computer understands instructions in machine code, i.e. in the form of 0s and 1s. It is a tedious task to write a computer program directly in machine code. The programs are written mostly in high-level languages like Java, C++, Python etc. and are called source code. These source code cannot be executed directly by the computer and must be converted into machine language to be executed. Cont.…
28 Compiler :- It reads the complete source program written in High-Level L anguage and translates it into an equivalent M achine L anguage. Example: C, C++, C#, Java. In a compiler, the source code is translated to object code successfully if it is free of errors. The compiler specifies the errors at the end of the compilation with line numbers when there are any errors in the source code. The errors must be removed before the compiler can successfully recompile the source code again the object program can be executed number of times without translating it again. Types of L anguage P rocessors
29 Assembler :- Assembler is used to translate the program written in Assembly Language into machine code. The source program is an input of an assembler that contains assembly language instructions. The output generated by the assembler is the object code or machine code understandable by the computer. Assembler is basically the 1st interface that is able to communicate humans with the machine. We need an Assembler to fill the gap between human and machine so that they can communicate with each other. code written in assembly language is some sort of mnemonics(instructions) like ADD, MUL, MUX, SUB, DIV, MOV and so on. Cont.…
30 A nd the assembler is basically able to convert these mnemonics in Binary code. Here, these mnemonics also depend upon the architecture of the machine. Cont.… Interpreter :- The translation of a single statement of the source program into machine code is done by a language processor and executes immediately before moving on to the next line is called an interpreter. If there is an error in the statement, the interpreter terminates its translating process at that statement and displays an error message.
31 The interpreter moves on to the next line for execution only after the removal of the error. An Interpreter directly executes instructions written in a programming or scripting language without previously converting them to an object code or machine code. An interpreter translates one line at a time and then executes it. Example:- Perl, Python and MATLAB. Cont.…
32 Difference between Compiler and Interpreter Compiler Interpreter A compiler is a program that converts the entire source code of a programming language into executable machine code for a CPU. An interpreter takes a source program and runs it line by line, translating each line as it comes to it. The compiler takes a large amount of time to analyze the entire source code but the overall execution time of the program is comparatively faster. An interpreter takes less amount of time to analyze the source code but the overall execution time of the program is slower.
33 The compiler generates the error message only after scanning the whole program, so debugging is comparatively hard as the error can be present anywhere in the program. Its Debugging is easier as it continues translating the program until the error is met. The compiler requires a lot of memory for generating object codes. It requires less memory than a compiler because no object code is generated. Generates intermediate object code. No intermediate object code is generated. For Security purpose compiler is more useful. The interpreter is a little vulnerable in case of security. Examples: C, C++, Java Examples: Python, Perl, JavaScript, Rubby Cont.…
34 Compiler Construction Tools The compiler writer can use some specialized tools that help in implementing various phases of a compiler. These tools assist in the creation of an entire compiler or its parts. Some commonly used compiler construction tools include:- Parser Generator :- It produces syntax analyzers (parsers) from the input that is based on a grammatical description of programming language or on a context-free grammar. It is useful as the syntax analysis phase is highly complex and consumes more manual and compilation time. Example: PIC, EQM
35 Scanner Generator :- It generates lexical analyzers from the input that consists of regular expression description based on tokens of a language. It generates a finite automaton to recognize the regular expression. Example: Lex Cont.….
36 Cont.…. Syntax Directed Translation Engines (SDTE) :- It generates intermediate code with three address format from the input that consists of a parse tree. These engines have routines to traverse the parse tree and then produces the intermediate code. In this, each node of the parse tree is associated with one or more translations.
37 Automatic Code Generators :- It generates the machine language for a target machine. Each operation of the intermediate language is translated using a collection of rules and then is taken as an input by the code generator. A template matching process is used. An intermediate language statement is replaced by its equivalent machine language statement using templates. Data-flow Analysis Engines :- It is used in code optimization. Data flow analysis is a key part of the code optimization that gathers the information, that is the values that flow from one part of a program to another. Cont.….
38 Compiler Construction Toolkits :- It provides an integrated set of routines that aids in building compiler components or in the construction of various phases of compiler. Cont.….
39 Lexical Analyzer Generator :- This tool helps in generating the lexical analyzer or scanner of the compiler. It takes as input a set of regular expressions that define the syntax of the language being compiled and produces a program that reads the input source code and tokenizes it based on these regular expressions. Parser Generator :- This tool helps in generating the parser of the compiler. It takes as input a context-free grammar that defines the syntax of the language being compiled and produces a program that parses the input tokens and builds an abstract syntax tree. Features of Compiler Construction Tools
40 Code Generation Tools :- These tools help in generating the target code for the compiler. They take as input the abstract syntax tree produced by the parser and produce code that can be executed on the target machine. Optimization Tools :- These tools help in optimizing the generated code for efficiency and performance. They can perform various optimizations such as dead code elimination, loop optimization, and register allocation. Debugging Tools :- These tools help in debugging the compiler itself or the programs that are being compiled. They can provide debugging information such as symbol tables, call stacks, and runtime errors. Cont.….
41 Profiling Tools :- These tools help in profiling the compiler or the compiled code to identify performance bottlenecks and optimize the code accordingly. Documentation Tools :- These tools help in generating documentation for the compiler and the programming language being compiled. They can generate documentation for the syntax, semantics, and usage of the language. Language Support :- Compiler construction tools are designed to support a wide range of programming languages, including high-level languages such as C++, Java, and Python, as well as low-level languages such as assembly language. Cont.….
42 Cross-Platform Support :- Compiler construction tools may be designed to work on multiple platforms, such as Windows, Mac, and Linux. User Interface :- Some compiler construction tools come with a user interface that makes it easier for developers to work with the compiler and its associated tools. Cont.….