2023 24 PCD UNIT 2 Modified
2023 24 PCD UNIT 2 Modified
PRINCIPLES OF
COMPILER DESIGN
(2023-2024)
1
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
UNIT-II : PARSING
Objective:
o Understand Top Down Parsing.
o To familiarize with different Bottom-up Parsers.
Syllabus:
Syntax analysis, role of a parser, classification of parsing techniques, top down
Parsing, Recursive descent, First and Follow, LL (1) Grammars, non-Recursive
predictive parsing
Learning Material
Syntax Analysis:
• Syntax analysis or parsing is the second phase of a compiler. It checks the syntactical
structure of the given input, i.e. whether the given input is in the correct syntax (of the
language in which the input has been written) or not. It does so by building a data structure,
called a Parse tree. The parse tree is constructed by using the pre-defined Grammar of the
language and the input string.
• We have seen that a lexical analyzer can identify tokens with the help of regular expressions
and pattern rules. But a lexical analyzer cannot check the syntax of a given sentence due to
the limitations of the regular expressions.
• Regular expressions cannot check balancing tokens, such as parenthesis. Therefore, this
2
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Type of errors that a parser cannot detect:
1. Variable re-declaration
2. All the internal nodes in the parse tree are non-terminal symbols.
4. If we write all the leaf nodes of the tree from left to right we get a string, that string
is called “Yield” of that tree
(or)
A string which has more than one leftmost derivation or more than one rightmost
derivation is said to be ambiguous.
Note: A CFL for which every CFG is ambiguous is said to be an inherently ambiguous CFL.
Derivation:
A derivation is basically a sequence of production rules, in order to get the input string.
During parsing, we take two decisions for some sentential form of input:
3
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
• Deciding the production rule, by which, the non-terminal will be replaced.
To decide which non-terminal to be replaced with production rule, we can have two options.
Left-most Derivation: If the sentential form of an input is scanned and replaced from left to
right, it is called left-most derivation. The sentential form derived by the left-most derivation
is called the left-sentential form.
Right-most Derivation: If we scan and replace the input with production rules, from right to
left, it is known as right- most derivation. The sentential form derived from the right-most
derivation is called the right- sentential form.
Example:
Production rules:
E→E+E
E→E*E
E → id
Input string: id + id * id
E→E*E
E→E+E*E
E → id + E * E
E → id + id * E
E → id + id * id
4
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
E→E+E
E→E+E*E
E → E + E * id
E → E + id * id
E → id + id * id
Example:
Precedence:
If two different operators share a common operand, the precedence of operators decides
which will take the operand. That is, 2+3*4 can have two different parse trees, one
corresponding to (2+3)*4 and another corresponding to 2+(3*4). By setting precedence
among operators, this problem can be easily removed. As in the previous example,
mathematically * (multiplication) has precedence over + (addition), so the expression 2+3*4
will always be interpreted as:
2 + (3 * 4)
Associativity:
The associativity of an operator is a property that determines how operators of the same
precedence are grouped in the absence of parentheses. If the operation is left-associative,
then the operand will be taken by the left operator or if the operation is right-associative,
5
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
the right operator will take the operand.
Example:
Operations such as Addition, Multiplication, Subtraction, and Division are left associative. If
the expression contains:
id op id op id
(id op id) op id
Operations like Exponentiation are right associative, i.e., the order of evaluation in the same
expression will be:
id op (id op id)
Left Recursion
Example: S→S+S
S→Sa
It is left recursive production this left recursion can be eliminated by introducing the
following productions.
A→β1A'|β2A'_ _ |βmA'
A'→
6
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Left Factoring:
If more than one grammar production rule has a common prefix string, then the top-down
parser cannot make a choice as to which of the production it should take to parse the
string in hand.
Example:
Then it cannot determine which production to follow to parse the string as both productions
are starting from the same terminal (or non-terminal). To remove this confusion, we use a
technique called left factoring.
Left factoring transforms the grammar to make it useful for top-down parsers. In this
technique, we make one production for each common prefixes and the rest of the
derivation is added by new productions.
Example:
A→
A'→β | δ | …
Now the parser has only one production per prefix which makes it easier to take decisions.
CLASSIFICATION OF PARSERS
2. Bottom up parsing
Top–down parsing: A parser can start with the start symbol and try to transform it to the
input string.
Example: LL Parsers.
Bottom–up parsing: A parser can start with input and attempt to rewrite it into the start
symbol.
7
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Example: LR Parsers. Types
of Top-Down Parsing:
2. Predictive parsing
The parse tree can be constructed using the following top-down approach:
Step1:
Initially create a tree with single node labeled S. An input pointer points to ‘c’, the first
symbol of w. Expand the tree with the production of S.
Step2:
The leftmost leaf ‘c’ matches the first symbol of w, so advance the input pointer to the
second symbol of w ‘a’ and consider the next leaf ‘A’. Expand A using the first alternative.
Step3:
The second symbol ‘a’ of w also matches with second leaf of tree. So advance the input
pointer to third symbol of w is ‘d’. But the third leaf of tree is b which does not match with
the input symbol.
Hence discard the chosen production and reset the pointer to second position. This is called
backtracking.
8
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Step4:
E → E+T | T
T → T*F | F
F → (E) | id
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε F → (E) | id
To compute FIRST(X) for all grammar symbols X, apply the following rules until no more
terminals or e can be added to any FIRST set.
9
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
For example, everything in FIRST(Yj) is surely in FIRST(X). If y1 does not derive e, then we
add nothing more to FIRST(X), but if Y1 ε, then we add FIRST(Y2) and so on.
To compute the FIRST(A) for all non-terminals A, apply the following rules until nothing can
be added to any FOLLOW set.
1. Place $ in FOLLOW(S), where S is the start symbol and $ in the input right end marker.
Consider the following example to understand the concept of First and Follow. Find the
first and follow of all non-terminals in the Grammar-
E → TE'
E'→ +TE'|ε
T →FT'
T'→ *FT'|ε
F -> (E)|id
Then:
FIRST(E)=FIRST(T)=FIRST(F)={(, id}
FIRST(E')={+, ε}
FIRST(T')={*, ε} FOLLOW(E)=FOLLOW(E')={),$}
FOLLOW(T)=FOLLOW(T')={+, ), $}
FOLLOW(F)={+, *, ) ,$}
Example:
id and left parenthesis are added to FIRST(F) by rule 3 in definition of FIRST with i=1 in
Then by rule 3 with i=1, the production T → FT' implies that id and left parenthesis belong to
FIRST (T) also.
10
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
To compute FOLLOW, we put $ in FOLLOW(E) by rule 1 for FOLLOW.
2. Predictive parsing:
• Predictive parsing is a special case of recursive descent parsing where no backtracking
is required.
• The key problem of predictive parsing is to determine the production to be applied for
1.
4. All the remaining entries in the table M are marked as Syntax Error.
1) E → T E'
11
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
2) E' → + T E'|ϵ
3)T → F T'
6) T' → * F T'|ε
8) F →num|id
FIRST[F] is {num, id}. This means that FIRST[T] = FIRST[F] = {num, id}.
The FOLLOW of E is {$} since there is no production that has ‘E’ at the rhs. For E', rules 1, 2,
and 3 have E' at the rhs. This means that the FOLLOW[E'] must contain both the FOLLOW[E]
and the FOLLOW[E'].
The first one is {$} while the latter is ignored since we are trying to find E'. Similarly, to find
FOLLOW[T], we find the rules that have T at the rhs: rules 1, 2, and 3.
Then FOLLOW[T] must include FIRST[E'] and, since E' can be reduced to the empty
sequence, it must include FOLLOW[E'] too (i.e, {$}). That is, FOLLOW[T]={+,-,$}.
To summarize, we have:
FIRST FOLLOW
E {num,id} {$}
T {num,id} {+,-,$}
F {num,id} {+,-,*,/,$}
12
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Now, given the above table, we can easily construct the parsing table by using the
algorithm for constructing predictive parse table.
LL (1) GRAMMAR
A context-free grammar G = (VT, VN, S, P ) whose parsing table has no multiple entries is
said to be LL(1). In the name LL(1)
• The first L stands for scanning the input from left to right,
• The 1 stands for using one input symbol of look-a-head at each step to make
parsing action decision.
• Not left-recursive.
LL Parsing Algorithm:
We may stick to deterministic LL(1) for parser explanation, as the size of table grows
exponentially with the value of k. Secondly, if a given grammar is not LL(1), then usually, it
is not LL(k), for any given k.
Method: Initially, the parser is in a configuration with w$ in the input buffer and the
start symbol S of G on top of the stack, above $.
13
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
set ip to point to the first symbol of w;
if (X is a terminal) error();
else if ( M[X, a] is an error entry ) error();
the stack;
• if β → t, then α does not derive any string beginning with a terminal in FOLLOW(A).
S → iEtS | iEtSeS | a E → b
S’→ eS | ε
E→b
14
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
To construct a parsing table, we need FIRST() and FOLLOW() for all the non-terminals.
FIRST(S) = {i, a}
FIRST(S’) = {e, ε}
FIRST(E) = { b}
FOLLOW(S) = {$, e}
FOLLOW(S’) = {$, e}
FOLLOW(E) = {t}
Parse Table:
Syntax analyzers receive their inputs, in the form of tokens, from lexical analyzers. Lexical
analyzers are responsible for the validity of a token supplied by the syntax analyzer. Syntax
analyzers have the following drawbacks -
These tasks are accomplished by the semantic analyzer, which we shall study in Semantic
Analysis.
Syntax analyzers follow production rules defined by means of context-free grammar. The way
the production rules are implemented (derivation) divides parsing into two types: top-down
parsing and bottom-up parsing.
BOTTOM-UP PARSING:
• Bottom-up parsing corresponds to the construction a parse tree for the given input
string beginning at the leaves (the bottom) and working up towards the root (the
top).
• Bottom up parsing is also known as shift-reduce parsing. It is the process of
15
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
“reducing” a string w to the start symbol of the grammar.
• At each reduction step, a particular substring matching the right side of a
production is replaced by the symbol on the left of that production.
• The key decisions during bottom-up parsing are about when to reduce and what
production to apply, as the parse proceeds.
• If the substring is chosen correctly at each step, a rightmost derivation is traced
out in reverse.
abbcde
aAbcde (A->b)
aAde (A->Abc)
aABe (B->d)
S (S->aABe)
• By a sequence of four reductions we are able to reduce abbcde to S
• These reductions trace out the following right most derivation in
• reverse: s aABe aAde aAbcd bcde
HANDLE:
• A handle of a string is a substring that matches the right side of a production and
whose reduction to the non-terminal on the left side of the production represents
one step along the reverse of a right most derivation.
• If S αAw αβw, then A→β in the position following α is a handle of αβw. The
string w to the right of the handle contains only terminal symbols.
• If a grammar is unambiguous, then every right sentential form of the grammar has
exactly one handle.
HANDLE PRUNING:
• A rightmost derivation in reverse can be obtained by “handle pruning”. That is,
we start with a string of terminals w that we wish to parse.
• If w is a sentence of the grammar at hand, then let w = γn, where γn is the nth
right- sentential form of some as yet unknown rightmost derivation.
S=γ0 γ1 γ2 …. γn-1 γn =w
• To reconstruct this derivation in reverse order, we locate the handle βn in γn and
16
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
replace βn by the left side of some production An→βn to obtain the (n-1)th right
sentential form γn-1.
• If by continuing this process we produce a right-sentential form consisting only of
the start symbol S, then we halt and announce successful completion of parsing.
Example:
• Parser takes the string w and reads symbol by symbol from left to right.
Whatever it reads it should remember till a handle is recognized.
• So it reads symbol by symbol and pushes it on to the stack until a
handle is detected.
• This process of reading the symbol and pushing onto the stack is called
shift action.
• Once a handle is detected, the parser reduces the handle by non-terminal of
the production. This is called reduce action.
$S $
• Upon, entering this configuration, the parser halts and announces successful
completion of parsing.
• The primary operations of the parser are shift and reduce, there are actually
four possible actions a shift-reduce parser can make
1. Shift: In shift action, the next input symbol is shifted onto the top of the
stack
2. Reduce: In Reduce action, the parser knows the right end of the handle is at
the top of the stack. It must then locate the left end of the handle within the
stack and decide with what non-terminal to replace the handle.
3. Accept: In an Accept action, the parser announces successful completion of
parsing
4. Error: In an error, the parser discovers that a syntax error has occurred and
calls an error recovery routine.
• The parser repeats this cycle until it has detected an error or until the stack
contains the start symbol and the input is empty.
Stack Input
$S $
• After entering this configuration, the parser halts and announces successful
completion of parsing.
Example 1:
S->aABe
A->Abc|b
B->d
18
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Stack Input Action
$ abbcde$ shift
$a bbcde$ shift
$ab bcde$ Reduce by A->b
$aA bcde$ shift
$aAb cde$ shift
$aAbc de$ Reduce by A->Abc
$aA de$ shift
$aAd e$ Reduce by B->d
$aAB e$ shift
$aABe $ Reduce by S->aABe
$S $ Accept
The parser announces successful completion of parsing.
Example 2:
Consider the rammar
E->E+E
E->E*E
E->(E)
E->id
19
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
cannot decide whether to shift or to reduce (a shift/reduce conflict), or cannot
decide which of several reductions to make (a reduce/reduce conflict).
LR PARSING:
LR GRAMMAR:
• A grammar for which we can construct LR parser is called LR grammar
• A grammar that can be parsed by a parser after examining k input symbols
on each move is called LR(k) grammar.
• LR parsers can describe more languages than LL parsers
LR PARSERS:
• An efficient, bottom-up syntax analysis technique that can be used to parse a
large class of context-free grammars called LR(k) parsing.
• The L is for left-to-right scanning of input, the R is for constructing a right most
derivation in reverse and k for the number of input symbols of lookahead
that are used in making parsing decisions.
• When k is omitted, k is assumed to be 1.
• LR parsing is attractive for a variety of reasons
LR parsers can be constructed to recognize virtually all programming-
language constructs for which context free grammars can be written.
The LR parsing method is the most general non backtracking shift-reduce
parsing method known, yet it can be implemented as efficiently as other
shift-reduce methods.
The class of grammars that can be parsed using LR methods is a proper
subset of the class of grammars that can be parsed with predictive parsers.
20
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
MODEL OF AN LR PARSER:
• The schematic form of an LR parser consists of an input, an output, a
stack, a driver program and a parsing table that has two parts (action and
goto).
• The driver program is the same for all LR parsers, only the parsing table
changes form one parser to another.
• The parsing program reads characters from an input buffer one at a time.
• The program uses a stack to store a string of the form S0X1S1X2…XmSm,
where Sm is on the top.
• Each Xi is a grammar a 1 ….. a i …. an $ symbol and each Si is a
symbol called a state.
input
stack
Sm LR Parsing program
Xm
output
S m-
1
.
.
S0 action goto
LR PARSING ALGORITHM:
Input: An input string w and an LR-parsing table with functions ACTION and
GOTO for a grammar G
Method: Initially, the parser has S0 on its stack, where S0 is the initial state, and
w$ in the input buffer. The parser then executes the program until accept or
error action is encountered.
Algorithm:
22
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
• pop 2*|β| symbols off the stack;
A->.XYZ
A->X.YZ
A->XY.Z
A->XYZ.
Closure of item sets: If I is a set of items for a grammar G, then closure(I) is the
23
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
set of items constructed from I by the two rules.
1. Initially add every item in I to closure(I).
2. If A->α.Bβ is in closure(I) and B->γ is a production, then add the item B->.γ to
closure(I), if it is not already there. Apply this rule until no more new items can be
added to closure(I).
GOTO: Goto(I,X), where I is a set of items and X is a grammar symbol.
1. Goto(I,X) is defined to be the closure of the set of all items A->αX.β such that
[A->α.Xβ] is in I.
2. Goto(I,X) specifies the transition from the state I under input X.
2. SLR Parsing Table:
The steps to be followed in construction of SLR parse table is given below:
1. Construct C={I0 , I1 , . , In } the collection of sets of LR(0) items from G'.
2. Initial state of the parser is constructed from the set of items from the
set of items for [S' ->.S].
3. State i is constructed from Ii. The parsing actions of the state i are
determined as follows:
b. if [A->α.] is in Ii, then ACTION[i,x] is set to 'reduce A-> α ', for all 'x'
in FOLLOW(A).(A must not be S')
E’ → E
E → E +T | T
24
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
T → T +F | F
F → (E)| id
Contain the
Item. E’→ .E
E→. E + T
E → .T
T→.T
*F T→ . F
F → .(E)
F → .id
25
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
26
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Even more powerful than SLR(l) is the LR(l) parsing method. LR(l) includes LR(O)
items and a look ahead token in itemsets.
An LR(l) item consists of,
Grammar production rule.
Right-hand position represented by the dot and.
Lookahead token.
A --->X1 · · · Xi • Xi+1 · · · Xn, l where l is a lookahead token
The • represents how much of the right-hand side has been seen,
X1 · · · Xi appear on top of the stack.
Xi+l · · · Xn are expected to appear on input buffer.
The lookahead token l is expected after X1 · · · Xn appears on stack.
An LR(l) state is a set of LR(l) items.
Construction of the CLR parser includes the following steps:
1. Collection of LR(1) item
2. CLR Parsing table
3. Check whether the string 'w' can be parsed by CLR or not
Closure of Item set: If I is a set of items for a grammar G, then closure(I) is the set of
items constructed from I by the two rules.
27
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
1. Initially find Closure(S'->.S,$)
2. Consider each set of item I in C and each grammar symbol X, Goto(I,X) is added
to C, when it is not empty and is not already in C. Rule 2 is repeated until no
more items can be added to C.
The steps to be followed in construction of CLR parse table are given below:
1. Construct C={I0,I1, .... ,In} the collection of set of LR(1) items for G’
2. Initial state of the parser is constructed from the set of items for [S'->.S,$].
3. State i is constructed from Ii. The parsing actions of the state is are determined
as follows:
S->CC
C->CC/d
S’->S
S->CC
C->cC/d
28
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
29
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
CONSTRUCTION OF LALR PARSER:
LALR stands for (looka head LR) parser. LALR parser starts wi th the idea of building
an LR parsing table. Tables generated by LALR parser are smaller in size as
compared to that of Canonical LR(CLR) and Simple LR(SLR) techniques. LALR
parsers are slightly less powerful than L Rparsers, but still more powerful than SLR
parsers.
Construction of LALR parser includes the following steps:
1. Collection of LR(1) item
2. LALR Parsing table
3. Check whether the string 'w' can be parsed by LALR or not
The construction of LR(1) items in LALR is similar to CLR. In LALR we merge the
states which are having same items with different lookahead symbols.
Closure of Item set: If I is a set of items for a grammar G, then closure(I) is the set of
items constructed from I by the two rules.
30
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Input: An augmented grammar G’
Output: The LALR parsing –table functions ACTION and GOTO for G’
Method:
3. Let C’= {J0,J1, J2,…… Jm } be the resulting sets of LR(1) items. The parsing actions for state
i are constructed from Ji. If there is a parsing action conflict, the algorithm failsto
produce a parser, and the grammar is said not be LALR(1).
4. The GOTO table is constructed as follows. IF J is the union of one or more sets of
LR(1) items, that is, J= I1 ∩ I2 ∩ I3…..∩ Ik, then the cores of GOTO(I1, X), GOTO(I2,
X)….GOTO(Ik, X) are the same, since I1, I2, I3…..,Ik all have the same core. Let K be the
union of all sets of items having the same core as GOTO(I1, X). then GOTO(J,X) = K.
S→CC
C→cC|d
S’->S
S->CC
C->cC/d
31
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
ACTION GOTO
START c d $ S C
0 S36 S47 1 2
1 Acc
2 S36 S47 5
36 S36 S47 89
47 R3 R3 R3
5 R1
89 R2 R2 R2
They depend on the core of the item but we merge only those rows whichhave
common cores.
The only way by which this conflict can arise in LALR is when the
conflict is already there in the LR(1).
32
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
PCD-UNIT-II
ASSIGNMENT-CUM-TUTORIAL QUESTIONS
Section-A-Objective Questions
1. is a hierarchical structure which represents the derivation of the grammar
to yield input strings.
2. The following grammar is [ ]
A->AaB | a
B-> aB |a
c. right-recursive d. an operator-grammar
33
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
8. The advantage of eliminating left recursion is [ ]
a. Avoids parser to go into an infinite loop
b. Avoids Backtracking
c. Both (a) and (b)
d. None of these
In the predictive parser table M of the grammar, the entries M[S, id] and M[R, $]
respectively.
34
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
13. If a state does not know whether it will make a shift operation or reduction for a
terminal is called [ ]
a. Shift/reduce conflict
b. Reduce /shift conflict
c. Shift conflict
d. Reduce conflict
14. Which of the following grammar rules violate the requirements of an operator
grammar? P, Q, R are non terminals, and r,s,t are terminals [ ]
a. P -> QR b) P -> QsR
c) P -> ε d) P -> QtRr
15. The default value of 'K' in LR(K) is [ ]
a)0 b)1 c)2 d)None of the above
16. Which of the following is the most powerful parsing method? [ ]
a. SLR b) LALR c) CLR d) LL(1)
17. Consider the grammar [ ]
S → (S) | a
Let the number of states in SLR(1), LR(1) and LALR(1) parsers for the grammar be n1,
n2 and n3 respectively. The following relationship holds good
a) n1 < n2 < n3 b) n1 = n3 < n2
c) n1 = n2 = n3 d) n1 ≥ n3 ≥ n2
18. of a string is a substring that matches the RHS of a production and whose reduction
to the non-terminal represents one step along the reverse of a rightmost derivation
toward reducing to the start symbol.
19. Among simple LR (SLR), canonical LR, and look – ahead LR (LALR), which of the
following pairs identify the method that is very easy to implement and the method that
is the most powerful, in that order? []
a. SLR, LALR b) Canonical LR, LALR
c) SLR, canonical LR d) LALR, canonical LR
20. Consider SLR(1) and LALR(1) tables for CFG.
35
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Which of the following is false? [ ]
a. Goto of both tables may be different
b. Shift entries are identical in both tables
c. Reduce entries in tables may be different
d. Error entries may be different
21. For a grammar G, Shift reduce (S-R) conflicts is present in LALR (1) parser, if
and only if [ ]
a. The LR (1) parser for G has S-R conflicts
b. The LR (0) parser for G has S-R conflicts
c. The SLR (1) parser for G has S-R conflicts
d. The SLR (0) parser for G has S-R conflicts
SECTION-B
II) Descriptive questions
S->iCtSS’ | a
S’->eS|ε
C->b
7. Construct predictive parsing table for the
grammar
S→AaAb|BaBb,
A→ ε
B→ ε
8. Construct the predictive parser for the following grammar
S→ (L) | a
L→ L,S | S
Check whether the string (a, a) is accepted by it or not
9. What is Bottom-up parsing? Define Handle. Explain handle pruning with
an example.
10.Explain Shift-Reduce parser. Construct Shift-Reduce parser for the following
grammar S->aABb A->c|aB B-> d|bA
with the input string "aabcdb".
19.Analyze the reasons why the following grammar is LR(1) but not
LALR(1). S->Aa|bAc|Bc|bBa
B->d
A->d
38
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
GATE CS 2012 [ ]
3. Consider the date same as above question. The appropriate entries for E1, E2, and E3
are
GATE CS 2012 [ ]
a. A b. B c. C d. D
4. A grammar G is LL(1) if and only if the following conditions hold for [ ]
two distinct productions NET CS 2014
A->α | β
I. First (α) ∩ First (β) ≠ {a} where a is some terminal symbol of the grammar.
II. First (α) ∩ First (β) ≠ ε
III. First (α) ∩ Follow (A) = ∅ if ε ∈ First (β)
(A) I and II (B) I and III (C) II and III (D) None
39
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
a) only S1
b) only S2
c) Both S1 and S2
d) Neither S1 nor S2
7. A canonical set of items is given below
S → L.> R, Q →R. [ ]
On input symbol < the set has (GATE CS 2014)
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.
8. An LALR(1) parser for a grammar G can have shift-reduce (S-R) conflicts if and only if
(GATE CS 2015) [ ]
a) the SLR(1) parser for G has S-R conflicts
b) the LR(1) parser for G has S-R conflicts
c) the LR(0) parser for G has S-R conflicts
d) the LALR(1) parser for G has reduce-reduce conflicts
40