0% found this document useful (0 votes)
9 views12 pages

Q1. Draw and Explain Different Phases of Compiler: - : Symbol Table

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Q1. Draw and Explain Different Phases of Compiler: - : Symbol Table

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Q1.

Draw and explain different phases of compiler : -

Symbol Table – It is a data structure being used and maintained by the compiler, consisting of
all the identifier’s names along with their types. It helps the compiler to function smoothly by
finding the identifiers quickly.

The compiler has two modules namely the front end and the back end. Front-end constitutes
the Lexical analyzer, semantic analyzer, syntax analyzer, and intermediate code generator.
And the rest are assembled to form the back end.
1. Lexical Analyzer –
It is also called a scanner. It takes the output of the preprocessor (which performs file
inclusion and macro expansion) as the input which is in a pure high-level language. It reads
the characters from the source program and groups them into lexemes (sequence of
characters that “go together”). Each lexeme corresponds to a token. Tokens are defined by
regular expressions which are understood by the lexical analyzer. It also removes lexical
errors (e.g., erroneous characters), comments, and white space.

2. Syntax Analyzer – It is sometimes called a parser. It constructs the parse tree. It takes all
the tokens one by one and uses Context-Free Grammar to construct the parse tree.
Why Grammar?
The rules of programming can be entirely represented in a few productions. Using these
productions we can represent what the program actually is. The input has to be checked
whether it is in the desired format or not.
The parse tree is also called the derivation tree. Parse trees are generally constructed to
check for ambiguity in the given grammar. There are certain rules associated with the
derivation tree.

 Any identifier is an expression


 Any number can be called an expression
 Performing any operations in the given expression will always result in an expression. For
example, the sum of two expressions is also an expression.
 The parse tree can be compressed to form a syntax tree

3} Semantic Analyzer –
It verifies the parse tree, whether it’s meaningful or not. It furthermore produces a
verified parse tree. It also does type checking, Label checking, and Flow control checking.

4} Intermediate Code Generator


– It generates intermediate code, which is a form that can be readily executed by a
machine We have many popular intermediate codes. Example – Three address codes etc.
Intermediate code is converted to machine language using the last two phases which are
platform dependent.
Till intermediate code, it is the same for every compiler out there, but after that, it
depends on the platform. To build a new compiler we don’t need to build it from scratch.
We can take the intermediate code from the already existing compiler and build the last
two parts.

5 }Code Optimizer
– It transforms the code so that it consumes fewer resources and produces more speed.
The meaning of the code being transformed is not altered. Optimization can be categorized
into two types: machine-dependent and machine-independent.
 Target Code Generator – The main purpose of the Target Code generator is to write a code
that the machine can understand and also register allocation, instruction selection, etc.
The output is dependent on the type of assembler. This is the final stage of compilation.
The optimized code is converted into relocatable machine code which then forms the
input to the linker and loader.
Q2 . one , two and multipass compiler

Compiler pass refers to the traversal of a compiler through the entire program. Compiler pass
are two types: Single Pass Compiler, and Two Pass Compiler or Multi Pass Compiler. These are
explained as following below.

1. Single Pass Compiler:


If we combine or group all the phases of compiler design in a single module known as single
pass compiler.

In above diagram there are all 6 phases are grouped in a single module, some points of single
pass compiler is as:

1. A one pass/single pass compiler is that type of compiler that passes through the part of each
compilation unit exactly once.
2. Single pass compiler is faster and smaller than the multi pass compiler.
3. As a disadvantage of single pass compiler is that it is less efficient in comparison with
multipass compiler.
4. Single pass compiler is one that processes the input exactly once, so going directly from
lexical analysis to code generator, and then going back for the next read.
Note: Single pass compiler almost never done, early Pascal compiler did this as an introduction.
Problems with single pass compiler:
1. We can not optimize very well due to the context of expressions are limited.
2. As we can’t backup and process, it again so grammar should be limited or simplified.
3. Command interpreters such as bash/sh/tcsh can be considered as Single pass compiler, but
they also execute entry as soon as they are processed.

3. Two Pass compiler or Multi Pass compiler:


A Two pass/multi-pass Compiler is a type of compiler that processes the source code or
abstract syntax tree of a program multiple times. In multipass Compiler we divide
phases in two pass as:

1. First Pass: is refers as


 (a). Front end
 (b). Analytic part
 (c). Platform independent

In first pass the included phases are as Lexical analyzer, syntax analyzer, semantic analyzer,
intermediate code generator are work as front end and analytic part means all phases
analyze the High level language and convert them three address code and first pass is
platform independent because the output of first pass is as three address code which is
useful for every system and the requirement is to change the code optimization and code
generator phase which are comes to the second pass.
2. Second Pass: is refers as
 (a). Back end
 (b). Synthesis Part
 (c). Platform Dependent

