SlideShare une entreprise Scribd logo
1  sur  51
Télécharger pour lire hors ligne
DEPARTMENT OF INFORMATION TECHNOLOGY
GATE QUESTIONS
COMPILER DESIGN
III IT
2018 – 2019 EVEN
Question Paper Years
2014 to 2017 and Few question from 1998 to 2013
Prepared by
R. ARTHY, AP/IT
GATE SYLLABUS
CS Computer Science and Information Technology
Section1: Engineering Mathematics
Discrete Mathematics: Propositional and first order logic. Sets, relations, functions, partial orders and
lattices. Groups. Graphs: connectivity, matching, coloring. Combinatorics: counting, recurrence
relations, generating functions. Linear Algebra: Matrices, determinants, system of linear equations,
eigenvalues and eigenvectors, LU decomposition. Calculus: Limits, continuity and differentiability.
Maxima and minima. Mean value theorem. Integration. Probability: Random variables. Uniform,
normal, exponential, poisson and binomial distributions. Mean, median, mode and standard deviation.
Conditional probability and Bayes theorem.
Computer Science and Information Technology
Section 2: Digital Logic
Boolean algebra. Combinational and sequential circuits. Minimization. Number representations and
computer arithmetic (fixed and floating point).
Section 3: Computer Organization and Architecture
Machine instructions and addressing modes. ALU, data‐path and control unit. Instruction pipelining.
Memory hierarchy: cache, main memory and secondary storage; I/O interface (interrupt and DMA
mode).
Section 4: Programming and Data Structures
Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary search trees, binary
heaps, graphs.
Section 5: Algorithms
Searching, sorting, hashing. Asymptotic worst case time and space complexity. Algorithm design
techniques: greedy, dynamic programming and divide‐and‐conquer. Graph search, minimum spanning
trees, shortest paths.
Section 6: Theory of Computation
Regular expressions and finite automata. Context-free grammars and push-down automata.
Regular and contex-free languages, pumping lemma. Turing machines and undecidability.
Section 7: Compiler Design
Lexical analysis, parsing, syntax-directed translation. Runtime environments. Intermediate code
generation.
Section 8: Operating System
Processes, threads, inter‐process communication, concurrency and synchronization. Deadlock. CPU
scheduling. Memory management and virtual memory. File systems.
CS6660 COMPILER DESIGN
OBJECTIVES:
The student should be made to:
Learn the design principles of a Compiler.
Learn the various parsing techniques and different levels of translation.
Learn how to optimize and effectively generate machine codes.
UNIT I INTRODUCTION TO COMPILERS
Translators-Compilation and Interpretation-Language processors -The Phases of Compiler-
Errors Encountered in Different Phases-The Grouping of Phases-Compiler Construction
Tools - Programming Language basics.
UNIT II LEXICAL ANALYSIS
Need and Role of Lexical Analyzer-Lexical Errors-Expressing Tokens by Regular
Expressions-Converting Regular Expression to DFA- Minimization of DFA-Language for
Specifying Lexical Analyzers-LEX-Design of Lexical Analyzer for a sample Language.
UNIT III SYNTAX ANALYSIS
Need and Role of the Parser-Context Free Grammars -Top Down Parsing -General
Strategies-Recursive Descent Parser Predictive Parser-LL(1) Parser-Shift Reduce Parser-LR
Parser-LR (0)Item-Construction of SLR Parsing Table -Introduction to LALR Parser - Error
Handling and Recovery in Syntax Analyzer-YACC-Design of a syntax Analyzer for a
Sample Language .
UNIT IV SYNTAX DIRECTED TRANSLATION & RUN TIME ENVIRONMENT
Syntax directed Definitions-Construction of Syntax Tree-Bottom-up Evaluation of S-
Attribute Definitions- Design of predictive translator - Type Systems-Specification of a
simple type checker-Equivalence of Type Expressions-Type Conversions.
RUN-TIME ENVIRONMENT: Source Language Issues-Storage Organization-Storage
Allocation-Parameter Passing-Symbol Tables-Dynamic Storage Allocation-Storage
Allocation in FORTAN.
UNIT V CODE OPTIMIZATION AND CODE GENERATION
Principal Sources of Optimization-DAG- Optimization of Basic Blocks-Global Data Flow
Analysis-Efficient Data Flow Algorithms-Issues in Design of a Code Generator - A Simple
Code Generator Algorithm.
OUTCOMES:
Students should be able to ...
 Discuss about the compiler translator to process a programming language and phases of
compilation
 Demonstrate lexical analyzer to generate valid tokens
 Demonstrate various parsers to syntactically check the source code using context free
grammar
 Construct a type checked syntax tree
 Apply various code optimization techniques to generate optimized machine codes
