0% found this document useful (0 votes)
4 views17 pages

Exams

The document contains exam questions from 2021 and 2022 related to compiler design, including topics such as compiler types, phases, error detection, and grammar analysis. It features multiple-choice questions, syntax tree construction, and derivation examples. Additionally, it discusses the differences between compilers and interpreters, as well as the functions of error handlers.
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)
4 views17 pages

Exams

The document contains exam questions from 2021 and 2022 related to compiler design, including topics such as compiler types, phases, error detection, and grammar analysis. It features multiple-choice questions, syntax tree construction, and derivation examples. Additionally, it discusses the differences between compilers and interpreters, as well as the functions of error handlers.
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

Exam 2021

Question one:
1) Which of the following is a compiler type?
a) Single pass b) Two pass c) Multi pass d) all
of them
2) The program that translates the assembly program into
machine code is..
a) compiler b) assembler c) linker d)
interpreter
3) The output of the lexical analyzer is .............
a) String of characters c) a syntax tree
B) A set of regular expressions d) set of tokens
4) Which grammar gives multiple parse trees for the same
string?
a) regular b) ambiguous c) unambiguous d) all of them
5) The compiler phase that checks the program grammar is……
a) Code optimization c) Code generation
b) Syntax analysis d) Semantic analysis
6) which is Considered as the sequence of characters in a token?
a) Mexeme b) lexem c) Texeme d) Pattern
7) A compiler that runs on platform (A) and is capable of
generating executable code for platform (B) is called……..
a) Source-to-source compiler c) Cross-compiler
b) Pass-compiler d) pre-processor
8) Which phase of the compiler is also known as parser?
a) Code optimization c) Semantic analysis
b) Lexical analysis d) syntax analysis
9) A programming language Keywords are recognized in a
compiler during ………
a) Code generation c) data flow analysis
b) lexical analysis d) program parsing
10) The compiler can detect what type of errors?
a) logical error only c) Neither logical nor
grammatical error.
b)grammatical error only d) both grammatical and
logical errors
11) leaf nodes in a parse tree indicate ..........
a) Semi-terminals b) Non- terminals c)terminal
d)sub terminal
12) The number of tokens in the statement printf(“hello”);
a) 5 b) 7 c) 6 d) 4
13) which following is suitable regular expression RE for
an identifier>
a) (letter)(letter|digit)* b) letter c) letter|digit d)
letter+
14) misspelling of identifiers is considered as …………
errors.
a) lexical b) semantic c) logical d) syntax
15) Compiler should report the presence of …….... in the
source program, in translation process.
a) classes b) objects c) errors d) text
question two :
1- For the grammar:
<integer> => <sign><digit>*
<digit> => 0|1|2|3|4|5|6|7|8|9
<sign> =>-|+|e
What are the terminal and nonterminal symbols?
Nonterminal terminal
<integer> , <sign> , <digit> 0,1,2,3,4,5,6,7,8,9,-,+,e

2- Draw a syntax tree for the following java statement:


if (x+3<200) x=0; else y=x*x;
3- Write the lexemes and tokens of the program statement:
p=x+50*y-(y/3);
Lexem Token
P Id
= Assign-op
X Id
+ Add-op
50 Num
* Mult-op
Y Id
- Sub-op
( Punctuation
Y Id
/ Div-op
3 Num
) Punctuation
; Punctuation

Question three:
1) For the product rule:
E→ E + E | E * E | id
Write the left most derivation for the Input string: id + id * id
ANS:
E→ E * E
E→ E + E * E
E→ id + E * E
E→ id + id * E
E→ id + id * id
2) Consider the grammar:
T => T+T | T-T | id And the input string: id+id-id
is this grammar ambiguous? why?
ANS:
Left most derivation Right most derivation
T→ T-T T→ T+T
T→ T+T-T T→ T+T-T
T→ id + T - T T→ T + T-id
T→ id + id - T T→ T + id - id
T→ id + id – id T→ id + id - id
Yes this grammar is ambiguous because because parse tree for left
most derivation and right most drivation is different
Question four:
1-Explain bottom-up approach for the grammar: S=> xxW|y ,
W=>Sz and the input: W=xxxxyzz
W=xxxxSzz
W=xxxxWz
W=xxSz
W=xxW
W=S
2-Construct the parse table for the grammar:
E => TE’
E’ => +TE’| e(epslon)
T => FT’
T’ => *FT’| e
F => (E) | id
ANS:
First set Follow set
This set is created to know calculate what terminal symbol
what terminal symbol is immediately follows a non
derived in the first position by a terminal α in production rules.
non terminal.
FIRST(E) = { (, id } Follow(E)=>{$,)}
First(E’)= {+,e} Follow(E’)=>{$,)}
First(T)= { (, id} Follow(T)=>{+,$,)}
First(T’)= {*, e} Follow(T’)=>{+,$,)}
First(F)= {(, id} Follow(F)=>{*,+,$,)}