In second Pass the included phases are as Code optimization and Code generator are work
as back end and the synthesis part refers to taking input as three address code and convert
them into Low level language/assembly language and second pass is platform dependent
because final stage of a typical compiler converts the intermediate representation of
program into an executable set of instructions which is dependent on the system.
With multi-pass Compiler we can solve these 2 basic problems:
1. If we want to design a compiler for different programming language for same machine. In
this case for each programming language there is requirement of making Front end/first
pass for each of them and only one Back end/second pass as:

2. If we want to design a compiler for same programming language for different


machine/system. In this case we make different Back end for different Machine/system
and make only one Front end for same programming language as:
Differences between Single Pass and Multipass Compilers:
Parameters Single pass multi Pass

Speed Fast Slow

Memory More Less

Time Less More

Portability No Yes
Q 3. Explain lex in Compiler Design
o Lex is a program that generates lexical analyzer. It is used with YACC parser generator.
o The lexical analyzer is a program that transforms an input stream into a sequence of
tokens.
o It reads the input stream and produces the source code as output through
implementing the lexical analyzer in the C program.

The function of Lex is as follows:

o Firstly lexical analyzer creates a program lex.1 in the Lex language. Then Lex compiler
runs the lex.1 program and produces a C program lex.yy.c.
o Finally C compiler runs the lex.yy.c program and produces an object program a.out.
o a.out is lexical analyzer that transforms an input stream into a sequence of tokens.

Lex file format

A Lex program is separated into three sections by %% delimiters. The formal of Lex source is as
follow :

1. { definitions }
2. %%
3. { rules }
4. %%
5. { user subroutines }

Definitions include declarations of constant, variable and regular definitions.

Rules define the statement of form p1 {action1} p2 {action2}....pn {action}.

Where pi describes the regular expression and action1 describes the actions what action the
lexical analyzer should take when pattern pi matches a lexeme
Q4. Explain role of lexical analyzer

The lexical analyzer is the first phase of compiler. Its main task is to read the input characters
and produce as output a sequence of tokens that the parser uses for syntax analysis

 It is implemented by making lexical analyzer be a subroutine


 Upon receiving a “get next token” command from parser, the lexical analyzer reads the
input character until it can identify the next token
 It may also perform secondary task at user interface
 One such task is stripping out from the source program comments and white space in
the form of blanks, tabs, and newline characters
 Some lexical analyzer are divided into cascade of two phases, the first called scanning
and second is “lexical analysis”.
 The scanner is responsible for doing simple task while lexical analysis does the more
complex task
Q5. Difference between single pass & multi-pass compiler

One-Pass Compiler Multi-Pass Compiler

It reads the code only once and It reads the code multiple times, each time changing it into
translates it at a similar time. numerous forms.

They are faster. They are "slower." As more number of passes means more
execution time.

Less efficient code optimization Better code optimization and code generation.
and code generation.

It is also called a "Narrow It is also called a "wide compiler." As they can scan every
compiler." It has limited scope. portion of the program.

The compiler requires large The memory occupied by one pass can be reused by a
memory. subsequent pass; therefore, small memory is needed by the
compiler.

Example − Pascal & C Example − Modula -2 languages use multi-pass compilation.


languages use one-pass
compilation.
Q6. One and two buffer scheme

1} Single buffer :
A buffer is provided by the operating system to the system portion of the main memory.

Block oriented device –


 System buffer takes the input.
 After taking the input, the block gets transferred to the user space by the process and
then the process requests for another block.
 Two blocks works simultaneously, when one block of data is processed by the user
process, the next block is being read in.
 OS can swap the processes.
 OS can record the data of system buffer to user processes.

Stream oriented device –
 Line- at a time operation is used for scroll made terminals. User inputs one line at a time,
with a carriage return signaling at the end of a line.
 Byte-at a time operation is used on forms mode, terminals when each keystroke is
significant.

2. Double buffer :
Block oriented –
 There are two buffers in the system.
 One buffer is used by the driver or controller to store data while waiting for it to be taken
by higher level of the hierarchy.
 Other buffer is used to store data from the lower level module.
 Double buffering is also known as buffer swapping.
 A major disadvantage of double buffering is that the complexity of the process get
increased.
 If the process performs rapid bursts of I/O, then using double buffering may be deficient.
Stream oriented –
 Line- at a time I/O, the user process need not be suspended for input or output, unless
process runs ahead of the double buffer.
 Byte- at a time operations, double buffer offers no advantage over a single buffer of twice
the length.

You might also like