Assignment CS7002 Compiler Design Powered by A2softech (A2kash)
Assignment CS7002 Compiler Design Powered by A2softech (A2kash)
A.1: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.
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.
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.
Ans.i) LEX:- • Lex is a tool in lexical analysis phase to recognize tokens using regular
expression.
• Lex tool itself is a lex compiler.
Use of Lex
• lex.l is an a input file written in a language which describes the generation of lexical analyzer.
The lex compiler transforms lex.l to a C program known as lex.yy.c.
• lex.yy.c is compiled by the C compiler to a file called a.out.
• The output of C compiler is the working lexical analyzer which takes stream of input characters
and produces a stream of tokens.
• yylval is a global variable which is shared by lexical analyzer and parser to return the name and
an attribute value of token.
• The attribute value can be numeric code, pointer to symbol table or nothing.
• Another tool for lexical analyzer generation is Flex.
ii) Input buffering:- The Input buffer is also commonly
known as the input area or input block.
ii) Input buffering:- The Input buffer is also commonly known as the input area or input
block.
1. When referring to computer memory, the input information before it continues to the CPU for
processing buffer is a location that holds all incoming.
2. Input buffer can be also used to describe other Input buffer can be also used to describe other
information before it is processed.
Ans: 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.
Ans:-
Q 3 a) What is syntax analysis? What are its primary functions?
Ans: Syntax analysis is a second phase of the compiler design process that comes after
lexical analysis. It analyses the syntactical structure of the given input. It checks if the given
input is in the correct syntax of the programming language in which the input which has
been written. It is known as the Parse Tree or Syntax Tree.
The Parse Tree is developed with the help of pre-defined grammar of the language. The syntax
analyzer also checks whether a given program fulfills the rules implied by a context-free
grammar. If it satisfies, the parser then creates the parse tree of that source program. Otherwise,
it will display error messages.
Ans: A compiler can broadly be divided into two phases based on the way they compile.
Analysis Phase
Known as the front-end of the compiler, the analysis phase of the compiler reads the source
program, divides it into core parts and then checks for lexical, grammar and syntax errors.The
analysis phase generates an intermediate representation of the source program and symbol table,
which should be fed to the Synthesis phase as input.
Synthesis Phase
Known as the back-end of the compiler, the synthesis phase generates the target program with
the help of intermediate source code representation and symbol table.
A compiler can have many phases and passes.
• Pass : A pass refers to the traversal of a compiler through the entire program.
• Phase : A phase of a compiler is a distinguishable stage, which takes input from the
previous stage, processes and yields output that can be used as input for the next stage.
A pass can have more than one phase.
First Set
This set is created to know what terminal symbol is derived in the first position by a non-
terminal. For example,
α→tβ
That is α derives t (terminal) in the very first position. So, t ∈ FIRST(α).
Algorithm for calculating First set
Look at the definition of FIRST(α) set:
Follow Set
Ans: A cross compiler is a compiler capable of creating executable code for a platform other
than the one on which the compiler is running. For example, a compiler that runs on a Windows
7 PC but generates code that runs on Android smartphone is a cross compiler.
A cross compiler is necessary to compile code for multiple platforms from one development
host. Direct compilation on the target platform might be infeasible, for example on
a microcontroller of an embedded system, because those systems contain no operating system.
In paravirtualization, one computer runs multiple operating systems and a cross compiler could
generate an executable for each of them from one main source.
Cross compilers are distinct from source-to-source compilers. A cross compiler is for cross-
platform software development of machine code, while a source-to-source compiler translates
from one programming language to another in text code. Both are programming tools.