Exam 2022
Question one:
1) A compiler can be divided into which two parts
a) linker-interpret b) one pass -two pass c) analysis-
synthesis d) all of them
2) The program that translates the assembly program into
machine code is..
a) compiler b) assembler c) linker d)
interpreter
3) The output of the lexical analyzer is .............
a) String of characters c) a syntax tree
B) A set of regular expressions d) set of tokens
4) Which grammar gives multiple parse trees for the same
string?
a) regular b) ambiguous c) unambiguous d) all of them
5) The compiler can check …..……
a) logical b) syntax c)content d) both a and b
6) which is Considered as the sequence of characters in a token?
a) Mexeme b) lexem c) Texeme d) Pattern
7) A compiler that runs on platform (A) and is capable of
generating executable code for platform (B) is called……..
a) Source-to-source compiler c) Cross-compiler
b) Pass-compiler d) pre-processor
8) which phase of the compiler is lexical analyzer
a) first phase c) third phase
b) second phase e) None of them
9) A programming language Keywords are recognized in a
compiler during ………
a) Code generation c) data flow analysis
b) lexical analysis d) program parsing
10) The compiler can detect what type of errors?
a) logical error only c) Neither logical nor
grammatical error.
b)grammatical error only d) both grammatical and
logical errors
11) leaf nodes in a parse tree indicate ..........
a) Semi-terminals b) Non- terminals c)terminal
d)sub terminal
12) The number of tokens in the statement printf(“hello”);
a) 5 b) 7 c) 6 d) 4
13) which following is suitable regular expression RE for an
identifier>
a) (letter)(letter|digit)* b) letter c) letter|digit d)
letter+
14) misspelling of identifiers is considered as …………
errors.
a) lexical b) semantic c) logical d) syntax
15) Compiler should report the presence of …….... in the
source program, in translation process.
a) classes b) objects c) errors d) text

question two:
1- state two difference between compiler and interpreter
Interpreter Compiler
In reads a statement, executes reads the whole source code at
it, then takes the next statement once
in sequence.
If an error occurs, an interpreter If an error occurs, compiler
stops execution and reports it reads the whole program even
if it encounters several errors
An interpreter can give better compiler is usually much faster
error diagnostics than a than an interpreter
compiler, because it executes
The program statement by
statement

Terminal nonterminal
If, then ,else, epslon ,id , num , Stmt , expr, term
relop
Parse tree can not completed draw because 2x is lexical error so we
should solve error before drawing parse tree
4- Write the lexemes and tokens of the program statement: p=x-
100*y-(y/3)-1;
Lexem Token
P Id
= Assign-op
X Id
- sub-op
100 Num
* Mult-op
Y Id
- Sub-op
( Punctuation
Y Id
/ Div-op
3 Num
) Punctuation
- Sub-op
1 num
; Punctuation

Question three:
1- Lexical analyzer: are responsible for the validity of a token
supplied by the syntax analyzer it is phase of front end .
2- Syntax Analyzer : It checks whether a given program fulfills the
rules in a context free grammar , creates the parse tree of that
source program. Handel errors it is phase of front end .
3-semantic analyzer: it is phase of front end it is work with Syntax
phases to handle large portion of errors detected by compiler
4- intermediate code generator: it is phase of front end , translate
parse tree to intermediate code
5- intermediate code optimization: it is phase of front end
6-code generator: it is phase of back end , translate intermediate
code to assembly
7-code optimization: it is phase of back end

 Symbol Table is built in lexical and syntax analysis phases. It


is collected by the analysis phases and is used by the
synthesis phases to generate code.
2) For the product rule:
E→ E + E | E * E | id
Write the right most derivation for the Input string: id + id * id
ANS:
E→ E + E
E→ E + E * E
E→ E + E * id
E→ E + id * id
E→ id + id * id

Functions of Error Handler:

 Error Detection
 Error Report
 Error Recovery

4) Consider the grammar:


T => T+T | T-T | id And the input string: id-id+id
is this grammar ambiguous? why?
ANS:
Left most derivation Right most derivation
T→ T+T T→ T-T
T→ T-T+T T→ T-T+T
T→ id - T + T T→ T - T+ id
T→ id - id + T T→ T - id + id
T→ id - id + id T→ id - id + id
Yes this grammar is ambiguous because because parse tree for left
most derivation and right most drivation is different
Question four:
1-Explain bottom-up approach for the grammar: S=> xxW|y ,
W=>Sz and the input: W=xxxxyzz
W=xxxxSzz
W=xxxxWz
W=xxSz
W=xxW
W=S

Disadvantages of using Syntax Analysers

 never determine if a token is valid or not


 You can't decide that token is declared & initialized before it
is being used
 Not helps you to determine if an operation performed on a
token type is valid or not

Items stored in Symbol table:


• Variable names and constants
• Procedure and function names
• Literal constants and strings
• Compiler generated temporaries
• Labels in source languages
The basic operations defined on a symbol table include:

type of compile time error:


Syntactical : EX: a missing semicolon or unbalanced parenthesis ,
missing operator, error in structure . ex: printf( “hello”)
Semantical : EX: incompatible value assignment or type
mismatches between operator and operand , undeclared variable
ex: x=50+”hello”
Lexical :EX: This includes misspellings of identifiers, keywords or
operators, exceed the length of identifier,appearance of illegal
character
Ex: +age = 32, y=2x
Logical : code not reachable, infinite loop

5-Construct the parse table for the grammar:


E => TE’
E’ => +TE’| e(epslon)
T => FT’
T’ => *FT’| e
F => (E) | id
ANS:
First set Follow set
This set is created to know calculate what terminal symbol
what terminal symbol is immediately follows a non
derived in the first position by a terminal α in production rules.
non terminal.
FIRST(E) = { (, id } Follow(E)=>{$,)}
First(E’)= {+,e} Follow(E’)=>{$,)}
First(T)= { (, id} Follow(T)=>{+,$,)}
First(T’)= {*, e} Follow(T’)=>{+,$,)}
First(F)= {(, id} Follow(F)=>{*,+,$,)}

You might also like