compiler design ecs-603asa

Download Compiler Design ECS-603asa

Post on 13-Jul-2016




3 download

Embed Size (px)





AKGEC/IAP/FM/03Ajay Kumar Garg Engineering College, Ghaziabad

Department of CSE

MODEL SOLUTION PUT Course:B.Tech Semester: VI


Section: A, B

Subject:Compiler Design

Sub. Code:ECS-603Max Marks: 100

Time: 3 hour

Note: All questions are compulsory. All questions carry equal marks.Q1. Attempt any FOUR parts of the following.

4X5=20a. Explain all phases of compiler with suitable diagram.Ans. Compiler is a program (written in a high-level language) that converts / translates / compiles source program written in a high level language into an equivalent machine code.

Phases of Compiler:

Lexical Analyzer:

Lexical Analyzer reads the source program character by character and returns the tokens of the source program. A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, delimiters and so on)


newval := oldval + 12 => tokens: newval identifier

:= assignment operator

oldval identifier

+ add operator

12 a number

Syntax Analyzer:

Syntax Analyzer creates the syntactic structure (generally a parse tree) of the given program.

A syntax analyzer is also called as a parser. A parse tree describes a syntactic structure. The syntax of a language is specified by a context free grammar (CFG). The rules in a CFG are mostly recursive.

A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not.

If it satisfies, the syntax analyzer creates a parse tree for the given program.

Semantic Analyzer:

A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation.

Type-checking is an important part of semantic analyzer.

Normally semantic information cannot be represented by a context-free language used in syntax analyzers.

Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules)

the result is a syntax-directed translation,

Attribute grammars

Ex: newval := oldval + 12

The type of the identifier newval must match with type of the expression (oldval+12).Intermediate Code Generation:

A compiler may produce an explicit intermediate codes representing the source program.

These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the level of machine codes.


newval := oldval * fact + 1

id1 := id2 * id3 + 1

MULT id2,id3,temp1

Intermediates Codes (Quadraples)



Code Optimizer:

The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space.


MULT id2,id3,temp1


Code Generator:

Produces the target language in a specific architecture.

The target program is normally is a relocatable object file containing the machine codes.

Ex: ( assume that we have an architecture with instructions whose at least one of its operands is a machine register)





b. Differentiate between

(i) Compiler and interpreterAns. Acompilertranslates a complete source program into machine code. The whole source code file is compiled in one go, and a complete, compiled version of the file is produced. This can be saved on some secondary storage medium (e.g. floppy disk, hard disk...). This means that:

The program can only be executed once translation is complete

ANY changes to the source code require a complete recompilation.

Aninterpreter, on the other hand, provides a means by which a program written in source language can be understood and executed by the CPU line by line. As the first line is encountered by the interpreter, it is translated and executed. Then it moves to the next line of source code and repeats the process. This means that: The interpreter is a program which is loaded into memory alongside the source program Statements from the source program are fetched and executed one by one No copy of the translation exists, and if the program is to be re-run, it has to be interpreted all over again.(ii) Macro processor and Pre processor Ans. Amacro processoris a program that copies a stream of text from one place to another, making a systematic set of replacements as it does so. Macro processors are often embedded in other programs, such as assemblers and compilers. Sometimes they are standalone programs that can be used to process any kind of text. Macro processors have been used for language expansion, for systematic text replacements that require decision making, and for text reformatting.

Apreprocessoris a program that processes its input data to produce output that is used as input to another program. The output is said to be apreprocessedform of the input data, which is often used by some subsequent programs like compilers. The amount and kind of processing done depends on the nature of the preprocessor; some preprocessors are only capable of performing relatively simple textual substitutions and macro expansions

c. What is bootstrapping? Explain with example.Ans. Incomputer science,bootstrappingis the process of writing acompiler(orassembler) in the targetprogramming languagewhich it is intended to compile. Applying this technique leads to aself-hostingcompiler.

Bootstrapping a compiler has the following advantages:

it is a non-trivial test of the language being compiled;

compiler developers only need to know the language being compiled;

improvements to the compiler's back-end improve not only general purpose programs but also the compiler itself; and

it is a comprehensive consistency check as it should be able to reproduce its own object code.

d. Explain Thompsons construction algorithm.Ans. Converting A Regular Expression into A NFA (Thomsons Construction):

This is one way to convert a regular expression into a NFA.

There can be other ways (much efficient) for the conversion.

Thomsons Construction is simple and systematic method. It guarantees that the resulting NFA will have exactly one final state, and one start state.

Construction starts from simplest parts (alphabet symbols). To create a NFA for a complex regular expression, NFAs of its sub-expressions are combined to create its NFA,

To recognize an empty string (

To recognize a symbol a in the alphabet (

If N(r1) and N(r2) are NFAs for regular expressions r1 and r2

For regular expression r1 | r2

For regular expression r1 r2

For regular expression r*

e. Explain the need of lexical analyzer in compilation process. Also explain the concept of input buffering and preliminary scanning.Ans. Lexical analyzer is needed in compilation process because of the reasons given below:Efficiency: A lexer may do the simple parts of the work faster than the more general parser can. Furthermore, the size of a system that is split in two may be smaller than a combined system. This may seem paradoxical but, as we shall see, there is a non-linear factor involved which may make a separated system smaller than a combined system.

Modularity: The syntactical description of the language need not be cluttered with small lexical details such as white-space and comments.

Tradition: Languages are often designed with separate lexical and syntactical phases in mind, and the standard documents of such languages typically separate lexical and syntactical elements of the languages.

Input buffering Lexical analyzer may need to look at least a character ahead to make a token decision. Buffering: to reduce overhead required to process a single character

Preliminary scanning

Remove comments and white spaces to make effective token recognition. f. Design NDFA with move for the following regular expression:


Q2. . Attempt any TWO parts of the following.

2X10=20a. Write the algorithm to find FIRST and FOLLOW for predictive parser. Also write the algorithm for constructing predictive parsing table.Ans. FIRST(X):

If X is a terminal symbol (FIRST(X)={X}

If X(a then FIRST(X)={a}. If X is a non-terminal symbol and X ( ( is a production rule ( ( is in FIRST(X).

If X is a non-terminal symbol and X ( Y1Y2..Yn is a production rule (if a terminal a in FIRST(Yi) and ( is in all FIRST(Yj) for j=1,...,i-1 then a is in FIRST(X). (if ( is in all FIRST(Yj) for j=1,...,n then ( is in FIRST(X).

If X is ( (FIRST(X)={(}

If X is Y1Y2..Yn (if a terminal a in FIRST(Yi) and ( is in all FIRST(Yj) for j=1,...,i-1 then a is in FIRST(X). (if ( is in all FIRST(Yj) for j=1,...,n then ( is in FIRST(X).


If S is the start symbol ( $ is in FOLLOW(S)

if A ( (B( is a production rule (everything in FIRST(() is FOLLOW(B) except (

If ( A ( (B is a production rule ) or ( A ( (B( is a production rule and ( is in FIRST(() ) ( everything in FOLLOW(A) is in FOLLOW(B). Constructing LL(1) Parsing Table -- Algorithm for each production rule A ( ( of a grammar G

for each terminal