You are on page 1of 5

B.Tech.

(Computer Science and Engineering)


Semester: VII
Subject: Compiler Construction (BCO 028A)
Marks: 50

Assignment #2
SECTION A (1*10=10 Marks)
1 LR Parser is a type of ____________ parser.
a) Top-Down b) Bottom-Up. c) Left-Right d) Right-Left
2 In LR parser, what does R stand for?
a) Rightmost Derivation. b) Rational Derivation
c) Roaming Derivation d) Real Time Derivation
3 Which of these is a component of LR Parser?
a) Stack b) Input Buffer c) Parsing Table d) All of these.
4 Which of these is not a component of the parsing table?
a) Goto b) Action c) Buffer. d) States
5 In the LALR parser, what does LA stand for?
a) Logic Acceptance b) Loop Ahead
c) Look Ahead. c) Loop Acceptance
6 Which parser is largest in size?
a) LALR b) SLR c) CLR. d) GLR
7 LR Parser is also called LR(k) parser.
a) True. b) False
8 ________ item consists of productions of grammar with dots at some point on the
right side.
a) LR(0). b) LL(0) c) LR(1) d) SLR(1)
9 Which parser provides immediate error detection?
a) LALR. b) SLR c) CLR d) GLR
10 __________ action is taken when parsing is complete.
a) forward b) parse c) accept. d) reduce

Last Date of Submission :


B.Tech. (Computer Science and Engineering)
Semester: VII
Subject: Compiler Construction (BCO 028A)
Marks: 50

SECTION B (2*4=8)
1. What are the demerits of SLR parser?
Ans:
A. On single input, State may include a Final Item and a Non- Final Item. This may
result in a Shift-Reduce Conflict.
B. A State may include Two Different Final Items. This might result in a Reduce-Reduce
Conflict.
C. SLR(1) Parser reduces only when the next token is in Follow of the left-hand side of
the production.
D. SLR(1) can reduce shift-reduce conflicts but not reduce-reduce conflicts

2. Define parsing. What is the role of a parser?
Ans: Parsing is the process of identifying tokens within a data instance and looking for
recognizable patterns. The role of a parser is to segregate each word, attempt to determine the
relationship between the word and previously defined token sets, and then form patterns from
sequences of tokens.
3. What is a Simple LR parser or SLR parser?
Ans: A Simple LR or SLR parser is a type of LR parser with small parse tables and a
relatively simple parser generator algorithm. SLR parser is quite efficient at finding the single
correct bottom-up parse in a single left-to-right scan over the input stream, without
guesswork or backtracking.

4. What is the canonical LR parser?


Ans: CLR parsing uses the canonical collection of LR (1) items to build the CLR (1) parsing
table. CLR (1) parsing table produces the most number of states as compared to the SLR (1)
parsing.

Last Date of Submission :


B.Tech. (Computer Science and Engineering)
Semester: VII
Subject: Compiler Construction (BCO 028A)
Marks: 50

SECTION C (6*2=12 Mark)


1. Explain the FIRST-FOLLOW procedure.
Ans: The construction of a predictive parser is aided by two functions associated with a
grammar G. These functions, FIRST and FOLLOW, allow us to fill in the entries of a
predictive parsing table for G, whenever possible. Sets of tokens yielded by the FOLLOW
function can also be used as synchronizing tokens during panic-mode error recovery.

FIRST(α)
If a is any string of grammar symbols, let FIRST(α) be the set of terminals that begin the
strings derived
from α. If α => ε then ε is also in FIRST(α).

FOLLOW(A)
Define FOLLOW(A), for nonterminal A, to be the set of terminals a that can appear
immediately to the right of A in some sentential form, that is, the set of terminals a such that
there exists a derivation of the form S=αAaꞵ for some α and ꞵ. Note that there may, at some
time during the derivation, have been symbols between A and a, but if so, they derived ε and
disappeared. If A can be the rightmost symbol in some sentential form, then $, representing
the input right endmarker, is in FOLLOW(A).

Last Date of Submission :


B.Tech. (Computer Science and Engineering)
Semester: VII
Subject: Compiler Construction (BCO 028A)
Marks: 50

2. Define handle. Write a short note on handle pruning.

Ans: HANDLE PRUNING is the general approach used in shift-and-reduce parsing.

A Handle is a substring that matches the body of a production. Handle reduction is a step in
the reverse of rightmost derivation. A rightmost derivation in reverse can be obtained by
handle pruning.

The implementation of handle pruning involves the following data-structures:- a stack - to


hold the grammar symbols; an input buffer that contains the remaining input and a table to
decide handles.

SECTION D (10*2=20 Mark)


1. Compute FIRST and FOLLOW of given grammars:

a. E → E+T | T
T → T*F | F
F → (E) | id

b. E → TE′
E′ → +TE′ | ε
T′ → FT′
T → *FT′| ε
F → (E) | id

Last Date of Submission :


B.Tech. (Computer Science and Engineering)
Semester: VII
Subject: Compiler Construction (BCO 028A)
Marks: 50

Ans:

After eliminating left-recursion the grammar is

E → TE’

E’ → +TE’ | ε

T → FT’

T’ → *FT’ | ε

F → (E) | id

First( ) :

FIRST(E) = { ( , id}

FIRST(E’) ={+ , ε }

FIRST(T) = { ( , id}

FIRST(T’) = {*, ε }

FIRST(F) = { ( , id }

Follow( ):

FOLLOW(E) = { $, ) }

FOLLOW(E’) = { $, ) }

FOLLOW(T) = { +, $, ) }

FOLLOW(T’) = { +, $, ) }

FOLLOW(F) = {+, * , $ , ) }

2.  Construct LR(0) & SLR parsing tables for given grammar:

E → E+T | T
T → T*F | F
F → (E) | id

Ans: Page: 31
https://www.jntua.ac.in/gate-online-classes/registration/downloads/material/a1592805
08451.pdf

Last Date of Submission :

You might also like