PART1 - Compiler Lecture Notes
PART1 - Compiler Lecture Notes
PART1 - Compiler Lecture Notes
2. Syntax Analysis: It is also known as parsing as it generates parse tree from the
token produced. It checks whether the expression made by tokens follow the syntax or not.
The interior node of tree represent string of token i.e operation and children represents the
arguments of operations
• Syntax analysis is the second phase of compiler which is also called as parsing.
• Parser converts the tokens produced by lexical analyzer into a tree like representation
called parse tree.
3. Semantic Analysis: It uses syntax tree and information to check source program.
It checks whether the parse tree to be constructed follows the rules of languages or not.
Type information saves it in either the syntax tree or symbol table for subsequent use
during intermediate code generation. It also keeps track to identify their types and
expressions whether identifiers are declared before it is used or not.
• Semantic analysis is the third phase of compiler.
• It checks for the semantic consistency.
• Type information is gathered and stored in symbol table or in syntax tree.
• Performs type checking.
iii) It should itself be fast and should not delay overall compilation process.
t1=id3*60.0
id1=id2+t1
• Code optimization phase gets the intermediate code as input and produces optimized
intermediate code as output.
• It results in faster running machine code.
• It can be done by reducing the number of lines of code for a program.
• This phase reduces the redundant code and attempts to improve the intermediate code so
that faster-running machine code will result.
• During the code optimization, the result of the program is not affected.
To improve the code generation, the optimization involves
Deduction and removal of dead code (unreachable code).
Calculation of constants in expressions and terms.
Collapsing of repeated expression into temporary string.
Loop unrolling.
Moving code outside the loop.
Removal of unwanted temporary variables.
t1 = id3* 5.0
id1 = id2 + t1
6. Code Generation: It also keeps track of idea. It takes input or intermediate
representation of source program and maps it into target language. It is the final phase of
compiler. It generates object code of some lower level programming language[3]. Assembly
language, the code generated by code generator has following meaningful minimum
property to carry exact meaning of source code. If target language is machine code registers
or memory location then intermediate instructions are translated into sequence of machine
instruction that perform some tasks[3]. The intermediate might get translated into machine
code.
Code Generation
• Code generation is the final phase of a compiler.
• It gets input from code optimization phase and produces the target code or object code as
result.
• Intermediate instructions are translated into a sequence of machine instructions that
perform the same task.
The code generation involves
Allocation of register and memory.
Generation of correct references.
Generation of correct data types.
Generation of missing code.
Advantages:
Hardware Optimization
While being locked into a specific hardware package has its downsides, compiling a
program can also increase its performance. Users can send specific options to compilers
regarding the details of the hardware the program will be running on. This allows the
compiler to create machine language code that makes the most efficient use of the
specified hardware, as opposed to more generic code. This also allows advanced users to
optimize a program's performance on their computers.
Disadvantages:
Hardware Specific
Because a compiler translates source code into a specific machine language, programs
have to be specifically compiled for OS X, Windows or Linux, as well as specifically for 32-
bit or 64-bit architectures. For a programmer or software company trying to get a product
out to the widest possible audience, this means maintaining multiple versions of the
source code for the same application. This result in a more time spent on source code
maintenance and extra trouble when updates are released.
Compile Times
One of the drawbacks of having a compiler is that it must actually compile source code.
While the small programs that many novice programmers code take trivial amounts of
time to compile, larger application suites can take significant amounts of time to compile.
When programmers have nothing to do but wait for the compiler to finish, this time can
add up—especially during the development stage, when the code has to be compiled in
order to test functionality and troubleshoot glitches.
Bootstrapping in Compiler Design
Bootstrapping is a process in which simple language is used to translate more complicated
program which in turn may handle for more complicated program. This complicated program
can further handle even more complicated program and so on.
Writing a compiler for any high level language is a complicated process. It takes lot of time to
write a compiler from scratch. Hence simple language is used to generate target code in some
stages. to clearly understand the Bootstrapping technique consider a following scenario.
Suppose we want to write a cross compiler for new language X. The implementation language
of this compiler is say Y and the target code being generated is in language Z. That is, we
create XYZ. Now if existing compiler Y runs on machine M and generates code for M then it is
denoted as YMM. Now if we run XYZ using YMM then we get a compiler XMZ. That means a
compiler for source language X that generates a target code in language Z and which runs on
machine M.
Following diagram illustrates the above scenario.
Example:
We can create compiler of many different forms. Now we will generate.
Advantages
Bootstrapping a compiler has the following advantages:
it is a non-trivial test of the language being compiled, and as such is a form
of dogfooding.
compiler developers and bug reporting part of the community only need to know the
language being compiled.
compiler development can be performed in the higher-level language being compiled.
improvements to the compiler's back-end improve not only general-purpose programs
but also the compiler itself.
it is a comprehensive consistency check as it should be able to reproduce its own object
code.
Note that some of these points assume that the language runtime is also written in the
same language
Bootstrapping is a technique that is widely used in compiler development. It has four main
uses: