Unit - 1
Introduction
● A compiler is a translator that translates the language at the highest level into the language of the computer.
● A developer writes high-level language and the processor can understand machine language.
● The compiler is used to display a programr's errors.
● The main objective of the compiler is to modify the code written in one language without altering the program's meaning.
● When you run a program that is written in the language of HLL programming, it runs in two parts.
Fig 1: Execution process of Compiler
Features of compilers :
● Maintaining the right interpretation of the code
● The speed of the target code
● Understand prototypes for legal and illegal systems
● Good documentation / management of errors
● Correctness
● Speed of compilation
● Code debugging help
Task of compilers :
Tasks which are performed by compilers :
● Divides the source program into parts and imposes on them a grammatical structure.
● Allows you from the intermediate representation to construct the desired.
● Target program and also create the symbol table.
● Compiles and detects source code errors.
● Storage of all variables and codes is handled.
● Separate compilation support.
● Learn, interpret, and translate the entire program to a semantically equivalent.
● Depending on the computer sort, conversion of the source code into object code.
Key takeaway :
● A compiler is a computer program that lets you convert source code into low-level machine language written in a high-level language.
● Without modifying the context of the code, it converts the code written in one programming language to some other language.
● The compiler also makes the end code, optimised for execution time and memory space, efficient.
Phases -
The method of compilation includes the sequence of different stages. Each phase takes a source program in one representation and produces output in another representation. From its previous stage, each process takes input.
The various stages of the compiler take place :
- Lexical Analysis
- Syntax Analysis
- Semantic Analysis
- Intermediate Code Generation
- Code Optimization
- Code Generation
Fig 2: phases of compiler
- Lexical Analysis : The first phase of the compilation process is the lexical analyzer phase. As input, it takes source code. One character at a time, it reads the source program and translates it into meaningful lexemes. These lexemes are interpreted in the form of tokens by the Lexical analyzer.
2. Syntax Analysis : Syntax analysis is the second step of the method of compilation. Tokens are required as input and a parse tree is generated as output. The parser verifies that the expression made by the tokens is syntactically right or not in the syntax analysis process.
3. Semantic Analysis : The third step of the method of compilation is semantic analysis. This tests whether the parse tree complies with language rules. The semantic analyzer keeps track of identifiers, forms, and expressions of identifiers. The output step of semantic analysis is the syntax of the annotated tree.
4. Intermediate Code Generation : The compiler generates the source code into an intermediate code during the intermediate code generation process. Between high-level language and machine language, intermediate code is created. You can produce the intermediate code in such a way that you can convert it easily into the code of the target computer.
5. Code Optimization : An optional step is code optimization. It is used to enhance the intermediate code so that the program's output can run quicker and take up less space. It eliminates redundant code lines and arranges the sequence of statements to speed up the execution of the program.
6. Code Generation : The final stage of the compilation process is code generation. It takes as its input the optimised intermediate code and maps it to the language of the target computer. The code generator converts the intermediate code to the required computer's machine code.
Key takeaway :
● Each phase takes a source program in one representation and produces output in another representation.
● The method of compilation includes the sequence of different stages.
As computer-related technology spread all over the world, compiler construction instruments were added. They are also known as compilers, generators of compilers, or translators.
These tools use a particular language or algorithm for the compiler portion to be specified and implemented.
The tools for compiler construction are below.
- Parser Generator
It generates an input syntax analyzer (parser) based on a grammatical programming language description or context-free grammar. It is beneficial because the process of syntax analysis is extremely complex and takes more manual and compilation time. Example : PIC, EQM
Fig 3: parser generator
2. Scanner Generator
It produces lexical analyzers based on tokens of a language from the input that consists of a periodic expression description. To understand the periodic expression, it generates a finite automaton. Example : LEX
Fig 4: scanner generator
3. Automatic Code Generator
It produces the language of the computer for the target machine. Each intermediate language operation is interpreted using a series of rules and is then taken by the code generator as an input. A method of template matching is used. Using templates, an intermediate language statement is replaced by its corresponding machine language statement.
4. Syntax directed translation engines
This produces a three address format intermediate code from the input consisting of a parse tree. These engines have routines for traversing the parse tree and creating the intermediate code afterwards. In this, one or more translations are connected with each node of the parse tree.
5. Data- flow analysis engines
For code optimization, this method is helpful. Here, the user provides details and the intermediate code is compared to evaluate any relationship. It is also known as the analysis of data-flow. It allows you to figure out how values are transferred to another part from one part of the program.
6. Compiler construction toolkit
It offers an integrated collection of routines that assist in building compiler components or building different compiler phases.
Key takeaway :
● These tools use a particular language or algorithm for the compiler portion to be specified and implemented.
● Some specialised tools that aid in implementing different phases of a compiler can be used by the compiler writer.
● These tools help build a whole compiler or its components.
1.4 A simple one pass compiler
Pass is a full source software traversal. The programr has two passes through the source program to traverse it.
One - pass compiler -
● The one-pass compiler is only used once to access the program. The one-pass compiler passes through the components of each compilation unit only once.This translates every component into its final machine code.
● In the single pass parser, it is scanned and the token is extracted when the line source is processed.
● The syntax of each line is then evaluated and the structure of the tree is constructed. The code is created after the semantic portion.
● For every line of code, the same procedure is repeated until the whole program is compiled.
● Compiler source code is directly translated into computer code in a single pass. For example : The Pascal language.
Fig 5: single pass compiler
Problem with single pass compiler
● Due to the context of expressions that are constrained, we can not optimise very well.
● Since we can not backup and process it again, it should be constrained or simplified by grammar.
● Command interpreters such as bash/sh/tcsh can be regarded as a single pass parser, but as soon as they are processed, they also execute the entry.
Key takeaway :
● If we combine or group all compiler design stages into a single compiler module known as a single pass compiler.
● Almost never achieved with the single pass compiler, early Pascal compiler did this as an introduction.
References :
- “Compilers - Principles, Techniques and Tools”, A.V. Aho, R. Shethi and J.D.Ullman, Pearson Education
2. “Compiler Construction”, Barret, Bates, Couch, Galgotia.
3. “Compiler Construction”, Dhamdere, Mc-Millan.
4. “Unix Programming”, Pepkin Pike.