Compilers

Question 1
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.
D
Arguments to a function can be passed using the program stack.
       Compiler-Design       Compilers       Gate 2018
Question 1 Explanation: 
Type checking is done in semantic analysis phase after syntax analysis phase (i.e., after parsing).
Question 2

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
       Compiler-Design       Compilers       Gate 2018
Question 2 Explanation: 
a? means either 0 or 1 occurrence of “a”, so we can write T1, T2 and T3 as:
T1 : (b+c)*a + a(b+c)*a
T2 : (a+c)*b + b(a+c)*b
T3 : (b+a)*c + c(b+a)*c
Now the string is: bbaacabc
Please NOTE:
Token that matches the longest possible prefix
We can observe that the longest possible prefix in string is : bbaac which can be generated by T3.
After prefix we left with “abc” which is again generated by T3 (as longest possible prefix).
So, answer is T3T3.
Question 3

Match the following according to input (from the left column) to the compiler phase (in the right column) that processes 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)
       Compiler-Design       Compilers       GATE 2017(set-02)
Question 3 Explanation: 
Character stream is input to lexical analyzer which produces tokens as output. So Q → (iv).
Token stream is forwarded as input to Syntax analyzer which produces syntax tree as output. So, S → (ii).
Syntax tree is the input for the semantic analyzer, So P → (iii).
Intermediate representation is input for Code generator. So R → (i).
Question 4

Match the following:

(P) Lexical analysis              (i) Leftmost derivation
(Q) Top down parsing             (ii) Type checking
(R) Semantic analysis           (iii) Regular expressions
(S) Runtime environments         (iv) Activation records
A
P ↔ i, Q ↔ ii, R ↔ iv, S ↔ iii
B
P ↔ iii, Q ↔ i, R ↔ ii, S ↔ iv
C
P ↔ ii, Q ↔ iii, R ↔ i, S ↔ iv
D
P ↔ iv, Q ↔ i, R ↔ ii, S ↔ iii
       Compiler-Design       Compilers       GATE 2016 set-2
Question 4 Explanation: 
Regular expressions are used in lexical analysis.
Top down parsing has left most derivation of any string.
Type checking is done in semantic analysis.
Activation records are loaded into memory at runtime.
Question 5
Match the following :
A
P-2,Q-3,R-1,S-4
B
P-2,Q-1,R-4,S-3
C
P-2,Q-4,R-1,S-3
D
P-2,Q-3,R-4,S-1
       Compiler-Design       Compilers       GATE 2015 -(Set-2)
Question 5 Explanation: 
P) Lexical analysis is related with FA and Regular expressions.
Q) Expression can be evaluated with postfix traversals.
R) Register allocation can be done by graph colouring.
S) The parser constructs a production tree.
Hence, answer is ( C ).
Question 6
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
       Compiler-Design       Compilers       Gate 2011
Question 6 Explanation: 
Lexical Analysis is implemented by finite automata
Question 7
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
D
dataflow analysis
       Compiler-Design       Compilers       Gate 2011
Question 7 Explanation: 
Any identifier is also a token so it is recognized in lexical Analysis
Question 8
Which data structure in a compiler is used for managing information about variables and their attributes?
A
Abstract syntax tree
B
Symbol table
C
Semantic stack
D
Parse Table
       Compiler-Design       Compilers       2010
Question 8 Explanation: 
Symbol tables are data structures that are used by compilers to hold information about source-program constructs. The information is collected incrementally by the analysis phases of a compiler and used by the synthesis phases to generate the target code. Entries in the symbol table contain information about an identifier such as its character string (or lexeme) , its type, its position in storage, and any other relevant information.
Question 9
Match all items in Group 1 with correct options from those given in Group    
A
P-4, Q-1, R-2, S-3
B
P-3, Q-1, R-4, S-2
C
P-3, Q-4, R-1, S-2
D
P-2, Q-1, R-4, S-3
       Compiler-Design       Compilers       2009
Question 9 Explanation: 
Lexical analysis phase uses regular expression to identify the pattern of tokens, PDA is used in CFG and hence syntax analysis phase is related to PDA. Data flow analysis is done in code optimization phase and register allocation is related to code generation phase.
Question 10
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
       Compiler-Design       Compilers       Gate-2008
Question 10 Explanation: 
The code-optimization on intermediate code generation will always enhance the portability of the compiler to target processors. The main reason behind this is, as the intermediate code is independent of the target processor on which the code will be executed, so the compiler is able to optimize the intermediate code more conveniently without bothering the underlying architecture of target processor.
Question 11
Consider line number 3 of the following C- program.
int main ( ) {                   /* Line 1 */
  int I, N;                      /* Line 2 */
  fro (I = 0, I < N, I++);       /* Line 3 */
}
Identify the compiler's response about this line while creating the object-module
A
No compilation error
B
Only a lexical error
C
Only syntactic errors
D
Both lexical and syntactic errors
       Compiler-Design       Compilers       Gate-2005
Question 11 Explanation: 
There is no error in the above code. Actually it is a link error. Here compiler fro is a function which is not declared. Hence, it will not produce any error. It will only throw a warning in C.
Question 12
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
A
Edit time
B
Compile time
C
Link time
D
Load time
       Compiler-Design       Compilers       Gate-2004
Question 12 Explanation: 
The link time can gives the reference to the executable file when the functions are present in the other modules.
Question 13
 
A
3
B
26
C
10
D
21
       Compiler-Design       Compilers       Gate-2000
Question 13 Explanation: 
We have six different types of tokens are available
(i) Keyword
(ii) Identifier
(iii) Constant
(iv) Variable
(v) String
(vi) Operator
Print = Token 1
( = Token 2
"i=%d%x" = Token 3 [Anything inside " " is one Token]
, = Token 4
i = Token 5
, = Token 6
& = Token 7
i = Token 8
) = Token 9
; = Token 10
Here, totally 10 Tokens are present in the equation.
Question 14
Type checking is normally done during
A
lexical analysis
B
syntax analysis
C
syntax directed translation
D
code optimization
       Compiler-Design       Compilers       Gate-1998
Question 14 Explanation: 
Type checking is normally done during syntax directed translation.
Question 15
Generation of intermediate code based on an abstract machine model is useful in compilers because
A
it makes implementation of lexical analysis and syntax analysis easier
B
syntax-directed translations can be written for intermediate code generation
C
it enhances the portability of the front end of the compiler
D
it is not possible to generate code for real machines directly from high level language programs
       Compiler-Design       Compilers       Gate-1994
Question 15 Explanation: 
In Intermediate code optimizations can also enhances the probability of optimizer.
Question 16
 
A
The program leads to compile time error
B
The program leads to run time error
C
The program outputs 5.2
D
The program produces error relating to nil pointer dereferencing
E
None of the above
       Compiler-Design       Compilers       Gate-1993
Question 16 Explanation: 
Note: Out of syllabus.
Question 17
In a compiler the module the checks every character of the source text is called:
A
The code generator.
B
The code optimiser.
C
The lexical analyser.
D
The syntax analyser.
       Compiler-Design       Compilers       GATE-1987
Question 17 Explanation: 
Lexical analyzer phase checks every character of text to identify tokens.
Question 18
Using longer identifiers in a program will necessarily lead to:
A
Somewhat slower compilation
B
A program that is easier to understand
C
An incorrect program
D
None of the above
       Compiler-Design       Compilers       GATE-1987
Question 18 Explanation: 
Lexical analyzer will take more time to recognize the longer identifiers.
There are 18 questions to complete.
PHP Code Snippets Powered By : XYZScripts.com