DEPARTMENT OF INFORMATION TECHNOLOGY
GATE QUESTIONS
CS6660 – COMPILER DESIGN
INDEX
Academic Year : 2018 – 2019 EVEN
Question Paper Year : 2014 to 2017 and Few question from 1998 to 2013
Name of the Subject In – Charge : Mrs. R. Arthy, AP/IT
S. No. Name of the Topic
Number of
Questions
Page Number
1 Introduction to Compiler 11 1
2 Lexical Analyzer 13 6
3 Parsing 20 14
4
Syntax Directed Translation and
Run Time Environemnt
6 31
5 Code generation 11 35
Faculty In-charge GATE Coordinator HoD/IT
[GATE – Compiler Design] Page 1
DEPARTMENT OF INFORMATION TECHNOLOGY
GATE QUESTIONS
CS6660 – COMPILER DESIGN
UNIT I – INTRODUCTION TO COMPILER
1. The process of assigning load addresses to the various parts of the program and
adjusting the code and data in the program to reflect the assigned addresses is called
(GATE CS 2001)
a) Assembly
b) Parsing
c) Relocation
d) Symbol resolution
Answer: (c)
Relocation is the process of replacing symbolic references or names of libraries with actual
usable addresses in memory before running a program. It is typically done by the linker
during compilation (at compile time), although it can be done at runtime by a relocating
loader. Compilers or assemblers typically generate the executable with zero as the lower-
most starting address. Before the execution of object code, these addresses should be adjusted
so that they denote the correct runtime addresses.
Relocation is typically done in two steps:
1. Each object code has various sections like code, data, .bss etc. To combine all the objects
to a single executable, the linker merges all sections of similar type into a single section of
that type. The linker then assigns runtime addresses to each section and each symbol. At this
point, the code (functions) and data (global variables) will have unique runtime addresses.
2. Each section refers to one or more symbols which should be modified so that they point to
the correct runtime addresses.
[GATE – Compiler Design] Page 2
2. Consider a program P that consists of two source modules M1 and M2 contained in
two different files. If M1 contains a reference to a function defined in M2 the reference
will be resolved at (GATE CS 2004)
a) Edit time
b) Compile time
c) Link time
d) Load time
Answer (c)
Compiler transforms source code into the target language. The target language is generally in
binary form known as object code. Typically, an object file can contain three kinds of
symbols:
* defined symbols, which allow it to be called by other modules,
* undefined symbols, which call the other modules where these symbols are defined, and
* local symbols, used internally within the object file to facilitate relocation.
When a program comprises multiple object files, the linker combines these files into a unified
executable program, resolving the symbols as it goes along.
3. A loader is a program that
A) program that places programs into memory and prepares them for execution.
B) program that automates the translation of assembly language into machine
language.
C) program that accepts a program written in a high level language and produces as
object program
D) None of these
Answer: A
4. A system program that setup an executable program in main memory ready for
execution is
A) assembler
B) linker
C) loader
D) load and go
Answer: C
[GATE – Compiler Design] Page 3
5. Uniform symbol table
A) contains all constants in the program
B) is a permanent table of decision rules in the form of patterns for matching with the
uniform symbol table to discover syntactic structure.
C) consists of full or partial list of the token's as they appear in the program created by
Lexical analysis and used for syntax analysis and interpretation.
D) a permanent table which lists all key words and special symbols of the language in
symbolic form.
Answer: C
6. Assembler is a program that
A) places programs into memory and prepares them for execution
B) automates the translation of assembly language into machine language
C) accepts a program written in a high level language and produces an object
program.
D) None of these
Answer: B
7. Compiler can diagnose
A) grammatical errors only
B) logical errors only
C) grammatical as well as logical errors
D) None of these
Answer: A
8. A system program that set-up an executable program in main memory ready for
execution is
A) assembler
B) linker
C) loader
D) text editor
Answer: C
[GATE – Compiler Design] Page 4
9. Type checking is normally done during
(a) lexical analysis
(b) syntax analysis
(c) syntax directed translation
(d) code optimization
Answer: C
10. Match the following according to input(from the left column) to the compiler
phase(in the right column) that process it:
A P -> (ii), Q -> (iii), R -> (iv), S -> (i)
B P -> (ii), Q -> (i), R -> (iii), S -> (iv)
C P -> (iii), Q -> (iv), R -> (i), S -> (ii)
D P -> (i), Q -> (iv), R -> (ii), S -> (iii)
GATE-CS-2017 (Set 2)
Answer: (C)
Explanation: P – iii Syntax tree is input to semantic analyser
Q – iv Character stream is input to lexical analyser.
R – i Intermediate code is input to code generator
S -ii Token stream is input to syntax analyser.
11. Which one of the following statements is FALSE?
A Context-free grammar can be used to specify both lexical and syntax rules.
B Type checking is done before parsing.
C High-level language programs can be translated to different Intermediate
Representations.
[GATE – Compiler Design] Page 5
D Arguments to a function can be passed using the program stack.
GATE CS 2018
Answer: (B)
Explanation: Type checking is done at semantic analysis phase and parsing is done at syntax
analysis phase. And we know Syntax analysis phase comes before semantic analysis. So
Option (B) is False.
All other options seems Correct.
[GATE – Compiler Design] Page 6
UNIT II – LEXICAL ANALYSIS
1. In some programming languages, an identifier is permitted to be a letter following by
any number of letters or digits. If L and D denote the sets of letters and digits
respectively, which of the following expressions defines an identifier? GATE – CS - 1995
Answer : B
2. The minimum possible number of states of a deterministic finite automaton that
accepts a regular language L = {w1aw2 | w1, w2 ∈{a,b}* , |w1| = 2, w2>=3} is_______
A 3
B 5
C 8
D 7
GATE-CS-2017 (Set 2)
Answer: C
3. The length of the shortest string NOT in the language (over Σ = {a, b}) of the
following regular expression is ______________.
a*b*(ba)*a*
A 2
B 3
C 4
D 5
GATE-CS-2014-(Set-3)
Answer: (B)
[GATE – Compiler Design] Page 7
Explanation: All the strings that can be generated till length-2 are definitely present in this
language.
Now, let’s look at the strings of length-3; {aaa, aab, aba, abb, baa, bab, bba, bbb} . String
“bab” cannot be generated from given language. so, the string “bab” is the shortest string not
acceptable by given regular expression.
4.
of the following is CORRECT?
A Only (I)
B Only (II)
C Both (I) and (II)
D Neither (I) nor (II)
GATE-CS-2014-(Set-2)
Answer: (A)
Explanation: L1.L2 is definitely regular, since regular languages are closed under
concatenation.
But L1.L2 = { an
bn
| n ≥ 0 } is not correct. Because both variable are independent of each
other.
L1.L2 = { an
bm
| n ≥ 0 , m ≥ 0 } is possible.
Therefore, only statement (i) is TRUE.
5. Let L1 = {w ∈ {0,1}∗ | w has at least as many occurrences of (110)’s as (011)’s}.
Let L2 = { ∈ {0,1}∗ | w has at least as many occurrences of (000)’s as (111)’s}.
Which one of the following is TRUE?
A L1 is regular but not L2
B L2 is regular but not L!
C Both L2 and L1 are regular
D Neither L1 nor L2 are regular
[GATE – Compiler Design] Page 8
GATE-CS-2014-(Set-2)
Answer: (A)
Explanation: L1 is regular
let us consider the string 011011011011
In this string, number of occurrences of 011 are 4 but when we see here 110 is also occurred
and the number of occurrence of 110 is 3.
Note that if i add a 0 at the last of string we can have same number of occurrences of 011 and
110 so this string is accepted. We can say if the string is ending with 011 so by appending a 0
we can make 110 also.
Now string2: 110110110110 in this number of occurrences of 110 is 4 and 011 is 3 which
already satisfy the condition
So we can observe here that whenever 110 will be there string will be accepted
So with this idea we can build an automata for this. Therefore, it is regular.
6. Which one of the following is TRUE? GATECS2014Q25
A A
B B
C C
D D
GATE-CS-2014-(Set-1)
Answer: (C)
Explanation: (A) L = {a n b n |n >= 0} is not regular because there does not exists a finite
automaton that can
derive this grammar. Intuitively, finite automaton has finite memory, hence it can’t track
number of as. It is a standard CFL though.
(B) L = {a n b n |n is prime} is again not regular because there is no way to remember/check
if
current n is prime or not. Hence, no finite automaton exists to derive this grammar, thus it
[GATE – Compiler Design] Page 9
is not regular.
(C) L = {w|w has 3k+1 bs} is a regular language because k is a fixed constant and we can
easily
emulate L as a ∗ ba ∗ …..ba ∗ such that there are exactly 3k + 1 bs and a ∗ s surrounding each
b in the grammar.
D) L = {ww| w ∈ Σ ∗ } is again not a regular grammar, infact it is not even a CFG. There is
no
way to remember and derive double word using finite automaton.
Hence, correct answer would be (C).
7. The number of tokens in the following C statement is (GATE 2000)
printf("i = %d, &i = %x", i, &i);
A 3
B 26
C 10
D 21
Answer - C
Tokens are:
printf
(
"i=%d, &i=%x"
,
i
,
&
i
)
;
8. In a compiler, keywords of a language are recognized during
A parsing of the program
B the code generation
C the lexical analysis of the program
[GATE – Compiler Design] Page 10
D dataflow analysis
GATE CS 2011
Answer - C
9. The lexical analysis for a modern computer language such as Java needs the power of
which one of the following machine models in a necessary and sufficient sense?
A Finite state automata
B Deterministic pushdown automata
C Non-Deterministic pushdown automata
D Turing Machine
GATE CS 2011
Answer – A
10. Consider the following statements: (I) The output of a lexical analyzer is groups of
characters. (II) Total number of tokens in printf("i=%d, &i=%x", i, &i); are 11. (III)
Symbol table can be implementation by using array and hash table but not tree. Which
of the following statement(s) is/are correct?
A Only (I)
B Only (II) and (III)
C All (I), (II), and (III)
D None of these
GATE CS Mock 2018 | Set 2
Answer: (D)
Explanation: (I) The output of a lexical analyzer is tokens.
(II) Total number of tokens in printf("i=%d, &i=%x", i, &i); are 10.
(III) Symbol table can be implementation by using array, hash table, tree and linked lists.
So, option (D) is correct.
11. Consider the DFAs M and N given above. The number of states in a minimal DFA
that accepts the language L(M) ∩ L(N) is __________.
[GATE – Compiler Design] Page 11
A 0
B 1
C 2
D 3
GATE-CS-2015 (Set 1)
Answer: (B)
Explanation: In DFA M: all strings must end with ‘a’.
In DFA N: all strings must end with ‘b’.
So the intersection is empty.
For an empty language, only one state is required in DFA. The state is non-accepting and
remains on itself for all characters of alphabet.
12. A lexical analyzer uses the following patterns to recognize three tokens T1, T2, and
T3 over the alphabet {a,b,c}. T1: a?(b∣c)*a T2: b?(a∣c)*b T3: c?(b∣a)*c Note that ‘x?’
means 0 or 1 occurrence of the symbol x. Note also that the analyzer outputs the token
that matches the longest possible prefix. If the string bbaacabc is processes by the
analyzer, which one of the following is the sequence of tokens it outputs?
A T1T2T3
B T1T1T3
C T2T1T3
D T3T3
GATE CS 2018
Answer: (D)
Explanation: 0 or 1 occurrence of the symbol x.
T1 : (b+c)* a + a(b+c)* a
T2 : (a+c)* b + b(a+c)* b
T3 : (b+a)* c + c(b+a)* c
[GATE – Compiler Design] Page 12
Given String : bbaacabc
Longest matching prefix is ” bbaac ” (Which can be generated by T3)
The remaining part (after Prefix) “abc” (Can be generated by T3)
So, the answer is T3T3
13.
A {q0, q1, q2}
B {q0, q1}
C {q0, q1, q2, q3}
D {q3}
GATE-CS-2014-(Set-1)
Answer: (A)
Explanation:
[GATE – Compiler Design] Page 13
So, q0, q1 and q2 are reachable states for the input string 0011, but q3 is not.
So, option (A) is answer.
[GATE – Compiler Design] Page 14
UNIT III – SYNTAX ANALYSIS
1. Consider the following languages.
L1 = {ap
| p is a prime number}
L2 = {an
bm
c2m
| n >= 0, m >= 0}
L3 = {an
bn
c2n
| n >= 0}
L4 = {an
bn
| n >= 1}
Which of the following are CORRECT ?
I. L1 is context free but not regular.
II. L2 is not context free.
III. L3 is not context free but recursive
IV. L4 is deterministic context free
A I, II and IV only
B II and III only
C I and IV only
D III and IV only
GATE-CS-2017 (Set 2)
Answer: (D)
Explanation:
L1 =a p | p is prime : is context sensitive not context free
L2 is context free
L3 is not context free because we are not sure when to pop b and push a,
because it is comparison between three consecutive terminals
Clearly L4 is deterministic context free because
we are sure of pushing a into stack first and on
seeing b we are sure of popping a.
[GATE – Compiler Design] Page 15
2. Let L1 and L2 be any context-free language and R be any regular language. Then,
which of the following is correct ?
I. L1 ∪ L2 is context-free.
II. L1' is context-free.
III. L1-R is context-free.
IV. L1 ∩ L2
A I, II and IV only
B I and III only
C II and IV only
D I only
GATE-CS-2017 (Set 2)
Answer: (B)
Explanation: Context free language are closed under union and difference with regular
language.
It is not closed under complementation and intersection . Complement of CFL is recursive
language.
3. Identity the language generated by following grammar where S is the start variable.
S --> XY
X --> aX | a
Y --> aYb | ∈
A {am
bn
| m>=n, n>0 }
B {am
bn
| m>=n, n>=0 }
C {am
bn
| m>n, n>=0 }
D {am
bn
| m>n, n>0 }
GATE-CS-2017 (Set 2)
Answer: (C)
Explanation:
S --> XY
X --> aX | a // This produces only "a"
[GATE – Compiler Design] Page 16
Y --> aYb | ∈ // This produces and "a" for every "b"
Option (A) and (B) are wrong because n can be zero also
due to epsilon in Y
Option (D) is wrong because Y–>aYb produces equal number of a’s and b’s.
Since there is one variable X which produces at least one a.
Therefore numbers of a’s are always greater than numbers of b’s.
4. Which of the following statements is false? (GATE CS 2001)
a) An unambiguous grammar has same leftmost and rightmost derivation
b) An LL(1) parser is a top-down parser
c) LALR is more powerful than SLR
d) An ambiguous grammar can never be LR(k) for any k
Answer: (a)
If a grammar has more than one leftmost (or rightmost) derivation for a single sentential
form, the grammar is ambiguous. The leftmost and rightmost derivations for a sentential form
may differ, even in an unambiguous grammar
5. Which of the following derivations does a top-down parser use while parsing an input
string? The input is assumed to be scanned in left to right order (GATE CS 2000).
(a) Leftmost derivation
(b) Leftmost derivation traced out in reverse
(c) Rightmost derivation
(d) Rightmost derivation traced out in reverse
Answer (a)
Top-down parsing (LL)
In top down parsing, we just start with the start symbol and compare the right side of the
different productions against the first piece of input to see which of the productions should be
used.
A top down parser is called LL parser because it parses the input from Left to right, and
constructs a Leftmost derivation of the sentence.
Algorithm (Top Down Parsing)
a) In the current string, choose leftmost nonterminal.
b) Choose a production for the chosen nonterminal.
[GATE – Compiler Design] Page 17
c) In the string, replace the nonterminal by the right-hand-side
of the rule.
d) Repeat until no more nonterminals.
LL grammars are often classified by numbers, such as LL(1), LL(0) and so on. The number
in the parenthesis tells the maximum number of terminals we may have to look at at a time to
choose the right production at any point in the grammar.
The most common (and useful) kind of LL grammar is LL(1) where you can always choose
the right production by looking at only the first terminal on the input at any given time. With
LL(2) you have to look at two symbols, and so on. There exist grammars that are not LL(k)
grammars for any fixed value of k at all, and they are sadly quite common.
Let us see an example of top down parsing for following grammar. Let input string be ax.
S -> Ax
A -> a
A -> b
An LL(1) parser starts with S and asks “which production should I attempt?” Naturally, it
predicts the only alternative of S. From there it tries to match A by calling method A (in a
recursive-descent parser). Lookahead a predicts production
A -> a
The parser matches a, returns to S and matches x. Done. The derivation tree is:
S
/ 
A x
|
a
6. Given the following expression grammar:
E -> E * F | F+E | F
F -> F-F | id
which of the following is true? (GATE CS 2000)
(a) * has higher precedence than +
(b) – has higher precedence than *
(c) + and — have same precedence
(d) + has higher precedence than *
[GATE – Compiler Design] Page 18
Answer(b)
Precedence in a grammar is enforced by making sure that a production rule with higher
precedence operator will never produce an expression with operator with lower precedence.
In the given grammar ‘-’ has higher precedence than ‘*’
7. Consider the grammar defined by the following production rules, with two operators
∗ and +
S --> T * P
T --> U | T * U
P --> Q + P | Q
Q --> Id
U --> Id
Which one of the following is TRUE?
A + is left associative, while ∗ is right associative
B + is right associative, while ∗ is left associative
C Both + and ∗ are right associative
D Both + and ∗ are left associative
GATE-CS-2014-(Set-2)
Answer: (B)
Explanation: From the grammar we can find out associative by looking at grammar.
Let us consider the 2nd production
T -> T * U
T is generating T*U recursively (left recursive) so * is
left associative.
Similarly
P -> Q + P
Right recursion so + is right associative.
So option B is correct.
NOTE: Above is the shortcut trick that can be observed after drawing
few parse trees.
[GATE – Compiler Design] Page 19
One can also find out correct answer by drawing the parse tree.
8. Consider the following grammar
p --> xQRS
Q --> yz|z
R --> w|∈
S -> y
Which is FOLLOW(Q)?
(A) {R}
(B) {w}
(C) {w, y}
(D) {w, ∉}
Answer: C
Follow(Q) = {First(R)}
First(R) = {w, ∈}
Follow(Q) = {w, First(S)}
First(S) = {y}
Therefore Follow(Q) = {w, y}
9. Which one of the following grammars is free from left recursion?
A A
B B
C C
[GATE – Compiler Design] Page 20
D D
GATE-CS-2016 (Set 2)
Answer: (B)
Explanation: Grammar A has direct left recursion because of the production rule: A->Aa.
Grammar C has indirect left recursion because of the production rules:S-> Aa and A->Sc
Grammar D has indirect left recursion because of production rules : A-> Bd and B-> Ae
Grammar B doesn’t have any left recursion (neither direct nor indirect).
10. If G is grammar with productions
S → SaS | aSb | bSa | SS | ∈
where S is the start variable, then which one of the following is not generated by G?
A abab
B aaab
C abbaa
D babba
GATE-CS-2017 (Set 1)
Answer: D
11. Consider the grammar with non-terminals N = {S,C,S1 },terminals T={a,b,i,t,e},
with S as the start symbol, and the following set of rules:
S --> iCtSS1|a
S1 --> eS|ϵ
C --> b
The grammar is NOT LL(1) because:
A it is left recursive
B it is right recursive
C it is ambiguous
D It is not context-free.
GATE-CS-2007
Answer: (C)
Explanation:
[GATE – Compiler Design] Page 21
A LL(1) grammar doesn’t give to multiple entries in a single cell of its parsing table. It has
only single entry in a single cell, hence it should be unambiguous.
Option A is wrong. Grammar is not left recursive. For a grammar to be left recursive a
production should be of form A->Ab, where A is a single Non-Terminal and b is any string of
grammar symbols.
Option B is wrong. Because a right recursive grammar has nothing to do with LL(1).
Option D is wrong. Because the given grammar is clearly a Context Free Grammar. A
grammar is CFG if it has productions of the form A->(V∪ T)* , where A is a single non-
terminal and V is a set of Non-terminals and T is a set of Terminals.
Hence Option C should be the correct one. i.e. the grammar is ambiguous.
But let’s see how the grammar is ambiguous.
If the grammar is ambiguous then it should give multiple entry in a cell while making its
parsing table. And Parse table is made with the aid of two functions : FIRST and FOLLOW.
A parsing table of a grammar will not have multiple entries in a cell( i.e. will be a LL(1)
grammar) if and only if the following conditions hold for each production of the form A->α|β
1) FIRST(α) ∩ FIRST(β) = Φ
2) if FIRST(α) contains ‘ ε ‘ then FIRST(α) ∩ FOLLOW (A) = Φ and vice-versa.
Now,
For the production , S->iCtSS1|a, rule 1 is satisfied, because FIRST(iCtSS1) ∩ FIRST(a) =
{i} ∩ {a} = Φ
For the production, S1->eS|ε, rule 1 is satisfied, as FIRST(eS) ∩ FIRST(ε) = {e} ∩ {ε} = Φ
. But here due to ‘ε’ in FIRST, we have to check for rule 2. FIRST(eS) ∩ FOLLOW(S1) =
[GATE – Compiler Design] Page 22
{e} ∩ {e, $} ≠ Φ . Hence rule 2 fails in this production rule. Therefore there will be multiple
entries in the parsing table, hence the grammar is ambiguous and not LL(1).
12. Consider the following expression grammar G.
E -> E - T | T
T -> T + F | F
F -> (E) | id
Which of the following grammars are not left recursive, but equivalent to G.
A)
E -> E - T | T
T -> T + F | F
F -> (E) | id
B)
E -> TE'
E' -> -TE' | ε
T -> T + F | F
F -> (E) | id
C)
E -> TX
X -> -TX | ε
T -> FY
Y -> +FY | ε
F -> (E) | id
D)
E -> TX | (TX)
X -> -TX | +TX | ε
T -> id
A A
[GATE – Compiler Design] Page 23
B B
C C
D D
GATE-CS-2017 (Set 2)
Answer: (C)
Explanation: We know for left recursion : A -> Aα/β
After removing left recursion it can be written as
A->βA’
A’->αA’/ε
Thus for : E->E- T/T
α= -T , β= T . thus new production after removing left recursion
is E->TE’ and E’->- TE’/ ε
T->FT’ and T’->+FT’/ ε
F->(E)/id .
13. For the grammar below, a partial LL(1) parsing table is also presented along with
the grammar. Entries that need to be filled are indicated as E1, E2, and E3. epsilon is
the empty string, $ indicates end of input, and, | separates alternate right hand sides of
productions.
A A
[GATE – Compiler Design] Page 24
B B
C C
D D
GATE CS 2012
Answer: (A)
Explanation:
First(X) - It is the set of terminals that begin the
strings derivable from X.
Follow(X) - It is the set of terminals that can appear
immediately to the right of X in some sentential
form.
Now in the above question,
FIRST(S) = { a, b, epsilon}
FIRST(A) = FIRST(S) = { a, b, epsilon}
FIRST(B) = FIRST(S) = { a, b, epsilon}
FOLLOW (A) = { b , a }
FOLLOW (S) = { $ } U FOLLOW (A) = { b , a , $ }
FOLLOW (B) = FOLLOW (S) = { b ,a , $ }
epsilon corresponds to empty string.
14. Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?
(GATE CS 2003)
(a) Removing left recursion alone
(b) Factoring the grammar alone
(c) Removing left recursion and factoring the grammar
[GATE – Compiler Design] Page 25
(d) None of the above
Answer: (D)
Explanation: Removing left recursion and factoring the grammar do not suffice to convert an
arbitrary CFG to LL(1) grammar.
15. What is the maximum number of reduce moves that can be taken by a bottom-up
parser for a grammar with no epsilon- and unit-production (i.e., of type A -> є and A ->
a) to parse a string with n tokens?
A n/2
B n-1
C 2n-1
D 2n
GATE CS 2013
Answer: (B)
Explanation: Given in the question, a grammar with no epsilon- and unit-production (i.e., of
type A -> є and A -> a).
To get maximum number of Reduce moves, we should make sure than in each sentential
form only one terminal is reduced. Since there is no unit production, so last 2 tokens will take
only 1 move.
So To Reduce input string of n tokens, first Reduce n-2 tokens using n-2 reduce moves and
then Reduce last 2 tokens using production which has . So total of n-2+1 = n-1 Reduce
moves.
Suppose the string is abcd. ( n = 4 ).
We can write the grammar which accepts this string as follows:
S->aB
B->bC
C->cd
[GATE – Compiler Design] Page 26
The Right Most Derivation for the above is:
S -> aB ( Reduction 3 )
-> abC ( Reduction 2 )
-> abcd ( Reduction 1 )
We can see here that no production is for unit or epsilon. Hence 3 reductions here.
We can get less number of reductions with some other grammar which also does’t produce
unit or epsilon productions,
S->abA
A-> cd
The Right Most Derivation for the above as:
S -> abA ( Reduction 2 )
-> abcd ( Reduction 1 )
Hence 2 reductions.
But we are interested in knowing the maximum number of reductions which comes from the
1st grammar. Hence total 3 reductions as maximum, which is ( n – 1) as n = 4 here.
Thus, Option B.
16. Consider the following grammar.
S -> S * E
S -> E
E -> F + E
E -> F
F -> id
Consider the following LR(0) items corresponding to the grammar above.
(i) S -> S * .E
(ii) E -> F. + E
(iii) E -> F + .E
[GATE – Compiler Design] Page 27
Given the items above, which two of them will appear in the same set in the canonical
sets-of-items for the grammar?
A (i) and (ii)
B (ii) and (iii)
C (i) and (iii)
D None of the above
GATE-CS-2006
Answer: (D)
Explanation: Let’s make the LR(0) set of items. First we need to augment the grammar with
the production rule S’ -> .S , then we need to find closure of items in a set to complete a set.
Below are the LR(0) sets of items.
[GATE – Compiler Design] Page 28
17. Assume that the SLR parser for a grammar G has n1 states and the LALR parser
for G has n2 states. The relationship between nl and n2 is (GATE CS 2003)
(a) n1 is necessarily less than n2
(b) n1 is necessarily equal to n2
(c) n1 is necessarily greater than n2
(d) none of the above
GATE-CS-2017
Answer: (B)
18. Which of the following statements about the parser is/are correct?
I. Canonical LR is more powerful than SLR.
II. SLR is more powerful than LALR.
III. SLR is more powerful than canonical LR.
A I only
B II only
C III only
D II and III only
GATE-CS-2017 (Set 2)
Answer: A
19. A canonical set of items is given below
S --> L. > R
Q --> R.
On input symbol < the set has
(A) a shift-reduce conflict and a reduce-reduce conflict.
(B) a shift-reduce conflict but not a reduce-reduce conflict.
(C) a reduce-reduce conflict but not a shift-reduce conflict.
(D) neither a shift-reduce nor a reduce-reduce conflict.
GATE CS 2014
Answer: (D)
[GATE – Compiler Design] Page 29
Explanation: The question is asked with respect to the symbol ‘ < ‘ which is not present in
the given canonical set of items. Hence it is neither a shift-reduce conflict nor a reduce-
reduce conflict on symbol ‘<‘.
Hence D is the correct option.
But if the question would have asked with respect to the symbol ‘ > ‘ then it would have
been a shift-reduce conflict.
20. Which of the following statements related to merging of the two sets in the
corresponding LALR parser is/are FALSE?
1. Cannot be merged since look aheads are different.
2. Can be merged but will result in S-R conflict.
3. Can be merged but will result in R-R conflict.
4. Cannot be merged since goto on c will lead to two different sets.
A 1 only
B 2 only
C 1 and 4 only
D 1, 2, 3, and 4
GATE CS 2013
Answer: (D)
Explanation: The given two LR(1) set of items are :
X -> c.X, c/d
X -> .cX, c/d
X -> .d, c/d
and
X -> c.X, $
X -> .cX, $
X -> .d, $
The symbols/terminals after the comma are Look-Ahead symbols.
[GATE – Compiler Design] Page 30
These are the sets of LR(1) ( LR(1) is also called CLR(1) ) items.
The LALR(1) parser combines those set of LR(1) items which are identical with respect to
their 1st component but different with respect to their 2nd component.
In a production rule of a LR(1) set of items, ( A -> B , c ) , A->B is the 1st component , and
the Look-Ahead set of symbols, which is c here, is the second component.
Now we can see that in the sets given, 1st component of the corresponding production rule is
identical in both sets, and they only differ in 2nd component ( i.e. their look-ahead symbols)
hence we can combine these sets to make a a single set which would be :
X -> c.X, c/d/$
X -> .cX, c/d/$
X -> .d, c/d/$
This is done to reduce the total number of parser states.
Now we can check the statements given.
Statement 1 : The statement is false, as merging has been done because 2nd components i.e.
look-ahead were different.
Statement 2 : In the merged set, we can’t see any Shift-Reduce conflict ( because no
reduction even possible, reduction would be possible when a production of form P -> q. is
present)
Statement 3 : In the merged set, we can’t see any Reduce-Reduce conflict ( same reason as
above, no reduction even possible, so no chances of R-R conflict )
Statement 4: This statement is also wrong, because goto is carried on Non-Terminals
symbols, not on terminal symbols, and c is a terminal symbol.
Thus, all statements are wrong, hence option D.
[GATE – Compiler Design] Page 31
UNIT IV – SYNTAX DIRECTED TRANSLATION AND RUN TIME
ENVIRONMENT
1. Consider the grammar with the following translation rules and E as the start symbol.
E -> E1 #T {E.value = E1.value * T.value}
| T {E.value = T.value}
T -> T1 & F {T.value = T1.value + F.value}
|F {T.value= F.value}
F -> num {F.value = num.value}
Compute E.value for the root of the parse tree for the expression:2 # 3 & 5 # 6 &4.
(GATE CS 2004)
a) 200
b) 180
c) 160
d) 40
Answer: (c)
Explanation:
We can calculate the value by constructing the parse tree for the expression 2 # 3 & 5 # 6 &4.
Alternatively, we can calculate by considering following precedence and associativity rules.
Precedence in a grammar is enforced by making sure that a production rule with higher
precedence operator will never produce an expression with operator with lower precedence.
In the given grammar ‘&’ has higher precedence than ‘#’.
Left associativity for operator * in a grammar is enforced by making sure that for a
production rule like S -> S1 * S2 in grammar, S2 should never produce an expression with *.
On the other hand, to ensure right associativity, S1 should never produce an expression with
*.
In the given grammar, both ‘#’ and & are left-associative.
So expression 2 # 3 & 5 # 6 &4 will become
((2 # (3 & 5)) # (6 & 4))
Let us apply translation rules, we get
((2 * (3 + 5)) * (6 + 4)) = 160.
[GATE – Compiler Design] Page 32
2. Consider the following Syntax Directed Translation Scheme (SDTS), with non-
terminals {S, A} and terminals {a, b}}.
Using the above SDTS, the output printed by a bottom-up parser, for the input aab is
A 1 3 2
B 2 2 3
C 2 3 1
D Syntax Error
GATE-CS-2016 (Set 1)
Answer: (C)
Explanation: Bottom up parser builds the parse tree from bottom to up, i.e from the given
string to the starting symbol. The given string is aab and starting symbol is S.
so the process is to start from aab and reach S.
=>aab ( given string)
=>aSb (after reduction by S->a, and hence print 2)
=>aA (after reduction by A->Sb, and hence print 3)
=>S (after reduction by S->aA, and hence print 1)
As we reach the starting symbol from the string, the string belongs to the language of the
grammar.
Another way to do the same thing is :- bottom up parser does the parsing by RMD in reverse.
RMD is as follows:
=>S
=> aA (hence, print 1)
=> aSb (hence, print 3)
=> aab (hence, print 2)
If we take in Reverse it will print : 231
[GATE – Compiler Design] Page 33
3. Which one of the following is NOT performed during compilation?
A Dynamic memory allocation
B Type checking
C Symbol table management
D Inline expansion
GATE-CS-2014-(Set-2)
4. In a bottom-up evaluation of a syntax directed definition, inherited attributes can
(A) always be evaluated
(B) be evaluated only if the definition is L--attributed
(C) be evaluated only if the definition has synthesized attributes
(D) never be evaluated
GATE CS 2003
Answer: (B)
Explanation: A Syntax Directed Definition (SDD) is called S Attributed if it has only
synthesized attributes.
L-Attributed Definitions contain both synthesized and inherited attributes but do not need to
build a dependency graph to evaluate them.
5, Consider the translation scheme shown below
S → T R
R → + T {print ('+');} R | ε
T → num {print (num.val);}
Here num is a token that represents an integer and num.val represents the
corresponding integer value. For an input string ‘9 + 5 + 2’, this translation scheme will
print
(A) 9 + 5 + 2
(B) 9 5 + 2 +
(C) 9 5 2 + +
(D) + + 9 5 2
GATE CS 2003
[GATE – Compiler Design] Page 34
Answer: (B)
Explanation: Let us make the parse tree for 9+5+2 in top down manner, left first derivation.
Steps:
1) Exapnd S->TR
2) apply T->Num...
3) apply R -> +T...
4) appy T->Num...
5) apply R-> +T..
6) apply T-> Num..
7) apply R-> epsilon
After printing through the print statement in the parse tree formed you will get the answer as
95+2+
6. Consider the syntax directed definition shown below.
S → id : = E {gen (id.place = E.place;);}
E → E1 + E2 {t = newtemp ( ); gen (t = El.place + E2.place;); E.place = t}
E → id {E.place = id.place;}
Here, gen is a function that generates the output code, and newtemp is a function that
returns the name of a new temporary variable on every call. Assume that ti’s are the
temporary variable names generated by newtemp.
For the statement ‘X: = Y + Z’, the 3-address code sequence generated by this definition
is
(A) X = Y + Z
(B) t1 = Y + Z; X = t1
(C) t1 =Y; t2 = t1 + Z; X = t2
(D) t1 = Y; t2 = Z; t3 = t1 + t2; X = t3
GATE CS 2003
Answer: (B)
Explanation: It must be B. The production E –> E + E is used only one time and hence only
one temporary variable is generated.
[GATE – Compiler Design] Page 35
UNIT V – CODE OPTIMIZATION AND CODE GENERATION
1. Consider the following grammar:
stmt -> if expr then else expr; stmt | ε
expr -> term relop term | term
term -> id | number
id -> a | b | c
number -> [0-9]
where relop is a relational operate (e.g < >, ….), ε refers to the empty statement, and if
,then, else are terminals. Consider a program P following the above grammar
containing ten if terminals. The number of control flows paths in P is ____________.
For example, the program
if e1 then e2 else e3
has 2 control flow paths, e1 -> e2 and e1 -> e3
A 20
B 1024
C 2048
D 10
GATE-CS-2017 (Set 1)
Answer: (B)
Explanation: Number of control flow paths for 10 if terminals
if then else ; stmt
if then else ; if then else ; stmt
…………..
10 times.
Observe that there is a semi-colon after every if structure.
Since, every if structure has 2 control flows.
Therefore, 1st terminal has 2 control flows,
2nd terminal has 2 control flows,
[GATE – Compiler Design] Page 36
3rd terminal has 2 control flows,
………..
9th terminal has 2 control flows,
and 10th terminal has 2 control flows.
Using multiplication law of counting, we get
= 2*2*2*2*2……10 times = 2^10 = 1024 number of control flow paths for 10 if terminals.
1024 is correct answer.
2. A variable x is said to be live at a statement Si in a program if the following three
conditions hold simultaneously:
1. There exists a statement Sj that uses x
2. There is a path from Si to Sj in the flow
graph corresponding to the program
3. The path has no intervening assignment to x
including at Si and Sj
The variables which are live both at the statement in basic block 2 and at the statement
in basic block 3 of the above control flow graph are
A p, s, u
B r, s, u
C r, u
D q, v
GATE-CS-2015 (Set 1)
Answer: (C)
[GATE – Compiler Design] Page 37
Explanation: Live variable analysis is useful in compilers to find variables in each program
that may be needed in future.
As per the definition given in question, a variable is live if it holds a value that may be
needed in the future. In other words, it is used in future before any new assignment.
3. Consider the intermediate code given below:
1. i = 1
2. j = 1
3. t1 = 5 * i
4. t2 = t1 + j
5. t3 = 4 * t2
6. t4 = t3
7. a[t4] = –1
8. j = j + 1
9. if j <= 5 goto(3)
10. i = i + 1
11. if i < 5 goto(2)
The number of nodes and edges in the control-flow-graph constructed for the above
code, respectively, are
A 5 and 7
B 6 and 7
C 5 and 5
D 7 and 8
GATE-CS-2015 (Set 2)
Answer: (B)
Explanation: Below is control flow graph of above code.
[GATE – Compiler Design] Page 38
4. For a C program accessing X[i][j][k], the following intermediate code is generated by
a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8
bits.
t0 = i ∗ 1024
t1 = j ∗ 32
t2 = k ∗ 4
t3 = t1 + t0
t4 = t3 + t2
t5 = X[t4]
Which one of the following statements about the source code for the C program is
CORRECT?
(A) X is declared as “int X[32][32][8]”.
(B) X is declared as “int X[4][1024][32]”.
[GATE – Compiler Design] Page 39
(C) X is declared as “char X[4][32][8]”.
(D) X is declared as “char X[32][16][2]”.
GATE CS 2014
Answer: (A)
Explanation: The final expression can be simplified in form ofi, j and k by following the
intermediate code steps in reverse order
t5 = X[t4]
= X[t3 + t2]
= X[t1 + t0 + t2]
= X[i*1024 + j*32 + k*4]
= X + i*1024 + j*32 + k*4
Since k is multiplied by 4, the array must be an int array.
We are left with 2 choices (A and B) among the 4 given choices.
X[i][j][k]’th element in one dimensional array is equivalent to
X[i*M*L + j*L + k]’th element in one dimensional array
(Note that multi-dimensional arrays are stored in row major order in C).
So we get following equations
j*L*4 = j*32, we get L = 8 (4 is the sizeof(int))
i*1024 = i*M*L*4, we get M = 1024/32 = 32
Therefore option A is the only correct option as M and L are 32 and 8 respectively
only in option A.
5. Consider the basic block given below.
a = b + c
c = a + d
d = b + c
e = d - b
a = e + b
[GATE – Compiler Design] Page 40
The minimum number of nodes and edges present in the DAG representation of the
above basic block respectively are
(A) 6 and 6
(B) 8 and 10
(C) 9 and 12
(D) 4 and 4
GATE – CS – 2014 (Set 3)
Answer: (A)
Explanation: Simplifying the given equations :
d = b + c (given) e = d – b (given)
=> d = b + c and e = c
e = d – b (given) a = e + b (given)
=> a = d
Thus, the given DAG has 6 nodes and 6 edges.
6. One of the purposes of using intermediate code in compilers is to
[GATE – Compiler Design] Page 41
A make parsing and semantic analysis simpler.
B improve error recovery and error reporting.
C increase the chances of reusing the machine-independent code optimizer in other
compilers.
D improve the register allocation.
GATE-CS-2014-(Set-3)
Answer: (C)
Explanation: After semantic Analysis, the code is converted into intermediate code which is
language independent, the advantage of converting into intermediate code is to improve the
performance of code generation and to increase the chances of reusing the machine-
independent code optimizer in other compilers.
7. Which one of the following is FALSE?
A A basic block is a sequence of instructions where control enters the sequence at
the beginning and exits at the end.
B Available expression analysis can be used for common subexpression
elimination.
C Live variable analysis can be used for dead code elimination.
D x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination.
GATE-CS-2014-(Set-1)
Explanation: (A) A basic block is a sequence of instructions where control enters the
sequence at the beginning and exits at the end is TRUE.
(B) Available expression analysis can be used for common subexpression elimination is
TRUE. Available expressions is an analysis algorithm that determines for each point in the
program the set of expressions that need not be recomputed. Available expression analysis is
used to do global common subexpression elimination (CSE). If an expression is available at a
point, there is no need to re-evaluate it.
(C)Live variable analysis can be used for dead code elimination is TRUE.
(D) x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination is FALSE.
Common subexpression elimination (CSE) refers to compiler optimization replaces identical
expressions (i.e., they all evaluate to the same value) with a single variable holding the
computed value when it is worthwhile to do so.
[GATE – Compiler Design] Page 42
Below is an example
In the following code:
a = b * c + g;
d = b * c * e;
it may be worth transforming the code to:
tmp = b * c;
a = tmp + g;
d = tmp * e;
8. Some code optimizations are carried out on the intermediate code because
(A) they enhance the portability of the compiler to other target processors
(B) program analysis is more accurate on intermediate code than on machine code
(C) the information from dataflow analysis cannot otherwise be used for optimization
(D) the information from the front end cannot otherwise be used for optimization
Answer: (A)
Explanation: Option (B) is also true. But the main purpose of doing some code-optimization
on intermediate code generation is to enhance the portability of the compiler to target
processors. So Option A) is more suitable here.
Intermediate code is machine/architecture independent code. So a compiler can optimize it
without worrying about the architecture on which the code is going to execute (it may be the
same or the other ). So that kind of compiler can be used by multiple different architectures.
In contrast to that, suppose code optimization is done on target code, which is
machine/architecture dependent, then the compiler has be specific about the optimizations on
that kind of code. In this case the compiler can’t be used by multiple different architectures,
because the target code produced on different architectures would be different. Hence
portability reduces here.
9. In a simplified computer the instructions are:
[GATE – Compiler Design] Page 43
The computer has only two registers, and OP is either ADD or SUB. Consider the
following basic block:
Assume that all operands are initially in memory. The final value of the computation
should be in memory. What is the minimum number of MOV instructions in the code
generated for this basic block?
(A) 2
(B) 3
(C) 5
(D) 6
Answer: (B)
Explanation:
For Instructions of t2 and t3
1. MOV c, t2
2. OP d, t2(OP=ADD)
3. OP e, t2(OP=SUB)
[GATE – Compiler Design] Page 44
For Instructions of t1 and t4
4. MOV a, t1
5. OP b, t1(OP=ADD)
6. OP t1, t2(OP=SUB)
7. MOV t2, a(AS END Value has To be in the MEMORY)
10. Which one of the following is FALSE?
(A) A basic block is a sequence of instructions where control enters the sequence at the
beginning and exits at the end.
(B) Available expression analysis can be used for common subexpression elimination.
(C) Live variable analysis can be used for dead code elimination.
(D) x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination.
Answer: (D)
Explanation: (A) A basic block is a sequence of instructions where control enters the
sequence at the beginning and exits at the end is TRUE.
(B) Available expression analysis can be used for common subexpression elimination is
TRUE. Available expressions is an analysis algorithm that determines for each point in the
program the set of expressions that need not be recomputed. Available expression analysis is
used to do global common subexpression elimination (CSE). If an expression is available at a
point, there is no need to re-evaluate it.
(C)Live variable analysis can be used for dead code elimination is TRUE.
(D) x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination is FALSE.
Common subexpression elimination (CSE) refers to compiler optimization replaces identical
expressions (i.e., they all evaluate to the same value) with a single variable holding the
computed value when it is worthwhile to do so.
Below is an example
[GATE – Compiler Design] Page 45
In the following code:
a = b * c + g;
d = b * c * e;
it may be worth transforming the code to:
tmp = b * c;
a = tmp + g;
d = tmp * e;
11. Consider the following C code segment.
for (i = 0, i<n; i++)
{
for (j=0; j<n; j++)
{
if (i%2)
{
x += (4*j + 5*i);
y += (7 + 4*j);
}
}
}
Which one of the following is false?
(A) The code contains loop invariant computation
(B) There is scope of common sub-expression elimination in this code
(C) There is scope of strength reduction in this code
(D) There is scope of dead code elimination in this code
Answer: (D)
Explanation: Question asks about false statement
[GATE – Compiler Design] Page 46
4*j is common subexpression elimination so B is true.
5*i can be moved out of inner loop so can be i%2.
Means, A is true as we have loop invariant computation.
Next, 4*j as well as 5*i can be replaced with a = - 4;
before j loop then a = a + 4; where 4*j is computed,
likewise for 5*i. C is true as there is scope of strength
reduction.
By choice elimination, we have D.
Step 6 should have been enough, if the question hadn’t asked for final value in memory and
rather be in register. The final step require another MOV, thus a total of 3.

