0% found this document useful (0 votes)
64 views15 pages

Compiler Construction Past Questions

The document contains examination questions for a Compiler Construction course at the University of Ibadan, covering topics such as compiler structure, qualities of good compilers, differences between compilers and interpreters, and grammar types. It includes specific questions on semantic analysis, error handling, and the importance of BNF in compiler design. Students are required to answer multiple questions within a two-hour timeframe.

Uploaded by

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

Compiler Construction Past Questions

The document contains examination questions for a Compiler Construction course at the University of Ibadan, covering topics such as compiler structure, qualities of good compilers, differences between compilers and interpreters, and grammar types. It includes specific questions on semantic analysis, error handling, and the importance of BNF in compiler design. Students are required to answer multiple questions within a two-hour timeframe.

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE

UNIVERSITY OF IBADAN
B.Sc. Degree Examinations (First Semester, 2016/2017)

CSC 431 (COMPILER CONSTRUCTION)

Answer Question 1 and any other two  2 Hours

Q1. (a) Discuss with a diagram the general overview of a compiler. [7 marks]

Ans:

• Scanner: The simplest past of the compiler and is sometimes called a LEXICAL
ANALYSER. It scans the characters of the source program from left to right and build
the actual symbol of the program.
• Syntax and Semantic Analysis: Attaches meaning to the structures by generating
internal code using polish notation, triple or quadruple. Optimization is done, storage
problems is solved and other necessary things for the program to run are done.
• Preparation for code generation: This after semantic analysis the compiler generates
an intermediate code of the source code for the target machine. One important point
includes optimization of the program in order to reduce the execution time of the object
program. Optimization can be assumed as something that removes unnecessary code
lines, and arranges the sequence of statements in order to speed up the program
execution without wasting resources (C.P.U, memory).
• Code generation: This is the actual translation of the internal source programs into
assembly or machine language. In this phase, code generator takes the optimised
representations of the intermediate code and maps it to the target machine language.
Code generator translates the intermediate code into a sequence of relocatable machine
code.

(b) What are the qualities of a good compiler? [3 marks]

Ans:
1. Good Error Diagnostic and reporting.
2. Speed.
3. Efficiency, accuracy and Bug free. (All must be explained).

(c) Give two similarities and two differences between compilers and interpreters. [4 marks]

Ans:
Similarity
1. Both check our program for syntactic and semantic correctness.
2. The lexical and syntax phases of compilation may be likened to the interpreter.
3. Both programs/software on their own which are mainly used as tools of correct
programming.
Differences
1. Effort: when it comes to effort required to execute the source programs, an interpreter
definitely has advantage over a compiler.
2. Versatility: in terms of versatility an interpreter is more versatile than a compiler. A
compiler is always made for a particular machine, but an interpreter can run on different
machine.
3. Debugging: its easier in interpreter than compiler.
4. Runtime error is easily thrashed by interpreter than compiler.

(d) Differentiate between


(i) Compiler Pass and Compiler Phase. [3 marks]

Ans:
Compiler pass: this is the implementation of collection of phases in compilation. Compiler
pass is of 2 types which are;
Single phase and Multi pass
Single phase: It implement all the 4 phases of compilation in one module.
Multi pass: Implement all the phases of compilation into different modules.

Compiler phase: This refer to the stage that involved in the compilation process such as
lexical, syntax, semantic analysis and code optimization and generation phase.

(ii) Compiler-compilers and Cross compilers [3 marks]

Ans:
Compiler-compilers: This are tools that can be used to write a compiler. Compiler-
compilers are generated which produce a compiler for given language from the definition of
the language.

Cross compilers: These are translators which are used on one computer in order to produce
object programs used on a micro-computer.
(e) Given i8the grammar:
G = ({S, A, B}, {a, b, c}, { S→aA, A →bA, A →cB, B →bB, B → a}, S)

(i) Explain the four tuples that make up the grammar G [2 marks]

Ans:
Non-Terminals, VN is the set { S,A,B}
Terminals, VT is the set {a,b,c}
Production P is the set {S →aA, A →bA, A →cB, B →bB, B → a}
Start symbol is S

(ii) Obtain the derivation trees for the following sentences


abbcba and acbbba [8 marks]
Ans:
For sentences abbcba
S

a A

b A

b A

c B

b B

a
The Production used is {S →aA, A →bA, A →cB, B →bB, B → a}

For sentences acbbba


S

a A

c B

b B

b B

b B

a
The Production used is {S →aA, A →cB, B →bB, B → a}

