Você está na página 1de 6

Assignment:

Submitted To:

Submitted By:

Roll No:
Question No 1:

Write the importance of compiler construction?

Answer:
Compilers:
A program that translates an executable program in one language into an executable program in
another language.
Or
A program that converts/translates programing language in machine language so computer can
understand and vice versa.
 The compiler typically lowers the level of abstraction of the program
 We expect the program produced by the compiler to be better, in some way, than the original
language.

Importance of Compiler Construction:

1. Compilers provide an essential interface between applications and architectures


2. Compilers embody a wide range of theoretical techniques
3. Compiler construction teaches programming and software engineering skill

Role of Compilers:
High-level programming languages
 Increase programmer productivity
 Better maintenance
 Portable
Low-level machine details
 Instruction selection
 Addressing modes
 pipelines
 registers & cache
 Instruction- level parallelism.

Compilers are needed to efficiently bridge the gap!


Isn't it a solved problem?

Compiler’s Qualities:
 Correct code
 Output runs fast
 Compiler runs fast
 Compile time proportional to program size
 Support for separate compilation
 Good diagnostics for syntax errors
 Works well with the debugger
 Good diagnostics for flow anomalies
 Cross language calls
 Consistent, predictable optimization

Therefore Compiler construction important for programmer.

Question No.2:

Define phase of compiler with figure and define each phase?

Answer:
The compilation process is a sequence of various phases. Each phase takes input from its previous stage,
has its own representation of source program, and feeds its output to the next phase of the compiler.
Let us understand the phases of a compiler.

Phases:
These are the phases of Compiler construction:
Lexical Analysis:
The first phase of scanner works as a text scanner. This phase scans the source code as a stream of
characters and converts it into meaningful lexemes. Lexical analyzer represents these lexemes in the
form of tokens as:
<token-name, attribute-value>
Syntax Analysis:
The next phase is called the syntax analysis or parsing. It takes the token produced by lexical analysis as
input and generates a parse tree (or syntax tree). In this phase, token arrangements are checked against
the source code grammar, i.e. the parser checks if the expression made by the tokens is syntactically
correct.
Semantic Analysis:
Semantic analysis checks whether the parse tree constructed follows the rules of language. For example,
assignment of values is between compatible data types, and adding string to an integer. Also, the
semantic analyzer keeps track of identifiers, their types and expressions; whether identifiers are
declared before use or not etc. The semantic analyzer produces an annotated syntax tree as an output.
Intermediate Code Generation:
After semantic analysis the compiler generates an intermediate code of the source code for the target
machine. It represents a program for some abstract machine. It is in between the high-level language
and the machine language. This intermediate code should be generated in such a way that it makes it
easier to be translated into the target machine code.
Code Optimization:
The next phase does code optimization of the intermediate code. Optimization can be assumed as
something that removes unnecessary code lines, and arranges the sequence of statements in order to
speed up the program execution without wasting resources (CPU, memory).
Code Generation:
In this phase, the code generator takes the optimized representation of the intermediate code and maps
it to the target machine language. The code generator translates the intermediate code into a sequence
of (generally) re-locatable machine code. Sequence of instructions of machine code performs the task as
the intermediate code would do.
Symbol Table:
It is a data-structure maintained throughout all the phases of a compiler. All the identifier's names along
with their types are stored here. The symbol table makes it easier for the compiler to quickly search the
identifier record and retrieve it. The symbol table is also used for scope management.

Lexical Analysis

Syntax Analysis

Semantic Analysis

Symbol
Table Error
Inter. Code Gen Handling
Manage

Code Optimization

Target code gen

Assembly

Figure: Compiler construction phases


Question no 3:
What is the Difference between Syntax & Semantic Analysis?
Answer:
Syntax Analysis Semantic Analysis
1. Syntax refers to the structure/form of 1. Deal with the meaning assigned to the
the code that a specific programming symbols, characters and words.
language specifies.
2. It is referring to grammatically structure 2. Refers to the meaning of the vocabulary
of the language. If you are writing the c symbols arranged with that structure.
language. Grammatical (syntactically valid) does
not imply sensible (semantically valid).

3. Syntax refers to formal rules governing 3. Semantics refers to the set of rules
the construction of valid statements in a which give the meaning of a statement.
language.

4. Syntax errors. These are invalid code the 4. Semantic errors. These are valid code
compiler doesn't understand, e.g. your the compiler understands, but they do
example of multiplying a string with an not what you, the programmer,
integer in C. The compiler will detect intended.
them, because it can't compile them.

5. The grammar of a language is called 5. Semantics deals with the meaning of a


Syntax. sentence.

6. . Syntax errors can be caught at 6. Semantics errors are hard to find. They
compilation time and are easy to track are mostly encountered at runtime, or
when the results do not match with the
expectations.

Question No.4:
Why we use Automata in this course?
Answer:
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.

The more interesting part is in the second phase, Parsing. Our goal here is to build what’s known as
an Abstract Syntax Tree (or AST).
There are two types of Parsers:
 Top-down and Bottom-up.
 Top-down parsing is the simpler form.

Finite Automata is the major part of the compiler construction. As two phases of compiler uses
automata in it.
1. Lexical Analysis:
Uses Regular Expressions to tokenize the input. Regular expressions are usually implemented
with Finite Automata. As Regular Expression is major part of finite automata so finite automata
is compulsory part to operate Lexical Analysis. In this we use Regular Expressions to assign
tokens to given input through expressions.
2. Syntax Analysis:
Syntax analysis or parsing is the second phase of a compiler.
We have seen that a lexical analyzer can identify tokens with the help of regular expressions
and pattern rules.
But a lexical analyzer cannot check the syntax of a given sentence due to the limitations of the
regular expressions. Regular expressions cannot check balancing tokens, such as parenthesis.
Therefore, this phase uses context-free grammar (CFG), which is recognized by push-down
automata.
Hence Automata is much important to build a compiler.

Você também pode gostar