Contenu connexe

Tendances (20)

C notes
C notesC notes
C notes
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Notes of c programming 1st unit BCA I SEM
Notes of c programming  1st unit BCA I SEMNotes of c programming  1st unit BCA I SEM
Notes of c programming 1st unit BCA I SEM
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
C_Programming_Notes_ICE
C_Programming_Notes_ICEC_Programming_Notes_ICE
C_Programming_Notes_ICE
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c language
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Handout#08
Handout#08Handout#08
Handout#08
 
Assignment4
Assignment4Assignment4
Assignment4
 
C Language
C LanguageC Language
C Language
 
Handout#12
Handout#12Handout#12
Handout#12
 
C languaGE UNIT-1
C languaGE UNIT-1C languaGE UNIT-1
C languaGE UNIT-1
 
Handout#03
Handout#03Handout#03
Handout#03
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
Tokens_C
Tokens_CTokens_C
Tokens_C
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?Declare Your Language: What is a Compiler?
Declare Your Language: What is a Compiler?
 

Similaire à Compiler gate question key

Chapter One
Chapter OneChapter One
Chapter Onebolovv
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introductionRana Ehtisham Ul Haq
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof Chethan Raj C
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler designDHARANI BABU
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfDrIsikoIsaac
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdfAkarTaher
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfDrIsikoIsaac
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Tirumala Rao
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdfvino108206
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptxwoldu2
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptxMugilvannan11
 