Q2. Given the grammar:


G = ({S, T, F}, { +, a, *, (, )}, {S →S + T/T, T →T * F / F, F → ( S ) / a}, S)

By Using a precedence matrix algorithm, show if a * (a + a ) is a sentence accepted by the


language. [20 marks]
Ans:

First, we construct the set of first and last terminals as follows


Non-Terminals First Terminal Last Terminals
S +,*,(,a +,*,),a
T *,(,a *,),a
F (,a ),a

Now we construct the precedence matrix as follows with end markers #

Conclusion: Since we are able to reduce the expression to start symbol E, the sentence a*(a + a) is
acceptable by the grammar.

Q3. (a) Identify and explain any five significant routines necessary for implementing a semantic
analyser. [5 marks]
Ans:
• LOOK-UP (Name, P): Routine to check symbol table for “name “and returns the symbol
table address P.
• LOOK-UP-DEC: Behaves like LOOK-UP routine to determine whether “name” is
declared or not.
• INSERT (name, P): Routine to insert “name” at symbol table entry P.
• ENTER (Operator, Operand 1, Operand 2, Operand 3): Routine to enter the specified
quadruple.
• CHECK_TYPE (p, n): Routine to check if the symbol table entry at P is of type n. Note:
P is assumed to have been declared by the syntax Analyser.

b) Explain any two intermediate languages that can be used in semantic analysis to re-write
codes accepted by the syntax analyser. [3 marks each = 6]

Ans:
• Postfix reverse polish notation
• Quadruple
• Triple

• Postfix reverse polish notation: Is used to represent arithmetic or logical expression in a


manner which specifies simply and exactly the order in which operators are to be
evaluated. It’s sometimes called suffix or post fix notation.

• Quadruple: A convenient form for a single binary operation is a quadruple depicted


below; <operator>, <operand 1>, <operand 2>, result where; <operand 1> and < operand
2> specify the argument and <result>, the result.

• Triple: Each Instruction in triples presentation has three fields: op, arg1 and arg2. The
result respective sub expression are denoted by the position of expression, op means
operator, arg means argument.
(NOTE: Any Two (2) to Take)
Op arg1 arg2
* c d
+ b (0)
+ (1) (0)
= (2)

(a) Consider the following FORTRAN code snippet:


DIMENSION A(100)
IF (A(I) .LE. A(J) ) GOTO 10
T = A(2 * (J + 1))
A(J) = A(I)
A(2) = T
10 A = A + B + C

Re-write the FORTRAN code snippet using a Semantic analysis intermediate language of your
choice. [9 marks]
Ans:

DIMENSION A(100) 1. 100A ADEC


IF (A(I) .LE. A(J) ) GOTO 10 2. I, A, ASUBS, J, A, ASUBS
T = A(2 * (J + 1)) 3. T, 2JI+*A, ASUBS=
A(J) = A(I) 4. J, A, ASUBS, I, A, ASUBS=
A(2) = T 5. 2, A, ASUBS, T, =
10 A = A + B + C 6. AABC++=

Q4. (a) Give any five sources of error in program design and implementation [5 marks]