Fundamentals of programming and problem solving
Fundamentals of programming and problem solvingFundamentals of programming and problem solving
Fundamentals of programming and problem solvingJustine Dela Serna
 

Similaire à Compiler gate question key (20)

Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
Chapter One
Chapter OneChapter One
Chapter One
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Assignment1
Assignment1Assignment1
Assignment1
 
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
Prof. Chethan Raj C, BE, M.Tech (Ph.D) Dept. of CSE. System Software & Operat...
 
Principles of compiler design
Principles of compiler designPrinciples of compiler design
Principles of compiler design
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Chapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdfChapter1pdf__2021_11_23_10_53_20.pdf
Chapter1pdf__2021_11_23_10_53_20.pdf
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
CS8251_QB_answers.pdf
CS8251_QB_answers.pdfCS8251_QB_answers.pdf
CS8251_QB_answers.pdf
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
Fundamentals of programming and problem solving
Fundamentals of programming and problem solvingFundamentals of programming and problem solving
Fundamentals of programming and problem solving
 
Unit1 cd
Unit1 cdUnit1 cd
Unit1 cd
 
Ss ui lecture 1
Ss ui lecture 1Ss ui lecture 1
Ss ui lecture 1
 

Plus de ArthyR3

Unit IV Knowledge and Hybrid Recommendation System.pdf
Unit IV Knowledge and Hybrid Recommendation System.pdfUnit IV Knowledge and Hybrid Recommendation System.pdf
Unit IV Knowledge and Hybrid Recommendation System.pdfArthyR3
 
VIT336 – Recommender System - Unit 3.pdf
VIT336 – Recommender System - Unit 3.pdfVIT336 – Recommender System - Unit 3.pdf
VIT336 – Recommender System - Unit 3.pdfArthyR3
 
OOPs - JAVA Quick Reference.pdf
OOPs - JAVA Quick Reference.pdfOOPs - JAVA Quick Reference.pdf
OOPs - JAVA Quick Reference.pdfArthyR3
 
NodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdfArthyR3
 
MongoDB.pdf
MongoDB.pdfMongoDB.pdf
MongoDB.pdfArthyR3
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdfArthyR3
 
ANGULARJS.pdf
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdfArthyR3
 
JQUERY.pdf
JQUERY.pdfJQUERY.pdf
JQUERY.pdfArthyR3
 
Qb it1301
Qb   it1301Qb   it1301
Qb it1301ArthyR3
 
CNS - Unit v
CNS - Unit vCNS - Unit v
CNS - Unit vArthyR3
 
Cs8792 cns - unit v
Cs8792   cns - unit vCs8792   cns - unit v
Cs8792 cns - unit vArthyR3
 
Cs8792 cns - unit iv
Cs8792   cns - unit ivCs8792   cns - unit iv
Cs8792 cns - unit ivArthyR3
 
Cs8792 cns - unit iv
Cs8792   cns - unit ivCs8792   cns - unit iv
Cs8792 cns - unit ivArthyR3
 
Cs8792 cns - unit i
Cs8792   cns - unit iCs8792   cns - unit i
Cs8792 cns - unit iArthyR3
 
Java quick reference
Java quick referenceJava quick reference
Java quick referenceArthyR3
 
Cs8792 cns - Public key cryptosystem (Unit III)
Cs8792   cns - Public key cryptosystem (Unit III)Cs8792   cns - Public key cryptosystem (Unit III)
Cs8792 cns - Public key cryptosystem (Unit III)ArthyR3
 
Cryptography Workbook
Cryptography WorkbookCryptography Workbook
Cryptography WorkbookArthyR3
 
Cs6701 cryptography and network security
Cs6701 cryptography and network securityCs6701 cryptography and network security
Cs6701 cryptography and network securityArthyR3
 
Cyber forensics question bank
Cyber forensics   question bankCyber forensics   question bank
Cyber forensics question bankArthyR3
 

Plus de ArthyR3 (20)

Unit IV Knowledge and Hybrid Recommendation System.pdf
Unit IV Knowledge and Hybrid Recommendation System.pdfUnit IV Knowledge and Hybrid Recommendation System.pdf
Unit IV Knowledge and Hybrid Recommendation System.pdf
 
VIT336 – Recommender System - Unit 3.pdf
VIT336 – Recommender System - Unit 3.pdfVIT336 – Recommender System - Unit 3.pdf
VIT336 – Recommender System - Unit 3.pdf
 
OOPs - JAVA Quick Reference.pdf
OOPs - JAVA Quick Reference.pdfOOPs - JAVA Quick Reference.pdf
OOPs - JAVA Quick Reference.pdf
 
NodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdf
 
MongoDB.pdf
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
 
REACTJS.pdf
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
 
ANGULARJS.pdf
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdf
 
JQUERY.pdf
JQUERY.pdfJQUERY.pdf
JQUERY.pdf
 
Qb it1301
Qb   it1301Qb   it1301
Qb it1301
 
CNS - Unit v
CNS - Unit vCNS - Unit v
CNS - Unit v
 
Cs8792 cns - unit v
Cs8792   cns - unit vCs8792   cns - unit v
Cs8792 cns - unit v
 
Cs8792 cns - unit iv
Cs8792   cns - unit ivCs8792   cns - unit iv
Cs8792 cns - unit iv
 
Cs8792 cns - unit iv
Cs8792   cns - unit ivCs8792   cns - unit iv
Cs8792 cns - unit iv
 
Cs8792 cns - unit i
Cs8792   cns - unit iCs8792   cns - unit i
Cs8792 cns - unit i
 
Java quick reference
Java quick referenceJava quick reference
Java quick reference
 
Cs8792 cns - Public key cryptosystem (Unit III)
Cs8792   cns - Public key cryptosystem (Unit III)Cs8792   cns - Public key cryptosystem (Unit III)
Cs8792 cns - Public key cryptosystem (Unit III)
 
Cryptography Workbook
Cryptography WorkbookCryptography Workbook
Cryptography Workbook
 
Cns
CnsCns
Cns
 
Cs6701 cryptography and network security
Cs6701 cryptography and network securityCs6701 cryptography and network security
Cs6701 cryptography and network security
 
Cyber forensics question bank
Cyber forensics   question bankCyber forensics   question bank
Cyber forensics question bank
 