` Ans:
i. Error in compiler
ii. Error in compiler environment (Hardware, operating system)
iii. Design Errors in the programs being compiled
iv. An incomplete understanding of the source long by compiler
v. Transcription error
vi. Incorrect data

(b)What is the purpose of error handling in compiler construction? [3 marks]

Ans:

The purpose of error handling in compiler construction is to aid programmers by highlighting


inconsistencies in the programs.
(c) Explain the tasks of error handling routine in compilers. [3 marks]

Ans:
• To detect errors
• Report the errors detected by the users
• Possibly make some repair to allow processing to continue

(d) Explain the different classifications of errors in compiler error diagnostics [9 marks]

Ans:

• Note: refers to non-standard constructs and are only important for programs that will be
transported to other implementations.
• Comment: It criticize programming styles
• Warning: this refers to possible errors
• Fata error: suppress production of an executable program but the compiler will recover
from them
• Ordinary error: It cause compilation to terminate.

NOTE: (1) to (III) are reports of anomalies while level (IV) to (V) are reports of
actual errors or violation of limits.
UNIVERSITY OF IBADAN

DEPARTMENT OF COMPUTER SCIENCE


B.SC. DEGREE EXAMINATIONS (FIRST SEMESTER, 2017/2018)

CSC 431 (Compiling Techniques)


Answer Question 1 and any other two  2 Hours

Q1. (a) Give two reasons why a Computer Science Student must study the design and
construction of compilers. [2 marks]

Ans:

i. Student study design and construction of compilers to understand how code translation is
done.
ii. Student study design and construction of compilers to understand the purpose of
evolving new or better ones.

(b) Give two reasons why knowledge of Linguistic Analysis is very important in
Compiler design [2 marks]
Ans:

i. To understand the structure of programming languages.


ii. Compilers are used for specific languages, which need to be critically studied.
iii. New language can be invented if the study of the language is critically studied.

Note: (Pick 2 out of 3)

(c) Explain the following types of grammar:


(i) Context Free Grammar
(ii) Context Sensitive Grammar
(iii) Chomsky Type III Grammar
(iv) Precedence Grammar
(v) Operator Precedence Grammar [3 marks each = 15]

Ans:
i. Context Free Grammar: Given ꝩ1Aꝩ2→ ꝩ1Bꝩ2 boiling down to having a single non-
terminal on the left like B → aA, i.e., removing the restriction of the context of ꝩ1 on the
left and ꝩ2 on the right such that the production now becomes A → B, the grammar is
Chomsky Type Two or Context Free Grammar (CFG).

ii. Context Sensitive Grammar: If α, β in the production α → β are restricted such that α =
ꝩ1A ꝩ2 and β = ꝩ1B ꝩ2, then the production becomes ꝩ1A ꝩ2 = ꝩ1B ꝩ2. There is ꝩ1 on the
left and ꝩ2 on the right of A and B. Therefore, there is a context between A and B. We
can re-write the productions as A / ꝩ1 - ꝩ2 → B / ꝩ1 - ꝩ2 i.e. A with ꝩ1 on the left and ꝩ2
on the right derives B with ꝩ1 on the left and ꝩ2 on the right. A ℇ VN; ꝩ1, ꝩ2, B ℇ V*. This
type of grammar is called Chomsky Type One or Context Sensitive Grammar (CSG).

iii. Chomsky Type III Grammar: This is a Regular Grammar or Chomsky Type Three or
Finite State Grammar. In the production, A → B, we place a restriction on the string B
such that B is either a terminal symbol or it is a terminal symbol followed by non-
terminal symbol such as above. The grammar is called Regular Grammar which could be
left regular (e.g. A → b, A → aC) or right regular (e,g, A → b, A → Ca).
iv. Precedence Grammar: A precedence grammar is a context free grammar, which has the
additional property that a precedence relationship exists between some pairs of symbols
of the total vocabulary V of the grammar.

v. Operator Precedence Grammar: An operator precedence grammar is an operator


grammar in which a unique precedence relationship exists or not between any pair of
terminal symbols.

(d) Consider the following grammar productions


P1: aAb →acdeb
P2: A → cde

i. Identify the type of Grammar applicable to the two productions above [2 marks]

Ans:

Given:
P1: aAb →acdeb
P2: A → cde
The type of Grammar applicable to the two productions above are
P1: Context Sensitive Grammar (CSG);
P2: Context Free Grammar (CFG)

(ii) Identify one sentence and one sentential form from the productions [2 marks]

Ans:

Sentence are cde and acdeb and sentential form is aAb

(iii) Apply P1 on the following strings pqaAbr, fgcAb and aAc and make your
Conclusions [3 marks]
Ans:

pqaAbr becomes: pq acdebr


fgcAb: since P1 is context sensitive P1 cannot be applied to the string
aAc: since P1 is context sensitive P1 cannot be applied to the string

(e) What grammar generates the following language? L(G) = {abk abk a / k >= 0} [4 marks]

Ans:

L(G) = {abk abk a / k >= 0}


The grammar G = ({S, A, B} {a,b},{S → aAa, A → bAb, A → a}, S)

Q2. (a) What is the importance of BNF in Compiler Construction? Given that A represents Numbers,
B and C are digits, interpret the following BNF notations.

Ans:
BNF is a tool for defining the syntax of various programming languages. A BNF
grammar is a set terminal symbols Ʃ.a set of non-terminal symbols N and a start symbol,
S ℇ N, A rewriting rule is of the form.
A → W where;
A ℇ N and w ℇ (N U Ʃ). That is w is a strong of non-terminal and terminal symbols.

(i) A → B| [+ | - ] . C | B . C

Ans:

It means A number can be an integer or + or – sign symbol with a number with


decimal (e.g. +3) or number with decimal values (e.g. 2.4).

(ii) A → B{B} [5 marks]

Ans:

It means A number can be an integer, optionally followed by another integer (23).

(b) Write an algorithm that uses a stack to convert an infix expression to its postfix
equivalent [5 marks]

Ans:

While (more symbol s, x) {


Scan a symbol, x
If x is an identifier or constants,
Push (s,x)
If x is a binary operator, OP(+,-,/.%,*){
Apply OP to two top(s) symbols and compute result r
Replace s (top(s)<-- r
}
}

(c) Explain literarily or using algorithm how operator precedence parsing algorithm works
[5 marks]
Ans:

STEP 1: Define two functions: FIRST and LAST for any non-terminal define a set FIRST as
first terminal of the given non-terminal same for the set LAST.

STEP 2: Next, we construct the precedence matrix as follows with the end markers #

STEP 3: Then the stack implementation, using stack to implement the precedence algorithm.

STEP 4: Then we make conclusion: if we are able to reduce the expression to the start symbol E
Q3. (a) Briefly explain with the aid of a diagram, the different phases of compilation [6 marks]

Ans:

Explanation of the analysis phase (lexical, syntax and semantic and the synthesis phase (code
generation).

LEXICAL ANALYSIS: The compiler takes the source program statement, character by
character and separates it into basic tokens (keywords, separators, operators, constants,
identifiers etc.)

SYSNTAX ANALYSIS: Takes result coming from the lexical analyser and builds a syntactic
tree to confirm whether any entity or token is acceptable to it.

SEMANTIC ANALYSIS: Attaches meaning to the structures by generating internal codes using
polish notation, triples quadruples. Optimization is done, storage problems solved and other
necessary things for the program to run are done.

(b) Despite the many advantages of interpreters, compilers are still preferred. Discuss. [5 marks]

Ans:

Compilers always translate a high-level language into an intermediate assembly language, where
as interpreter does not produce an object program in terms of translation.

In terms of speed execution of object program are fast, since object program are generated is
executed at the top speed.

Compilers are preferred of the reduced amount of programming time and debugging and the
readability of the compiler when finished.

(c) Identify and explain the four alternatives for generating object codes in
compiler construction [4 marks]

Ans:
i. Generate an absolute machine language if the anatomy of the system (registers) is
known.
ii. Generate a relocatable machine language if link loader exists.
iii. Generate an assembly language code if assembly is available.
iv. Source code to generate PASCAL
Q4. (a) Identify and explain any five significant routines necessary for implementing a semantic
analyser. [5 marks]

Ans:

Refers to Q3 (a) (2016/2017)

(c) Explain any two intermediate languages that can be used in semantic analysis
to re-write codes accepted by the syntax analyser. [3 marks each = 6]

Ans:

Refers to Q3 (b) (2016/2017)

(d) Consider the following FORTRAN code snippet:


DIMENSION A(100)
IF (A(I) .LE. A(J) ) GOTO 10
T = A(2 * (J + 1))
A(J) = A(I)
A(2) = T
10 A = A + B + C

Re-write the FORTRAN code snippet using a Semantic analysis intermediate language of your
choice. [4 marks]

Ans:

Refers to Q3 (c) (2016/2017)


UNIVERSITY OF IBADAN
DEPARTMENT OF COMPUTER SCIENCE

B.SC. DEGREE EXAMINATIONS (FIRST SEMESTER, 2018/2019)

CSC 431 (Compiling Techniques)


Answer Question 1 and any other two  2 Hours

Q1. (a) Consider the following high level code, re-write the code using a Semantic analysis
intermediate language of your choice.

sum = 0, j = 2
i=1
10 sum += i + j
j++
i++
if i > 5 GOTO 10 [12 marks]
Ans:

The High level codes The Postfix conversion

sum = 0, sum 0 =
j=2 j2=
i=1 i1=
10 sum += i + j 10 sum I j ++ =
j++ j ++
i++ i ++
if i > 5 GOTO 10 i 5 – BP 10

(b) What are the qualities of a good compiler? [3 marks]


Ans:

Refer to Q1 (b) (2016/2017)

(c) Give two similarities and two differences between compilers and interpreters. [4 marks]

Ans:

Refer to Q1 (c) (2016/2017)

(d) Differentiate between


(iii) Compiler Pass and Compiler Phase. [3 marks]

Ans:

Refer to Q1 (d) (2016/2017)

(iv) Compiler-compilers and Cross compilers [3 marks]

Ans:

Refer to Q1 (d) (2016/2017)


(e) Explain why a compiled program is faster than the interpreted one [5 marks]

Ans:

Compiled program is translated only once and they can be used anytime without re-
translation where as translation always take place on each time an interpreted program is
used and translation is done line by line.

Q2. (a) What is the importance of BNF in Compiler Construction? Given that A represents Numbers,
B and C are digits, interpret the following BNF notations
(iii) A → B| [+ | - ] . C | B . C
(iv) A → B{B} [5 marks]

Ans:

Refer to Q2 (a) (2017/2018)

(b) Write an algorithm that uses a stack to convert an infix expression to its postfix
equivalent [7 marks]

Ans:

Refer to Q2 (b) (2017/2018)

(c) Explain literarily or using algorithm how operator precedence parsing algorithm works
[8 marks]
Ans:

Refer to Q2 (c) (2017/2018)

Q3. (a) Derive the grammar G that generates the language L(G) = {10k10n /k, n >= 0} [5 marks]

Ans:

The minimum string we can get from the language is 111, when k, n = 0.
The grammar G will be;
G = ({S, A, B, C} {0, 1}, P, S)
Where P = {S → 1B, B → 0B, B → 1C, C → 0C, C → 1}

(b) Write short notes on the following grammars, citing relative examples:

Ans:

(i) Chomsky Type I: If α, β in the production α → β are restricted such that α = ꝩ1A ꝩ2
and β = ꝩ1B ꝩ2, then the production becomes ꝩ1A ꝩ2 = ꝩ1B ꝩ2. There is ꝩ1 on the left
and ꝩ2 on the right of A and B. Therefore, there is a context between A and B. We can
re-write the productions as A / ꝩ1 - ꝩ2 → B / ꝩ1 - ꝩ2 i.e. A with ꝩ1 on the left and ꝩ2 on
the right derives B with ꝩ1 on the left and ꝩ2 on the right. A ℇ VN; ꝩ1, ꝩ2, B ℇ V*. This
type of grammar is called Chomsky Type One or Context Sensitive Grammar (CSG).
(ii) Chomsky Type III: This is a Regular Grammar or Chomsky Type Three or Finite
State Grammar. In the production, A → B, we place a restriction on the string B such
that B is either a terminal symbol or it is a terminal symbol followed by non-terminal
symbol such as above. The grammar is called Regular Grammar which could be left
regular (e.g. A → b, A → aC) or right regular (e,g, A → b, A → Ca).

(iii) Ambiguous Context Free Grammars: A grammar G is ambiguous if there


exists a sentence w for which more that one derivation tree can be constructed.
Consider the grammar:
G = ({S, A}, {a, b}, {S → aAS, S → a, S → SbA, A → ba, A → SS}, S).

Suppose we are given the string abaa and we want to determine if it’s a member
of the language generated by G then the following tree can be construction.

1) S 2) S

a A S S b A

b a a a S S

a a

The two trees derive the sentence abaa but they are structure rally different.
Therefore, the grammar G is ambiguous.

[5 marks each = 15]

Q4. (a) Explain the Code Optimization and Generation Phase of a compiler.

Ans:

Code optimization: this is the process of reducing the execution time of the object
program by remaining unnecessary line of codes and arranging line of codes, and
arranging the sequence of statements in order to speed of the program execution without
wasting resources ( CPU, memory).

Generation phase of a compiler: This involves the translation of the internal source
program into assembly or machine language. the code generator translates the
intermediate code into a sequence of re-locatable machine code.

(b) What difficulties are usually encountered in code generation?

Ans:
(c) What are the alternatives available for generating outputs in code generation?

Ans:

i. Generate an absolute machine language if he anatomy of the system (Registers) is


unknown.

ii. Generate a relocatable machine language if link loader exists.


iii. Generate an assembly language code if assembler is available.
iv. Generate another programming language like taking BASIC source codes to
generate PASCAL.

(d) In between compilers and interpreters, which will you prefer and why?
[5 marks each = 20]
Ans:

I will prefer an interpreter, because interpreter boast an eye-catching number of


advantages over compiler such as;

i. Effort: An interpreter translates source program line by line and does not
produce an object program, but a compiler translates the whole program at once.
ii. Versatility: An interpreter is more versatile than a compiler, because it can run
on many computers which compiler cannot.
iii. Runtime Error: Run time error is easily detected and debugged by interpreter,
since its in control when it’s executing a source program.
iv. Simulation: interpreter can be used to simulate a new machine not yet provided,
but for which software is already written.
v. Debugging: interpreters are a very effective debugging tools than compilers.

You might also like