Dernier

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Dernier (20)

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Compiler gate question key

  • 1. DEPARTMENT OF INFORMATION TECHNOLOGY GATE QUESTIONS COMPILER DESIGN III IT 2018 – 2019 EVEN Question Paper Years 2014 to 2017 and Few question from 1998 to 2013 Prepared by R. ARTHY, AP/IT
  • 2. GATE SYLLABUS CS Computer Science and Information Technology Section1: Engineering Mathematics Discrete Mathematics: Propositional and first order logic. Sets, relations, functions, partial orders and lattices. Groups. Graphs: connectivity, matching, coloring. Combinatorics: counting, recurrence relations, generating functions. Linear Algebra: Matrices, determinants, system of linear equations, eigenvalues and eigenvectors, LU decomposition. Calculus: Limits, continuity and differentiability. Maxima and minima. Mean value theorem. Integration. Probability: Random variables. Uniform, normal, exponential, poisson and binomial distributions. Mean, median, mode and standard deviation. Conditional probability and Bayes theorem. Computer Science and Information Technology Section 2: Digital Logic Boolean algebra. Combinational and sequential circuits. Minimization. Number representations and computer arithmetic (fixed and floating point). Section 3: Computer Organization and Architecture Machine instructions and addressing modes. ALU, data‐path and control unit. Instruction pipelining. Memory hierarchy: cache, main memory and secondary storage; I/O interface (interrupt and DMA mode). Section 4: Programming and Data Structures Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary search trees, binary heaps, graphs. Section 5: Algorithms Searching, sorting, hashing. Asymptotic worst case time and space complexity. Algorithm design techniques: greedy, dynamic programming and divide‐and‐conquer. Graph search, minimum spanning trees, shortest paths. Section 6: Theory of Computation Regular expressions and finite automata. Context-free grammars and push-down automata. Regular and contex-free languages, pumping lemma. Turing machines and undecidability. Section 7: Compiler Design Lexical analysis, parsing, syntax-directed translation. Runtime environments. Intermediate code generation. Section 8: Operating System Processes, threads, inter‐process communication, concurrency and synchronization. Deadlock. CPU scheduling. Memory management and virtual memory. File systems.
  • 3. CS6660 COMPILER DESIGN OBJECTIVES: The student should be made to: Learn the design principles of a Compiler. Learn the various parsing techniques and different levels of translation. Learn how to optimize and effectively generate machine codes. UNIT I INTRODUCTION TO COMPILERS Translators-Compilation and Interpretation-Language processors -The Phases of Compiler- Errors Encountered in Different Phases-The Grouping of Phases-Compiler Construction Tools - Programming Language basics. UNIT II LEXICAL ANALYSIS Need and Role of Lexical Analyzer-Lexical Errors-Expressing Tokens by Regular Expressions-Converting Regular Expression to DFA- Minimization of DFA-Language for Specifying Lexical Analyzers-LEX-Design of Lexical Analyzer for a sample Language. UNIT III SYNTAX ANALYSIS Need and Role of the Parser-Context Free Grammars -Top Down Parsing -General Strategies-Recursive Descent Parser Predictive Parser-LL(1) Parser-Shift Reduce Parser-LR Parser-LR (0)Item-Construction of SLR Parsing Table -Introduction to LALR Parser - Error Handling and Recovery in Syntax Analyzer-YACC-Design of a syntax Analyzer for a Sample Language . UNIT IV SYNTAX DIRECTED TRANSLATION & RUN TIME ENVIRONMENT Syntax directed Definitions-Construction of Syntax Tree-Bottom-up Evaluation of S- Attribute Definitions- Design of predictive translator - Type Systems-Specification of a simple type checker-Equivalence of Type Expressions-Type Conversions. RUN-TIME ENVIRONMENT: Source Language Issues-Storage Organization-Storage Allocation-Parameter Passing-Symbol Tables-Dynamic Storage Allocation-Storage Allocation in FORTAN. UNIT V CODE OPTIMIZATION AND CODE GENERATION Principal Sources of Optimization-DAG- Optimization of Basic Blocks-Global Data Flow Analysis-Efficient Data Flow Algorithms-Issues in Design of a Code Generator - A Simple Code Generator Algorithm. OUTCOMES: Students should be able to ...  Discuss about the compiler translator to process a programming language and phases of compilation  Demonstrate lexical analyzer to generate valid tokens  Demonstrate various parsers to syntactically check the source code using context free grammar  Construct a type checked syntax tree  Apply various code optimization techniques to generate optimized machine codes
  • 4. DEPARTMENT OF INFORMATION TECHNOLOGY GATE QUESTIONS CS6660 – COMPILER DESIGN INDEX Academic Year : 2018 – 2019 EVEN Question Paper Year : 2014 to 2017 and Few question from 1998 to 2013 Name of the Subject In – Charge : Mrs. R. Arthy, AP/IT S. No. Name of the Topic Number of Questions Page Number 1 Introduction to Compiler 11 1 2 Lexical Analyzer 13 6 3 Parsing 20 14 4 Syntax Directed Translation and Run Time Environemnt 6 31 5 Code generation 11 35 Faculty In-charge GATE Coordinator HoD/IT
  • 5.
  • 6. [GATE – Compiler Design] Page 1 DEPARTMENT OF INFORMATION TECHNOLOGY GATE QUESTIONS CS6660 – COMPILER DESIGN UNIT I – INTRODUCTION TO COMPILER 1. The process of assigning load addresses to the various parts of the program and adjusting the code and data in the program to reflect the assigned addresses is called (GATE CS 2001) a) Assembly b) Parsing c) Relocation d) Symbol resolution Answer: (c) Relocation is the process of replacing symbolic references or names of libraries with actual usable addresses in memory before running a program. It is typically done by the linker during compilation (at compile time), although it can be done at runtime by a relocating loader. Compilers or assemblers typically generate the executable with zero as the lower- most starting address. Before the execution of object code, these addresses should be adjusted so that they denote the correct runtime addresses. Relocation is typically done in two steps: 1. Each object code has various sections like code, data, .bss etc. To combine all the objects to a single executable, the linker merges all sections of similar type into a single section of that type. The linker then assigns runtime addresses to each section and each symbol. At this point, the code (functions) and data (global variables) will have unique runtime addresses. 2. Each section refers to one or more symbols which should be modified so that they point to the correct runtime addresses.
  • 7. [GATE – Compiler Design] Page 2 2. Consider a program P that consists of two source modules M1 and M2 contained in two different files. If M1 contains a reference to a function defined in M2 the reference will be resolved at (GATE CS 2004) a) Edit time b) Compile time c) Link time d) Load time Answer (c) Compiler transforms source code into the target language. The target language is generally in binary form known as object code. Typically, an object file can contain three kinds of symbols: * defined symbols, which allow it to be called by other modules, * undefined symbols, which call the other modules where these symbols are defined, and * local symbols, used internally within the object file to facilitate relocation. When a program comprises multiple object files, the linker combines these files into a unified executable program, resolving the symbols as it goes along. 3. A loader is a program that A) program that places programs into memory and prepares them for execution. B) program that automates the translation of assembly language into machine language. C) program that accepts a program written in a high level language and produces as object program D) None of these Answer: A 4. A system program that setup an executable program in main memory ready for execution is A) assembler B) linker C) loader D) load and go Answer: C
  • 8. [GATE – Compiler Design] Page 3 5. Uniform symbol table A) contains all constants in the program B) is a permanent table of decision rules in the form of patterns for matching with the uniform symbol table to discover syntactic structure. C) consists of full or partial list of the token's as they appear in the program created by Lexical analysis and used for syntax analysis and interpretation. D) a permanent table which lists all key words and special symbols of the language in symbolic form. Answer: C 6. Assembler is a program that A) places programs into memory and prepares them for execution B) automates the translation of assembly language into machine language C) accepts a program written in a high level language and produces an object program. D) None of these Answer: B 7. Compiler can diagnose A) grammatical errors only B) logical errors only C) grammatical as well as logical errors D) None of these Answer: A 8. A system program that set-up an executable program in main memory ready for execution is A) assembler B) linker C) loader D) text editor Answer: C
  • 9. [GATE – Compiler Design] Page 4 9. Type checking is normally done during (a) lexical analysis (b) syntax analysis (c) syntax directed translation (d) code optimization Answer: C 10. Match the following according to input(from the left column) to the compiler phase(in the right column) that process it: A P -> (ii), Q -> (iii), R -> (iv), S -> (i) B P -> (ii), Q -> (i), R -> (iii), S -> (iv) C P -> (iii), Q -> (iv), R -> (i), S -> (ii) D P -> (i), Q -> (iv), R -> (ii), S -> (iii) GATE-CS-2017 (Set 2) Answer: (C) Explanation: P – iii Syntax tree is input to semantic analyser Q – iv Character stream is input to lexical analyser. R – i Intermediate code is input to code generator S -ii Token stream is input to syntax analyser. 11. Which one of the following statements is FALSE? A Context-free grammar can be used to specify both lexical and syntax rules. B Type checking is done before parsing. C High-level language programs can be translated to different Intermediate Representations.
  • 10. [GATE – Compiler Design] Page 5 D Arguments to a function can be passed using the program stack. GATE CS 2018 Answer: (B) Explanation: Type checking is done at semantic analysis phase and parsing is done at syntax analysis phase. And we know Syntax analysis phase comes before semantic analysis. So Option (B) is False. All other options seems Correct.
  • 11. [GATE – Compiler Design] Page 6 UNIT II – LEXICAL ANALYSIS 1. In some programming languages, an identifier is permitted to be a letter following by any number of letters or digits. If L and D denote the sets of letters and digits respectively, which of the following expressions defines an identifier? GATE – CS - 1995 Answer : B 2. The minimum possible number of states of a deterministic finite automaton that accepts a regular language L = {w1aw2 | w1, w2 ∈{a,b}* , |w1| = 2, w2>=3} is_______ A 3 B 5 C 8 D 7 GATE-CS-2017 (Set 2) Answer: C 3. The length of the shortest string NOT in the language (over Σ = {a, b}) of the following regular expression is ______________. a*b*(ba)*a* A 2 B 3 C 4 D 5 GATE-CS-2014-(Set-3) Answer: (B)
  • 12. [GATE – Compiler Design] Page 7 Explanation: All the strings that can be generated till length-2 are definitely present in this language. Now, let’s look at the strings of length-3; {aaa, aab, aba, abb, baa, bab, bba, bbb} . String “bab” cannot be generated from given language. so, the string “bab” is the shortest string not acceptable by given regular expression. 4. of the following is CORRECT? A Only (I) B Only (II) C Both (I) and (II) D Neither (I) nor (II) GATE-CS-2014-(Set-2) Answer: (A) Explanation: L1.L2 is definitely regular, since regular languages are closed under concatenation. But L1.L2 = { an bn | n ≥ 0 } is not correct. Because both variable are independent of each other. L1.L2 = { an bm | n ≥ 0 , m ≥ 0 } is possible. Therefore, only statement (i) is TRUE. 5. Let L1 = {w ∈ {0,1}∗ | w has at least as many occurrences of (110)’s as (011)’s}. Let L2 = { ∈ {0,1}∗ | w has at least as many occurrences of (000)’s as (111)’s}. Which one of the following is TRUE? A L1 is regular but not L2 B L2 is regular but not L! C Both L2 and L1 are regular D Neither L1 nor L2 are regular
  • 13. [GATE – Compiler Design] Page 8 GATE-CS-2014-(Set-2) Answer: (A) Explanation: L1 is regular let us consider the string 011011011011 In this string, number of occurrences of 011 are 4 but when we see here 110 is also occurred and the number of occurrence of 110 is 3. Note that if i add a 0 at the last of string we can have same number of occurrences of 011 and 110 so this string is accepted. We can say if the string is ending with 011 so by appending a 0 we can make 110 also. Now string2: 110110110110 in this number of occurrences of 110 is 4 and 011 is 3 which already satisfy the condition So we can observe here that whenever 110 will be there string will be accepted So with this idea we can build an automata for this. Therefore, it is regular. 6. Which one of the following is TRUE? GATECS2014Q25 A A B B C C D D GATE-CS-2014-(Set-1) Answer: (C) Explanation: (A) L = {a n b n |n >= 0} is not regular because there does not exists a finite automaton that can derive this grammar. Intuitively, finite automaton has finite memory, hence it can’t track number of as. It is a standard CFL though. (B) L = {a n b n |n is prime} is again not regular because there is no way to remember/check if current n is prime or not. Hence, no finite automaton exists to derive this grammar, thus it
  • 14. [GATE – Compiler Design] Page 9 is not regular. (C) L = {w|w has 3k+1 bs} is a regular language because k is a fixed constant and we can easily emulate L as a ∗ ba ∗ …..ba ∗ such that there are exactly 3k + 1 bs and a ∗ s surrounding each b in the grammar. D) L = {ww| w ∈ Σ ∗ } is again not a regular grammar, infact it is not even a CFG. There is no way to remember and derive double word using finite automaton. Hence, correct answer would be (C). 7. The number of tokens in the following C statement is (GATE 2000) printf("i = %d, &i = %x", i, &i); A 3 B 26 C 10 D 21 Answer - C Tokens are: printf ( "i=%d, &i=%x" , i , & i ) ; 8. In a compiler, keywords of a language are recognized during A parsing of the program B the code generation C the lexical analysis of the program
  • 15. [GATE – Compiler Design] Page 10 D dataflow analysis GATE CS 2011 Answer - C 9. The lexical analysis for a modern computer language such as Java needs the power of which one of the following machine models in a necessary and sufficient sense? A Finite state automata B Deterministic pushdown automata C Non-Deterministic pushdown automata D Turing Machine GATE CS 2011 Answer – A 10. Consider the following statements: (I) The output of a lexical analyzer is groups of characters. (II) Total number of tokens in printf("i=%d, &i=%x", i, &i); are 11. (III) Symbol table can be implementation by using array and hash table but not tree. Which of the following statement(s) is/are correct? A Only (I) B Only (II) and (III) C All (I), (II), and (III) D None of these GATE CS Mock 2018 | Set 2 Answer: (D) Explanation: (I) The output of a lexical analyzer is tokens. (II) Total number of tokens in printf("i=%d, &i=%x", i, &i); are 10. (III) Symbol table can be implementation by using array, hash table, tree and linked lists. So, option (D) is correct. 11. Consider the DFAs M and N given above. The number of states in a minimal DFA that accepts the language L(M) ∩ L(N) is __________.
  • 16. [GATE – Compiler Design] Page 11 A 0 B 1 C 2 D 3 GATE-CS-2015 (Set 1) Answer: (B) Explanation: In DFA M: all strings must end with ‘a’. In DFA N: all strings must end with ‘b’. So the intersection is empty. For an empty language, only one state is required in DFA. The state is non-accepting and remains on itself for all characters of alphabet. 12. A lexical analyzer uses the following patterns to recognize three tokens T1, T2, and T3 over the alphabet {a,b,c}. T1: a?(b∣c)*a T2: b?(a∣c)*b T3: c?(b∣a)*c Note that ‘x?’ means 0 or 1 occurrence of the symbol x. Note also that the analyzer outputs the token that matches the longest possible prefix. If the string bbaacabc is processes by the analyzer, which one of the following is the sequence of tokens it outputs? A T1T2T3 B T1T1T3 C T2T1T3 D T3T3 GATE CS 2018 Answer: (D) Explanation: 0 or 1 occurrence of the symbol x. T1 : (b+c)* a + a(b+c)* a T2 : (a+c)* b + b(a+c)* b T3 : (b+a)* c + c(b+a)* c
  • 17. [GATE – Compiler Design] Page 12 Given String : bbaacabc Longest matching prefix is ” bbaac ” (Which can be generated by T3) The remaining part (after Prefix) “abc” (Can be generated by T3) So, the answer is T3T3 13. A {q0, q1, q2} B {q0, q1} C {q0, q1, q2, q3} D {q3} GATE-CS-2014-(Set-1) Answer: (A) Explanation:
  • 18. [GATE – Compiler Design] Page 13 So, q0, q1 and q2 are reachable states for the input string 0011, but q3 is not. So, option (A) is answer.
  • 19. [GATE – Compiler Design] Page 14 UNIT III – SYNTAX ANALYSIS 1. Consider the following languages. L1 = {ap | p is a prime number} L2 = {an bm c2m | n >= 0, m >= 0} L3 = {an bn c2n | n >= 0} L4 = {an bn | n >= 1} Which of the following are CORRECT ? I. L1 is context free but not regular. II. L2 is not context free. III. L3 is not context free but recursive IV. L4 is deterministic context free A I, II and IV only B II and III only C I and IV only D III and IV only GATE-CS-2017 (Set 2) Answer: (D) Explanation: L1 =a p | p is prime : is context sensitive not context free L2 is context free L3 is not context free because we are not sure when to pop b and push a, because it is comparison between three consecutive terminals Clearly L4 is deterministic context free because we are sure of pushing a into stack first and on seeing b we are sure of popping a.
  • 20. [GATE – Compiler Design] Page 15 2. Let L1 and L2 be any context-free language and R be any regular language. Then, which of the following is correct ? I. L1 ∪ L2 is context-free. II. L1' is context-free. III. L1-R is context-free. IV. L1 ∩ L2 A I, II and IV only B I and III only C II and IV only D I only GATE-CS-2017 (Set 2) Answer: (B) Explanation: Context free language are closed under union and difference with regular language. It is not closed under complementation and intersection . Complement of CFL is recursive language. 3. Identity the language generated by following grammar where S is the start variable. S --> XY X --> aX | a Y --> aYb | ∈ A {am bn | m>=n, n>0 } B {am bn | m>=n, n>=0 } C {am bn | m>n, n>=0 } D {am bn | m>n, n>0 } GATE-CS-2017 (Set 2) Answer: (C) Explanation: S --> XY X --> aX | a // This produces only "a"
  • 21. [GATE – Compiler Design] Page 16 Y --> aYb | ∈ // This produces and "a" for every "b" Option (A) and (B) are wrong because n can be zero also due to epsilon in Y Option (D) is wrong because Y–>aYb produces equal number of a’s and b’s. Since there is one variable X which produces at least one a. Therefore numbers of a’s are always greater than numbers of b’s. 4. Which of the following statements is false? (GATE CS 2001) a) An unambiguous grammar has same leftmost and rightmost derivation b) An LL(1) parser is a top-down parser c) LALR is more powerful than SLR d) An ambiguous grammar can never be LR(k) for any k Answer: (a) If a grammar has more than one leftmost (or rightmost) derivation for a single sentential form, the grammar is ambiguous. The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar 5. Which of the following derivations does a top-down parser use while parsing an input string? The input is assumed to be scanned in left to right order (GATE CS 2000). (a) Leftmost derivation (b) Leftmost derivation traced out in reverse (c) Rightmost derivation (d) Rightmost derivation traced out in reverse Answer (a) Top-down parsing (LL) In top down parsing, we just start with the start symbol and compare the right side of the different productions against the first piece of input to see which of the productions should be used. A top down parser is called LL parser because it parses the input from Left to right, and constructs a Leftmost derivation of the sentence. Algorithm (Top Down Parsing) a) In the current string, choose leftmost nonterminal. b) Choose a production for the chosen nonterminal.
  • 22. [GATE – Compiler Design] Page 17 c) In the string, replace the nonterminal by the right-hand-side of the rule. d) Repeat until no more nonterminals. LL grammars are often classified by numbers, such as LL(1), LL(0) and so on. The number in the parenthesis tells the maximum number of terminals we may have to look at at a time to choose the right production at any point in the grammar. The most common (and useful) kind of LL grammar is LL(1) where you can always choose the right production by looking at only the first terminal on the input at any given time. With LL(2) you have to look at two symbols, and so on. There exist grammars that are not LL(k) grammars for any fixed value of k at all, and they are sadly quite common. Let us see an example of top down parsing for following grammar. Let input string be ax. S -> Ax A -> a A -> b An LL(1) parser starts with S and asks “which production should I attempt?” Naturally, it predicts the only alternative of S. From there it tries to match A by calling method A (in a recursive-descent parser). Lookahead a predicts production A -> a The parser matches a, returns to S and matches x. Done. The derivation tree is: S / A x | a 6. Given the following expression grammar: E -> E * F | F+E | F F -> F-F | id which of the following is true? (GATE CS 2000) (a) * has higher precedence than + (b) – has higher precedence than * (c) + and — have same precedence (d) + has higher precedence than *
  • 23. [GATE – Compiler Design] Page 18 Answer(b) Precedence in a grammar is enforced by making sure that a production rule with higher precedence operator will never produce an expression with operator with lower precedence. In the given grammar ‘-’ has higher precedence than ‘*’ 7. Consider the grammar defined by the following production rules, with two operators ∗ and + S --> T * P T --> U | T * U P --> Q + P | Q Q --> Id U --> Id Which one of the following is TRUE? A + is left associative, while ∗ is right associative B + is right associative, while ∗ is left associative C Both + and ∗ are right associative D Both + and ∗ are left associative GATE-CS-2014-(Set-2) Answer: (B) Explanation: From the grammar we can find out associative by looking at grammar. Let us consider the 2nd production T -> T * U T is generating T*U recursively (left recursive) so * is left associative. Similarly P -> Q + P Right recursion so + is right associative. So option B is correct. NOTE: Above is the shortcut trick that can be observed after drawing few parse trees.
  • 24. [GATE – Compiler Design] Page 19 One can also find out correct answer by drawing the parse tree. 8. Consider the following grammar p --> xQRS Q --> yz|z R --> w|∈ S -> y Which is FOLLOW(Q)? (A) {R} (B) {w} (C) {w, y} (D) {w, ∉} Answer: C Follow(Q) = {First(R)} First(R) = {w, ∈} Follow(Q) = {w, First(S)} First(S) = {y} Therefore Follow(Q) = {w, y} 9. Which one of the following grammars is free from left recursion? A A B B C C
  • 25. [GATE – Compiler Design] Page 20 D D GATE-CS-2016 (Set 2) Answer: (B) Explanation: Grammar A has direct left recursion because of the production rule: A->Aa. Grammar C has indirect left recursion because of the production rules:S-> Aa and A->Sc Grammar D has indirect left recursion because of production rules : A-> Bd and B-> Ae Grammar B doesn’t have any left recursion (neither direct nor indirect). 10. If G is grammar with productions S → SaS | aSb | bSa | SS | ∈ where S is the start variable, then which one of the following is not generated by G? A abab B aaab C abbaa D babba GATE-CS-2017 (Set 1) Answer: D 11. Consider the grammar with non-terminals N = {S,C,S1 },terminals T={a,b,i,t,e}, with S as the start symbol, and the following set of rules: S --> iCtSS1|a S1 --> eS|ϵ C --> b The grammar is NOT LL(1) because: A it is left recursive B it is right recursive C it is ambiguous D It is not context-free. GATE-CS-2007 Answer: (C) Explanation:
  • 26. [GATE – Compiler Design] Page 21 A LL(1) grammar doesn’t give to multiple entries in a single cell of its parsing table. It has only single entry in a single cell, hence it should be unambiguous. Option A is wrong. Grammar is not left recursive. For a grammar to be left recursive a production should be of form A->Ab, where A is a single Non-Terminal and b is any string of grammar symbols. Option B is wrong. Because a right recursive grammar has nothing to do with LL(1). Option D is wrong. Because the given grammar is clearly a Context Free Grammar. A grammar is CFG if it has productions of the form A->(V∪ T)* , where A is a single non- terminal and V is a set of Non-terminals and T is a set of Terminals. Hence Option C should be the correct one. i.e. the grammar is ambiguous. But let’s see how the grammar is ambiguous. If the grammar is ambiguous then it should give multiple entry in a cell while making its parsing table. And Parse table is made with the aid of two functions : FIRST and FOLLOW. A parsing table of a grammar will not have multiple entries in a cell( i.e. will be a LL(1) grammar) if and only if the following conditions hold for each production of the form A->α|β 1) FIRST(α) ∩ FIRST(β) = Φ 2) if FIRST(α) contains ‘ ε ‘ then FIRST(α) ∩ FOLLOW (A) = Φ and vice-versa. Now, For the production , S->iCtSS1|a, rule 1 is satisfied, because FIRST(iCtSS1) ∩ FIRST(a) = {i} ∩ {a} = Φ For the production, S1->eS|ε, rule 1 is satisfied, as FIRST(eS) ∩ FIRST(ε) = {e} ∩ {ε} = Φ . But here due to ‘ε’ in FIRST, we have to check for rule 2. FIRST(eS) ∩ FOLLOW(S1) =
  • 27. [GATE – Compiler Design] Page 22 {e} ∩ {e, $} ≠ Φ . Hence rule 2 fails in this production rule. Therefore there will be multiple entries in the parsing table, hence the grammar is ambiguous and not LL(1). 12. Consider the following expression grammar G. E -> E - T | T T -> T + F | F F -> (E) | id Which of the following grammars are not left recursive, but equivalent to G. A) E -> E - T | T T -> T + F | F F -> (E) | id B) E -> TE' E' -> -TE' | ε T -> T + F | F F -> (E) | id C) E -> TX X -> -TX | ε T -> FY Y -> +FY | ε F -> (E) | id D) E -> TX | (TX) X -> -TX | +TX | ε T -> id A A
  • 28. [GATE – Compiler Design] Page 23 B B C C D D GATE-CS-2017 (Set 2) Answer: (C) Explanation: We know for left recursion : A -> Aα/β After removing left recursion it can be written as A->βA’ A’->αA’/ε Thus for : E->E- T/T α= -T , β= T . thus new production after removing left recursion is E->TE’ and E’->- TE’/ ε T->FT’ and T’->+FT’/ ε F->(E)/id . 13. For the grammar below, a partial LL(1) parsing table is also presented along with the grammar. Entries that need to be filled are indicated as E1, E2, and E3. epsilon is the empty string, $ indicates end of input, and, | separates alternate right hand sides of productions. A A
  • 29. [GATE – Compiler Design] Page 24 B B C C D D GATE CS 2012 Answer: (A) Explanation: First(X) - It is the set of terminals that begin the strings derivable from X. Follow(X) - It is the set of terminals that can appear immediately to the right of X in some sentential form. Now in the above question, FIRST(S) = { a, b, epsilon} FIRST(A) = FIRST(S) = { a, b, epsilon} FIRST(B) = FIRST(S) = { a, b, epsilon} FOLLOW (A) = { b , a } FOLLOW (S) = { $ } U FOLLOW (A) = { b , a , $ } FOLLOW (B) = FOLLOW (S) = { b ,a , $ } epsilon corresponds to empty string. 14. Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar? (GATE CS 2003) (a) Removing left recursion alone (b) Factoring the grammar alone (c) Removing left recursion and factoring the grammar
  • 30. [GATE – Compiler Design] Page 25 (d) None of the above Answer: (D) Explanation: Removing left recursion and factoring the grammar do not suffice to convert an arbitrary CFG to LL(1) grammar. 15. What is the maximum number of reduce moves that can be taken by a bottom-up parser for a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a) to parse a string with n tokens? A n/2 B n-1 C 2n-1 D 2n GATE CS 2013 Answer: (B) Explanation: Given in the question, a grammar with no epsilon- and unit-production (i.e., of type A -> є and A -> a). To get maximum number of Reduce moves, we should make sure than in each sentential form only one terminal is reduced. Since there is no unit production, so last 2 tokens will take only 1 move. So To Reduce input string of n tokens, first Reduce n-2 tokens using n-2 reduce moves and then Reduce last 2 tokens using production which has . So total of n-2+1 = n-1 Reduce moves. Suppose the string is abcd. ( n = 4 ). We can write the grammar which accepts this string as follows: S->aB B->bC C->cd
  • 31. [GATE – Compiler Design] Page 26 The Right Most Derivation for the above is: S -> aB ( Reduction 3 ) -> abC ( Reduction 2 ) -> abcd ( Reduction 1 ) We can see here that no production is for unit or epsilon. Hence 3 reductions here. We can get less number of reductions with some other grammar which also does’t produce unit or epsilon productions, S->abA A-> cd The Right Most Derivation for the above as: S -> abA ( Reduction 2 ) -> abcd ( Reduction 1 ) Hence 2 reductions. But we are interested in knowing the maximum number of reductions which comes from the 1st grammar. Hence total 3 reductions as maximum, which is ( n – 1) as n = 4 here. Thus, Option B. 16. Consider the following grammar. S -> S * E S -> E E -> F + E E -> F F -> id Consider the following LR(0) items corresponding to the grammar above. (i) S -> S * .E (ii) E -> F. + E (iii) E -> F + .E
  • 32. [GATE – Compiler Design] Page 27 Given the items above, which two of them will appear in the same set in the canonical sets-of-items for the grammar? A (i) and (ii) B (ii) and (iii) C (i) and (iii) D None of the above GATE-CS-2006 Answer: (D) Explanation: Let’s make the LR(0) set of items. First we need to augment the grammar with the production rule S’ -> .S , then we need to find closure of items in a set to complete a set. Below are the LR(0) sets of items.
  • 33. [GATE – Compiler Design] Page 28 17. Assume that the SLR parser for a grammar G has n1 states and the LALR parser for G has n2 states. The relationship between nl and n2 is (GATE CS 2003) (a) n1 is necessarily less than n2 (b) n1 is necessarily equal to n2 (c) n1 is necessarily greater than n2 (d) none of the above GATE-CS-2017 Answer: (B) 18. Which of the following statements about the parser is/are correct? I. Canonical LR is more powerful than SLR. II. SLR is more powerful than LALR. III. SLR is more powerful than canonical LR. A I only B II only C III only D II and III only GATE-CS-2017 (Set 2) Answer: A 19. A canonical set of items is given below S --> L. > R Q --> R. On input symbol < the set has (A) a shift-reduce conflict and a reduce-reduce conflict. (B) a shift-reduce conflict but not a reduce-reduce conflict. (C) a reduce-reduce conflict but not a shift-reduce conflict. (D) neither a shift-reduce nor a reduce-reduce conflict. GATE CS 2014 Answer: (D)
  • 34. [GATE – Compiler Design] Page 29 Explanation: The question is asked with respect to the symbol ‘ < ‘ which is not present in the given canonical set of items. Hence it is neither a shift-reduce conflict nor a reduce- reduce conflict on symbol ‘<‘. Hence D is the correct option. But if the question would have asked with respect to the symbol ‘ > ‘ then it would have been a shift-reduce conflict. 20. Which of the following statements related to merging of the two sets in the corresponding LALR parser is/are FALSE? 1. Cannot be merged since look aheads are different. 2. Can be merged but will result in S-R conflict. 3. Can be merged but will result in R-R conflict. 4. Cannot be merged since goto on c will lead to two different sets. A 1 only B 2 only C 1 and 4 only D 1, 2, 3, and 4 GATE CS 2013 Answer: (D) Explanation: The given two LR(1) set of items are : X -> c.X, c/d X -> .cX, c/d X -> .d, c/d and X -> c.X, $ X -> .cX, $ X -> .d, $ The symbols/terminals after the comma are Look-Ahead symbols.
  • 35. [GATE – Compiler Design] Page 30 These are the sets of LR(1) ( LR(1) is also called CLR(1) ) items. The LALR(1) parser combines those set of LR(1) items which are identical with respect to their 1st component but different with respect to their 2nd component. In a production rule of a LR(1) set of items, ( A -> B , c ) , A->B is the 1st component , and the Look-Ahead set of symbols, which is c here, is the second component. Now we can see that in the sets given, 1st component of the corresponding production rule is identical in both sets, and they only differ in 2nd component ( i.e. their look-ahead symbols) hence we can combine these sets to make a a single set which would be : X -> c.X, c/d/$ X -> .cX, c/d/$ X -> .d, c/d/$ This is done to reduce the total number of parser states. Now we can check the statements given. Statement 1 : The statement is false, as merging has been done because 2nd components i.e. look-ahead were different. Statement 2 : In the merged set, we can’t see any Shift-Reduce conflict ( because no reduction even possible, reduction would be possible when a production of form P -> q. is present) Statement 3 : In the merged set, we can’t see any Reduce-Reduce conflict ( same reason as above, no reduction even possible, so no chances of R-R conflict ) Statement 4: This statement is also wrong, because goto is carried on Non-Terminals symbols, not on terminal symbols, and c is a terminal symbol. Thus, all statements are wrong, hence option D.
  • 36. [GATE – Compiler Design] Page 31 UNIT IV – SYNTAX DIRECTED TRANSLATION AND RUN TIME ENVIRONMENT 1. Consider the grammar with the following translation rules and E as the start symbol. E -> E1 #T {E.value = E1.value * T.value} | T {E.value = T.value} T -> T1 & F {T.value = T1.value + F.value} |F {T.value= F.value} F -> num {F.value = num.value} Compute E.value for the root of the parse tree for the expression:2 # 3 & 5 # 6 &4. (GATE CS 2004) a) 200 b) 180 c) 160 d) 40 Answer: (c) Explanation: We can calculate the value by constructing the parse tree for the expression 2 # 3 & 5 # 6 &4. Alternatively, we can calculate by considering following precedence and associativity rules. Precedence in a grammar is enforced by making sure that a production rule with higher precedence operator will never produce an expression with operator with lower precedence. In the given grammar ‘&’ has higher precedence than ‘#’. Left associativity for operator * in a grammar is enforced by making sure that for a production rule like S -> S1 * S2 in grammar, S2 should never produce an expression with *. On the other hand, to ensure right associativity, S1 should never produce an expression with *. In the given grammar, both ‘#’ and & are left-associative. So expression 2 # 3 & 5 # 6 &4 will become ((2 # (3 & 5)) # (6 & 4)) Let us apply translation rules, we get ((2 * (3 + 5)) * (6 + 4)) = 160.
  • 37. [GATE – Compiler Design] Page 32 2. Consider the following Syntax Directed Translation Scheme (SDTS), with non- terminals {S, A} and terminals {a, b}}. Using the above SDTS, the output printed by a bottom-up parser, for the input aab is A 1 3 2 B 2 2 3 C 2 3 1 D Syntax Error GATE-CS-2016 (Set 1) Answer: (C) Explanation: Bottom up parser builds the parse tree from bottom to up, i.e from the given string to the starting symbol. The given string is aab and starting symbol is S. so the process is to start from aab and reach S. =>aab ( given string) =>aSb (after reduction by S->a, and hence print 2) =>aA (after reduction by A->Sb, and hence print 3) =>S (after reduction by S->aA, and hence print 1) As we reach the starting symbol from the string, the string belongs to the language of the grammar. Another way to do the same thing is :- bottom up parser does the parsing by RMD in reverse. RMD is as follows: =>S => aA (hence, print 1) => aSb (hence, print 3) => aab (hence, print 2) If we take in Reverse it will print : 231
  • 38. [GATE – Compiler Design] Page 33 3. Which one of the following is NOT performed during compilation? A Dynamic memory allocation B Type checking C Symbol table management D Inline expansion GATE-CS-2014-(Set-2) 4. In a bottom-up evaluation of a syntax directed definition, inherited attributes can (A) always be evaluated (B) be evaluated only if the definition is L--attributed (C) be evaluated only if the definition has synthesized attributes (D) never be evaluated GATE CS 2003 Answer: (B) Explanation: A Syntax Directed Definition (SDD) is called S Attributed if it has only synthesized attributes. L-Attributed Definitions contain both synthesized and inherited attributes but do not need to build a dependency graph to evaluate them. 5, Consider the translation scheme shown below S → T R R → + T {print ('+');} R | ε T → num {print (num.val);} Here num is a token that represents an integer and num.val represents the corresponding integer value. For an input string ‘9 + 5 + 2’, this translation scheme will print (A) 9 + 5 + 2 (B) 9 5 + 2 + (C) 9 5 2 + + (D) + + 9 5 2 GATE CS 2003
  • 39. [GATE – Compiler Design] Page 34 Answer: (B) Explanation: Let us make the parse tree for 9+5+2 in top down manner, left first derivation. Steps: 1) Exapnd S->TR 2) apply T->Num... 3) apply R -> +T... 4) appy T->Num... 5) apply R-> +T.. 6) apply T-> Num.. 7) apply R-> epsilon After printing through the print statement in the parse tree formed you will get the answer as 95+2+ 6. Consider the syntax directed definition shown below. S → id : = E {gen (id.place = E.place;);} E → E1 + E2 {t = newtemp ( ); gen (t = El.place + E2.place;); E.place = t} E → id {E.place = id.place;} Here, gen is a function that generates the output code, and newtemp is a function that returns the name of a new temporary variable on every call. Assume that ti’s are the temporary variable names generated by newtemp. For the statement ‘X: = Y + Z’, the 3-address code sequence generated by this definition is (A) X = Y + Z (B) t1 = Y + Z; X = t1 (C) t1 =Y; t2 = t1 + Z; X = t2 (D) t1 = Y; t2 = Z; t3 = t1 + t2; X = t3 GATE CS 2003 Answer: (B) Explanation: It must be B. The production E –> E + E is used only one time and hence only one temporary variable is generated.
  • 40. [GATE – Compiler Design] Page 35 UNIT V – CODE OPTIMIZATION AND CODE GENERATION 1. Consider the following grammar: stmt -> if expr then else expr; stmt | ε expr -> term relop term | term term -> id | number id -> a | b | c number -> [0-9] where relop is a relational operate (e.g < >, ….), ε refers to the empty statement, and if ,then, else are terminals. Consider a program P following the above grammar containing ten if terminals. The number of control flows paths in P is ____________. For example, the program if e1 then e2 else e3 has 2 control flow paths, e1 -> e2 and e1 -> e3 A 20 B 1024 C 2048 D 10 GATE-CS-2017 (Set 1) Answer: (B) Explanation: Number of control flow paths for 10 if terminals if then else ; stmt if then else ; if then else ; stmt ………….. 10 times. Observe that there is a semi-colon after every if structure. Since, every if structure has 2 control flows. Therefore, 1st terminal has 2 control flows, 2nd terminal has 2 control flows,
  • 41. [GATE – Compiler Design] Page 36 3rd terminal has 2 control flows, ……….. 9th terminal has 2 control flows, and 10th terminal has 2 control flows. Using multiplication law of counting, we get = 2*2*2*2*2……10 times = 2^10 = 1024 number of control flow paths for 10 if terminals. 1024 is correct answer. 2. A variable x is said to be live at a statement Si in a program if the following three conditions hold simultaneously: 1. There exists a statement Sj that uses x 2. There is a path from Si to Sj in the flow graph corresponding to the program 3. The path has no intervening assignment to x including at Si and Sj The variables which are live both at the statement in basic block 2 and at the statement in basic block 3 of the above control flow graph are A p, s, u B r, s, u C r, u D q, v GATE-CS-2015 (Set 1) Answer: (C)
  • 42. [GATE – Compiler Design] Page 37 Explanation: Live variable analysis is useful in compilers to find variables in each program that may be needed in future. As per the definition given in question, a variable is live if it holds a value that may be needed in the future. In other words, it is used in future before any new assignment. 3. Consider the intermediate code given below: 1. i = 1 2. j = 1 3. t1 = 5 * i 4. t2 = t1 + j 5. t3 = 4 * t2 6. t4 = t3 7. a[t4] = –1 8. j = j + 1 9. if j <= 5 goto(3) 10. i = i + 1 11. if i < 5 goto(2) The number of nodes and edges in the control-flow-graph constructed for the above code, respectively, are A 5 and 7 B 6 and 7 C 5 and 5 D 7 and 8 GATE-CS-2015 (Set 2) Answer: (B) Explanation: Below is control flow graph of above code.
  • 43. [GATE – Compiler Design] Page 38 4. For a C program accessing X[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits. t0 = i ∗ 1024 t1 = j ∗ 32 t2 = k ∗ 4 t3 = t1 + t0 t4 = t3 + t2 t5 = X[t4] Which one of the following statements about the source code for the C program is CORRECT? (A) X is declared as “int X[32][32][8]”. (B) X is declared as “int X[4][1024][32]”.
  • 44. [GATE – Compiler Design] Page 39 (C) X is declared as “char X[4][32][8]”. (D) X is declared as “char X[32][16][2]”. GATE CS 2014 Answer: (A) Explanation: The final expression can be simplified in form ofi, j and k by following the intermediate code steps in reverse order t5 = X[t4] = X[t3 + t2] = X[t1 + t0 + t2] = X[i*1024 + j*32 + k*4] = X + i*1024 + j*32 + k*4 Since k is multiplied by 4, the array must be an int array. We are left with 2 choices (A and B) among the 4 given choices. X[i][j][k]’th element in one dimensional array is equivalent to X[i*M*L + j*L + k]’th element in one dimensional array (Note that multi-dimensional arrays are stored in row major order in C). So we get following equations j*L*4 = j*32, we get L = 8 (4 is the sizeof(int)) i*1024 = i*M*L*4, we get M = 1024/32 = 32 Therefore option A is the only correct option as M and L are 32 and 8 respectively only in option A. 5. Consider the basic block given below. a = b + c c = a + d d = b + c e = d - b a = e + b
  • 45. [GATE – Compiler Design] Page 40 The minimum number of nodes and edges present in the DAG representation of the above basic block respectively are (A) 6 and 6 (B) 8 and 10 (C) 9 and 12 (D) 4 and 4 GATE – CS – 2014 (Set 3) Answer: (A) Explanation: Simplifying the given equations : d = b + c (given) e = d – b (given) => d = b + c and e = c e = d – b (given) a = e + b (given) => a = d Thus, the given DAG has 6 nodes and 6 edges. 6. One of the purposes of using intermediate code in compilers is to
  • 46. [GATE – Compiler Design] Page 41 A make parsing and semantic analysis simpler. B improve error recovery and error reporting. C increase the chances of reusing the machine-independent code optimizer in other compilers. D improve the register allocation. GATE-CS-2014-(Set-3) Answer: (C) Explanation: After semantic Analysis, the code is converted into intermediate code which is language independent, the advantage of converting into intermediate code is to improve the performance of code generation and to increase the chances of reusing the machine- independent code optimizer in other compilers. 7. Which one of the following is FALSE? A A basic block is a sequence of instructions where control enters the sequence at the beginning and exits at the end. B Available expression analysis can be used for common subexpression elimination. C Live variable analysis can be used for dead code elimination. D x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination. GATE-CS-2014-(Set-1) Explanation: (A) A basic block is a sequence of instructions where control enters the sequence at the beginning and exits at the end is TRUE. (B) Available expression analysis can be used for common subexpression elimination is TRUE. Available expressions is an analysis algorithm that determines for each point in the program the set of expressions that need not be recomputed. Available expression analysis is used to do global common subexpression elimination (CSE). If an expression is available at a point, there is no need to re-evaluate it. (C)Live variable analysis can be used for dead code elimination is TRUE. (D) x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination is FALSE. Common subexpression elimination (CSE) refers to compiler optimization replaces identical expressions (i.e., they all evaluate to the same value) with a single variable holding the computed value when it is worthwhile to do so.
  • 47. [GATE – Compiler Design] Page 42 Below is an example In the following code: a = b * c + g; d = b * c * e; it may be worth transforming the code to: tmp = b * c; a = tmp + g; d = tmp * e; 8. Some code optimizations are carried out on the intermediate code because (A) they enhance the portability of the compiler to other target processors (B) program analysis is more accurate on intermediate code than on machine code (C) the information from dataflow analysis cannot otherwise be used for optimization (D) the information from the front end cannot otherwise be used for optimization Answer: (A) Explanation: Option (B) is also true. But the main purpose of doing some code-optimization on intermediate code generation is to enhance the portability of the compiler to target processors. So Option A) is more suitable here. Intermediate code is machine/architecture independent code. So a compiler can optimize it without worrying about the architecture on which the code is going to execute (it may be the same or the other ). So that kind of compiler can be used by multiple different architectures. In contrast to that, suppose code optimization is done on target code, which is machine/architecture dependent, then the compiler has be specific about the optimizations on that kind of code. In this case the compiler can’t be used by multiple different architectures, because the target code produced on different architectures would be different. Hence portability reduces here. 9. In a simplified computer the instructions are:
  • 48. [GATE – Compiler Design] Page 43 The computer has only two registers, and OP is either ADD or SUB. Consider the following basic block: Assume that all operands are initially in memory. The final value of the computation should be in memory. What is the minimum number of MOV instructions in the code generated for this basic block? (A) 2 (B) 3 (C) 5 (D) 6 Answer: (B) Explanation: For Instructions of t2 and t3 1. MOV c, t2 2. OP d, t2(OP=ADD) 3. OP e, t2(OP=SUB)
  • 49. [GATE – Compiler Design] Page 44 For Instructions of t1 and t4 4. MOV a, t1 5. OP b, t1(OP=ADD) 6. OP t1, t2(OP=SUB) 7. MOV t2, a(AS END Value has To be in the MEMORY) 10. Which one of the following is FALSE? (A) A basic block is a sequence of instructions where control enters the sequence at the beginning and exits at the end. (B) Available expression analysis can be used for common subexpression elimination. (C) Live variable analysis can be used for dead code elimination. (D) x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination. Answer: (D) Explanation: (A) A basic block is a sequence of instructions where control enters the sequence at the beginning and exits at the end is TRUE. (B) Available expression analysis can be used for common subexpression elimination is TRUE. Available expressions is an analysis algorithm that determines for each point in the program the set of expressions that need not be recomputed. Available expression analysis is used to do global common subexpression elimination (CSE). If an expression is available at a point, there is no need to re-evaluate it. (C)Live variable analysis can be used for dead code elimination is TRUE. (D) x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination is FALSE. Common subexpression elimination (CSE) refers to compiler optimization replaces identical expressions (i.e., they all evaluate to the same value) with a single variable holding the computed value when it is worthwhile to do so. Below is an example
  • 50. [GATE – Compiler Design] Page 45 In the following code: a = b * c + g; d = b * c * e; it may be worth transforming the code to: tmp = b * c; a = tmp + g; d = tmp * e; 11. Consider the following C code segment. for (i = 0, i<n; i++) { for (j=0; j<n; j++) { if (i%2) { x += (4*j + 5*i); y += (7 + 4*j); } } } Which one of the following is false? (A) The code contains loop invariant computation (B) There is scope of common sub-expression elimination in this code (C) There is scope of strength reduction in this code (D) There is scope of dead code elimination in this code Answer: (D) Explanation: Question asks about false statement
  • 51. [GATE – Compiler Design] Page 46 4*j is common subexpression elimination so B is true. 5*i can be moved out of inner loop so can be i%2. Means, A is true as we have loop invariant computation. Next, 4*j as well as 5*i can be replaced with a = - 4; before j loop then a = a + 4; where 4*j is computed, likewise for 5*i. C is true as there is scope of strength reduction. By choice elimination, we have D. Step 6 should have been enough, if the question hadn’t asked for final value in memory and rather be in register. The final step require another MOV, thus a total of 3.