0% found this document useful (0 votes)
16 views40 pages

2023 24 PCD UNIT 2 Modified

Uploaded by

chaithubrazil
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)
16 views40 pages

2023 24 PCD UNIT 2 Modified

Uploaded by

chaithubrazil
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/ 40

SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN

SESHADRI RAO GUDLAVALLERU


ENGINEERING COLLEGE

(An Autonomous Institute with Permanent Affiliation to JNTUK, Kakinada)


SeshadriRao Knowledge Village, Gudlavalleru – 521 356

Department of Information Technology

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

Bottom-up parsing: Shift-Reduce parsing, LR parsers: Construction of SLR, CLR(1), LALR


parsers.

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

phase uses context-free grammar (CFG), which is recognized by push-down automata.


• CFG, on the other hand, is a superset of Regular Grammar.

Role of the Parser:


• It verifies the structure generated by the tokens based on the grammar.

• It constructs the parse tree.

• It reports the errors.

• It performs error recovery.

2
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
Type of errors that a parser cannot detect:

1. Variable re-declaration

2. Variable initialization before use.

3. Data type mismatch for an operation


Parse Tree:

If w is a string in the context free grammar G. w belongs to L(G).Then derivation of w


is represented a tree is called derivation tree or parse tree. A parse tree can be seen as a
graphical representation of a derivation.

In the derivation tree we have:

1. The starting symbol S is the root.

2. All the internal nodes in the parse tree are non-terminal symbols.

3. All the leaf nodes are 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

Ambiguity in context free grammars:


A context-free grammar G is said to be ambiguous if it has more than one parse tree (left or
right derivation) for at least one.

(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:

• Deciding the non-terminal which is to be replaced.

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

The left-most derivation :

E→E*E

E→E+E*E

E → id + E * E

E → id + id * E

E → id + id * id

Notice that the left-most side non-terminal is always processed first.

4
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN

The right-most derivation is:

E→E+E

E→E+E*E

E → E + E * id

E → E + id * id

E → id + id * id

Example:

G = ({S}, {a, b, +, *}, P. S), where P consists of S S+S | S*S | a | b

We have two derivation trees for a + a*b

Two derivation trees for a + a * b

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)

These methods decrease the chances of ambiguity in a language or its grammar.

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

It will be evaluated as:

(id op id) op id

For example, (id + id) + id

Operations like Exponentiation are right associative, i.e., the order of evaluation in the same
expression will be:

id op (id op id)

For example, id ^ (id ^ id)

Left Recursion

A grammar G is said to be left recursive if and only if it is of the form

Example: S→S+S

S→Sa

Elimination of Left Recursion:

Consider the grammar G is in the form A→


_ _βm does not start with A.

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:

If a top-down parser encounters a production like A ⟹αβ|αδ

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:

The above productions can be written as

A→

A'→β | δ | …

Now the parser has only one production per prefix which makes it easier to take decisions.

CLASSIFICATION OF PARSERS

1. Top down parsing

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:

1. Recursive descent parsing

2. Predictive parsing

1. Recursive Descent Parsing:


• Recursive descent parsing is one of the top-down parsing techniques that uses a set

of recursive procedures to scan its input.


• This parsing method may involve backtracking, that is, making repeated scans of
the input.
Example for backtracking:
Consider the grammar G: S → cAd
A → ab | a
and the input string w=cad.

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:

Now try the second alternative for A.

Now we can halt and announce the successful completion of parsing.

Example for recursive decent parsing:

A left-recursive grammar can cause a recursive-descent parser to go into an infinite loop.


Hence, elimination of left-recursion must be done before parsing.

Consider the grammar for arithmetic expressions

E → E+T | T

T → T*F | F

F → (E) | id

After eliminating the left-recursion the grammar becomes,

E → TE’

E’ → +TE’ | ε

T → FT’

T’ → *FT’ | ε F → (E) | id

First and Follow:

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.

1. If X is terminal, then FIRST(X) is {X}.

2. If X→ε is a production, then add ε to FIRST(X).

3. If X is non-terminal and X→Y1Y2...Yk is a production, then place a in FIRST(X) if for some


i, a is in FIRST(Yi) and ε is in all of FIRST(Y1),...,FIRST(Yi-1) that is, Y1. .... Yi-1 ε.

If ε is in FIRST(Yj) for all j=1,2, ... ,k, then add ε to FIRST(X).

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.

2. If there is a production A→αBβ where FIRST(β) except ϵ is placed in FOLLOW(B).

3. If there is a production A→αB or a production A→αBβ where FIRST(β) contains ϵ,


then everything in FOLLOW(A) is in FOLLOW(B).

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

each case, since FIRST(id)=(id) and FIRST('(')= { ( } by rule 1.

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.

By rule 2 applied to production F→ (E), right parenthesis is also in FOLLOW(E).

By rule 3 applied to production E→ TE', $ and right parenthesis are in FOLLOW(E').

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

a non-terminal in case of alternatives.

Following are the steps for constructing predictive parsing:


• Eliminating Left Recursion
• Left factoring of a grammar
• First and Follow
• Constructing a Parse Table
• Parsing the string
Algorithm for the Construction of Predictive Parsing Table:
The construction of predictive parsing table is an important activity in predictive parsing
method. This algorithm depends on FIRST and FOLLOW functions.

Input: The Context Free Grammar G.

Output: Predictive Parsing table M.

Algorithm: For the

1.

2. Where b is the symbols from FOLLOW(A).


3.

4. All the remaining entries in the table M are marked as Syntax Error.

Consider the grammar:

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}.

In addition, FIRST[E]=FIRST[T]={num, id}.

Similarly, FIRST[T'] is {*,/} and FIRST[E'] is {+,-}.

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]={+,-,$}.

Similarly, FOLLOW[T']=FOLLOW[T]={+,-,$} from rule 5. The FOLLOW[F] is equal to


FIRST[T']={*,/}plus FOLLOW[T'] and plus FOLLOW[T] from rules 5, 6, and 7, since T' can be
reduced to the empty sequence.

To summarize, we have:

FIRST FOLLOW

E {num,id} {$}

E' {+,-} {$}

T {num,id} {+,-,$}

T' {*,/} {+,-,$}

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.

For example, the parsing table of the grammar G1 is:

LL (1) GRAMMAR

LL (1) GRAMMARS AND LANGUAGES:

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 second L stands for producing a leftmost derivation,

• The 1 stands for using one input symbol of look-a-head at each step to make
parsing action decision.

A language is said to be LL(1) if it can be generated by a LL(1) grammar . It can be shown


that LL (1) grammars are

• Not ambiguous and

• 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.

Given below is an algorithm for LL(1) Parsing:

Algorithm: Table-Driven Predictive Parsing

Input: A String w and a Parsing Table M for Grammar G.

Output: If w is in L(G), a leftmost derivation of w; otherwise, an error indication.

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;

set X to the top of the stack;

while (X≠$) { /*stack is not empty*/

if (X is a) pop the stack and advance ip; else

if (X is a terminal) error();
else if ( M[X, a] is an error entry ) error();

else if ( M[X, a] = X→Y1Y2...Yk) {

output the production X→Y1Y2...Yk; pop

the stack;

push Yk,Yk-1. .. ,Y1 onto the stack, with Y1 on top;

set X to the top stack symbol;

A grammar G is LL(1) if A → α | β are two distinct productions of G:

• for no terminal, both α and β derive strings beginning with a.

• at most one of α and β can derive empty string.

• if β → t, then α does not derive any string beginning with a terminal in FOLLOW(A).

Consider this following grammar:

S → iEtS | iEtSeS | a E → b

After eliminating left factoring, we have S → iEtSS’ | a

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:

Limitations of Syntax Analyzers

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 -

• It cannot determine if a token is valid,

• It cannot determine if a token is declared before it is being used,

• It cannot determine if a token is initialized before it is being used,

• It cannot determine if an operation performed on a token type is valid or not.

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.

• For example, consider the grammar


s->aABe
A->Abc|b
B->d

The sentence abbcde can be reduced to S by the following steps

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:

BOTTOM-UP PARSING TECHNIQUES:


1. Shift Reduce Parsing
2. LR parsing-SLR, CLR, LALR
3. Operator Precedence Parsing
SHIFT-REDUCE PARSING:
• Shift-Reduce parsing is a form of bottom-up parsing in which a stack holds
grammar symbols and an input buffer holds the rest of the string to be
parsed.

• 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.

STACK IMPLEMENTATION OF SHIFT-REDUCE PARSING:


• A way to implement a shift-reduce parser is to use a stack to hold
grammar symbols and an input buffer to hold the string w to be parsed.
• The handle always appears at the top of the stack just before it is identified
as the handle.
• $ is used to mark the bottom of the stack and also right end of the input.
• Initially, the stack is empty, and the string w is on the input as
follows: Stack Input
17
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
$ w$
• During a left-to-right scan of the input string, the parser shifts zero or more
input symbols onto the stack, until it is ready to reduce a string β of grammar
symbols on top of the stack.
• It then reduces β to the left hand side of the appropriate production.
• 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 $

• 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

Implement a shift-reduce parser on the input “abbcde”

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

Implement a shift reduce parser on the input id+id*id

Stack Input Action


$ id+id*id$ shift
$id +id*id$ Reduce by E->id
$E +id*id$ shift
$E+ id*id$ shift
$E+ id *id$ Reduce by E->id
$E+E *id$ shift
$E+E* id$ shift
$E+E*id $ Reduce by E->id
$E+E*E $ Reduce by E->E*E
$E+E $ Reduce by E->E+E
$E $ Accept

DRAWBACKS OF SHIFT-REDUCE PARSING:


• There are context-free grammars for which shift-reduce parsing cannot be used
• Every shift-reduce parser for such a grammar can reach a configuration in
which the parser, knowing the entire stack contents and the next input symbol,

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.

 An LR parser can detect a syntactic error as soon as it is possible to do so


on a left to right scan of the input.
• The principal drawback of the method is that it is too much work to construct
on LR parsers by hand for a typical programming language grammar.
• The three techniques for constructing an LR parsing table for a grammar:
1. Simple LR(SLR) is the easiest to implement, but the least powerful of the
three. It may fail to produce a parsing table for certain grammars on which
the other methods succeed.
2. Canonical LR (CLR) is the most powerful, and the most expensive.
3. Lookahead LR (LALR) is intermediate in power and cost between the other
two. The LALR will work on most programming language grammars and
with some effort, can be implemented efficiently. For a comparison of
parser size, the SLR & LALR tables for a grammar always have the same
number of
4. states. Thus it is much easier to construct SLR & LALR tables than the CLR
tables.

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

• Each state symbol summarizes the information contained in the stack


below it, and the combination of the state symbol on top of the stack
and the current
input symbol are used to index the parsing table and determine the shift-reduce
parsing decision.
STRUCTURE OF THE LR PARSING TABLE:
• The parsing table consists of two parts, a parsing ACTION function action
and a goto function GOTO.

• The Action function takes asarguements a state i and a terminal a. The


value of ACTION[i,a] can have one of the forms:
a) SHIFT j, where j is a state. The action taken by the parser effectively
shifts input a to the stack, but uses state j to represent a.
b) Reduce A→β. The action of the parser effectively reduces β on the
top of the stack to head A.
c) Accept. The parser accepsts the input and finishes parsing.
d) Error. The parser discovers an error in its input and takes some
21
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
correction action.
• We extend the GOTO function, defined onsets of items, to states:
if GOTO[Ii ,A] = Ij, then GOTO also maps a state i and a nonterminal A to
state j.
• The program deriving the LR parser behaves as follows:
 It determines Sm , the state currently on the top of the stack, and a i , the
current input symbol. It then consults action[S m ,ai] the parsing action table
entry for state Sm and input ai , which has one of four values:
1. Shift S, where S is a state
2. Reduce by a grammar production A->β
3. Accept and
4. Error
 The function goto takes a state and grammar symbol as arguments and
produces a state.
 The GOTO function of a parsing table constructed from a grammar G using
SLR, CLR or LALR method is the transition function of a deterministic finite
automata that recognizes the viable prefixes of G.
 Viable Prefix: The set of prefixes of right sentential forms that can appear
on the stack of a shift-reduce parser are called viable prefixes.

LR PARSING ALGORITHM:

Input: An input string w and an LR-parsing table with functions ACTION and
GOTO for a grammar G

Output: If w is in L(G), a bottom-up parse for w, otherwise an error indication.

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:

Let a be the the first symbol of w$;


• repeat forever begin
• let s be the state on top of the stack and a the symbol pointed to by ip; if

action[S,a]=shift S1 then begin

• push a then S1 on top of the stack; advance ip to the


next input symbol
• end
• else if action[S,a]=reduce A->β then begin

22
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
• pop 2*|β| symbols off the stack;

• let S1 be the state now on top of the stack;

• push A then goto[S1 ,A] on top of the stack;


• output the production A->β end
• else if action[S,a]=accept then
• return
else
error()
end

CONSTRUCTION OF SLR PARSER:


A grammar for which an SLR parser can be constructed is said to be an SLR
grammar

Steps to be followed in the construction of SLR parser:

1. Canonical collection of LR(0) item


2. SLR Parsing table
3. Check whether the string 'w' can be parsed by SLR or not

1. Canonical Collection of LR(0) Item:

LR(0) item: An LR(0) item of a grammar G is a production of G with a dot at


some position of the right side of the body.

Thus production A->XYZ yields the four items

A->.XYZ
A->X.YZ

A->XY.Z

A->XYZ.

To construct the canonical LR(0) collection for a grammar, we define an augmented


grammar and two functions CLOSURE and GOTO.

Augmented Grammar: If G is a grammar with start symbol S, then G' , the


augmented grammar for G with a new start symbol S' and new production S'->S.

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:

a. if [A->α.xβ] is in Ii and goto (Ii,x)=Ij , then ACTION[i,x] is set to 'shift


j'. Here x must be a terminal.

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')

c. if [S' ->S.] is in Ii, then Action[i,$] is set to 'accept'.


4. GOTO transitions are constructed for all non-terminals. If Goto(Ii,
A)=Ij, then Goto(i,A) of the parse table is set to j.
5. All other entries are error entries.
3. Check whether the string 'w' can be parsed by SLR or not
(Refer LR parsing algorithm )
Example: Construct SLR parser for the following grammar
E→ E + T
E→T
T→T*F
T→F
F → (E)
F → id
Step 1: Consider augmented grammar

E’ → E

E → E +T | T

24
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
T → T +F | F

F → (E)| id

Step 2: Construct canonical LR(0) items

If I is the set of one item {[E’ →.E]} then closure(I’)

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

Step 3: construct SLR parsing table for the grammar


Step 4: Moves of SLR parser on the input id * id+id

Stack Input Action


O id*id+id$ Shift
Oid5 *id+id$ Reduce by F->id
OF3 *id+id$ Reduce by T->F
OT2 *id+id$ Shift
OT2*7 id+id$ Shift
OT2*7id5 +id$ Reduce by F->id
OT2*7F10 +id$ Reduce by T->T*F
OT2 +id$ Reduce by E->T
OE1 +id$ Shift
OE1+6 id$ Shift
OE1+6id5 $ Reduce by F->id
OE1+6F3 $ Reduce by T->F
OE1+6T9 $ Reduce by E->E+T
OE1 $ Accept

26
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN

CONSTRUCTION OF CLR PARSER:

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

1. Collection of LR(1) item:

LR(1) item: A LR(1) item is A->α.β,a where a is the lookahead symbol.


Augmented Grammar: If G is a grammar with start symbol S, then G', the
augmented grammar for G with a new start symbol S' and new production S'->S.

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.

1. Every item in I is added to Closure(I).

2. If an LR(1) item [X->A.BC,a] exists in I and there exists a production,

B->b1,b2,..bn, then add item [B->.b1b2...bn,z], where z is a terminal in


First(Ca), if it is not already in Closure(I). Keep applying this rule until no
more elements can be added to I.

Goto: Goto(I,X) for an item I:[A->α.Xβ,a] where X is a grammar symbol defined


as the closure of the set of all items [A->αX.β,a]

LR(1) canonical collection of item:

The rules to compute LR(1) collection of items are given below:

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.

2. CLR Parsing table

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:

a. if [A->α.aβ,b] is in Iiand Goto(Ii,a)=Ij, then Action[i,a] is set to 'shift j'.


Here 'a' must be a terminal.
b. if [A->α.,a] is in Ii, then Action[i,a] is set to 'reduce A->α', if the next
input is 'a' ie lookahead is 'a'. ('A' must not be S).

c. if [S'->S.,$] is in Ii, then Action[i,$] is set to 'accept'.

4. Goto transitions are constructed for all non-terminals. If Goto(Ii,A)=Ij, then


Goto(i,A) of the parse table is set to j.
1. All other entries are error entries.

3. Check whether the string 'w' can be parsed by CLR or not:

Refer LR parsing algorithm.

Example: Construct CLR parser for the following grammar

S->CC

C->CC/d

Step 1: The augmented grammar is

S’->S

S->CC

C->cC/d

Step 2: Construct sets of LR(1) items for the given grammar

28
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN

Step 3: Construct canonical parsing table.

Step 4: Moves of CLR parser for the input cdd

Stack Input Action


0 cdd$ Shift
0c3 dd$ Shift
0c3d4 d$ Reduce by c->d
0c3C8 d$ Reduce by c->cC
0c2 d$ Shift
0c2d7 d$ Reduce by c->d
0C2C5 $ Reduce by S →CC
0S1 $ Accept

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

1. Collection of LR(1) item:

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.

LR(1) item: A LR(1) item is A->α.β,a where a is the lookahead symbol.

Augmented Grammar: If G is a grammar with start symbol S, then G', the


augmented grammar for G with a new start symbol S' and new production S'->S.

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.

1. Every item in I is added to Closure(I).

2. If an LR(1) item [X->A.BC,a] exists in I and there exists a production, B-

>b1,b2,..bn, then add item [B->.b1b2...bn,z], where z is a terminal in First(Ca), if it


is not already in Closure(I). Keep applying this rule until no more elements can
be added to I.

Goto: Goto(I,X) for an item I:[A->α.Xβ,a] where X is a grammar symbol defined


as the closure of the set of all items [A->αX.β,a]

LR(1) canonical collection of item:

The rules to compute LR(1) collection of items are given below:

3. Initially find Closure(S'->.S,$)


4. 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.

2. LALR Parsing table:

Algorithm: An easy, but space-consuming LALR table construction

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:

1. Construct C= { I0,I1, I2,…… In }, the collection of sets of LR(1) items.


2. For each core present among the set of LR(1) items, find all sets having that core and
replace these sets by their union.

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.

3. Check whether the string 'w' can be parsed by LALR or not:


Refer LR parsing algorithm.

Now consider the grammar

S→CC
C→cC|d

The augmented grammar is

S’->S

S->CC

C->cC/d

• Take a pair of similar looking states such as I4 & I7


• Each of these states has only items with first component c → d.
• In I4, the lookahead’s are c or d; in I7 , $ is the only look ahead.
• Let us now replace I4 & I7 by I47, the union of I4 & I7 consisting of the set of three
items represented by
I47: C → d., c/d/$
• Similarly I3 and I6 form another pair.
I36: C→ c.C, c/d/$
C→ .cC, c/d/$
C→ .d, c/d/$
• I8 and I9 are replaced by their union
I89: C → cC., c/d/$
The LALR action and goto functions for the condensed sets of items are shown in below.

31
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN

LALR Parsing Table:

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

Stack Input Action


0 cdd$ Shift
0c36 dd$ Shift
0c36d47 d$ Reduce by C->d
0c36C36 d$ Reduce by C->cC
0c2 d$ Shift
0c2d47 $ Reduce by C->d
0C2C5 $ Reduce by S →CC
0S1 $ Accept

Confli cts in LALR Pa r ser:

 LALR Parser cannot introduce shift/reduce conflicts.


 Such conflicts arises when the look ahead is same as the token on
whichwe can shift.

 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

a. Unambiguous b. Left recursive c. Right recursive d. Ambiguous

3. A->A α | β is left recursive then its equivalent production are [ ]


a. A-> β R, R-> α R | ε b. A-> α R, R-> β R | ε

c. A-> α R |ε, R->β R | β d. None of these

4. The grammar A → AA | (A) | ε is not suitable for predictive-parsing because the


grammar is
[ ]
a. ambiguous b. left-recursive

c. right-recursive d. an operator-grammar

5. Is every LL (1) grammar is unambiguous grammar? [True | False]


6. In LL(1), First L indicates [ ]
a. Left to Right scanning of input b) Left Most Derivation
c) Left recursion d) None of these
7. In LL(1),second L indicates [ ]
a. Left to Right scanning of input b. Left Most Derivation

c. Left recursion d. None of these

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

9. Consider the following grammar: [ ]


S → FR, R → S | ε, F → id

In the predictive parser table M of the grammar, the entries M[S, id] and M[R, $]
respectively.

a. {S → FR} and {R → ε } b. S → FR} and { }

c. {S → FR} and {R → *S} d.{F → id} and {R → ε}

10.Consider the grammar with non-terminals N = {S,C,S1}, terminals T={a, b, i, t, e},with


S as the start symbol, and the following set of rules S → iCtSS1|a, S1→eS|ε, C→ b
[ ]
The grammar is NOT LL(1) because:

a. it is left recursive b. it is right recursive

c. it is ambiguous d. it is not context-free


11. Which of the following derivations does a bottom-up parser use while parsing an
input string? The input is assumed to be scanned in left to right order [ ]
a) Leftmost derivation b) Leftmost derivation traced out in reverse
c) Rightmost derivation d) Rightmost derivation traced out in reverse
12. Shift reduce parsing belongs to a class of [ ]
a. bottom up parsing b) top down parsing
c) recursive parsing d) predictive parsing

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

22. What is the similarity between LR, LALR and SLR? [ ]


a. Use same algorithm, but different parsing table.
b. Same parsing table, but different algorithm.
c. Both Parsing tables and algorithm are different.
d. Their Parsing tables & algorithm are similar but uses top down approach.

SECTION-B
II) Descriptive questions

1. Define ambiguous grammar. Consider the context free


grammar: S→SS+ | SS*|a and the string aa+a*
Is the grammar ambiguous or unambiguous? Justify your answer.
2. a) Explain the role of syntax analysis phase.
b) Classify the types of parsers.
c) Define LMD, RMD and derivation tree.
3. What is LL(1) grammar? Explain with an example.
4. Eliminate left recursion from the following grammars
a) i) S->AA | 0 ii) A->SS | 1
b) i) A → ABd /Aa /a ii) B → Be / b
c) S → (L) / a ii) L → L , S / S
36
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
5. Eliminate left factoring from the following grammars
a) S → a / ab / abc / abcd b) (i) S->bAd | bAe |ed (ii) A-> e | bA
c) A → aAB / aBc / aAc d) S → aSSbS / aSaSb / abb / b
6. a) List out the rules for FIRST and FOLLOW.
b) Compute FIRST and FOLLOW for the grammar.

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".

11.Write the procedure to construct Canonical Collection of:


i) LR(0) item sets. ii) LR(1) item sets. iii) LALR(1) item sets.
12.Define LR grammar. Explain the model of LR parser.
13.Write LR Parsing Algorithm.
14.Write the procedure to construct following LR parse tables:
a) SLR b) CLR c) LALR
15.Construct SLR parse table for the following grammar E→E+T|T, T→TF|F,
F→F*|a|b. Indicate the moves made by the SLR parser to parse the string a+b.
37
SRGEC-R20-IT-III-II-PRINCIPLES OF COMPILER DESIGN
16.Construct canonical collection of LR(1) item sets in CLR parser for the following
grammar S→CC, C→aC, C→d.
17.Construct LALR parse table for the following
grammar S->L=R, S→R, L→*R, R→L
18.Identify whether the given grammar is LL (1) or SLR (1).
S->AaAb|BbBa
A->ε
B->ε

19.Analyze the reasons why the following grammar is LR(1) but not
LALR(1). S->Aa|bAc|Bc|bBa
B->d
A->d

Questions asked in Competitive exams


1. Consider the grammar defined by the following production rules, with two operators ∗
and +
GATE CS 2014 [ ]
S --> T * P , T --> U | T * U ,
P --> Q + P | Q
Q --> Id , U --> Id
Which one of the following is TRUE?
a. + is left associative, while ∗ is right associative
b. + is right associative, while ∗ is left associative
c. Both + and ∗ are right associative
d. Both + and ∗ are left associative
2. For the grammar below, a partial LL(1) parsing table is also presented along with the
grammar. Entries that need to be filled are indicated as E1, E2, and E3.ε ,is the empty
string,$ indicates end of input, and, | separates alternate right hand sides of
productions

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

5. Which of the following statements is false? (GATE CS 2001) [ ]


a) An unambiguous grammar has same leftmost and rightmost derivation
b) An LL(1) parser is a top-down parser
c) LALR is more powerful than SLR
d) An ambiguous grammar can never be LR(k) for any k
6. Consider the following grammar G [ ]
S → F | H, F→ p| c , H→ d | c
Where S, F, and H are non – terminal symbols, p, d, and c are terminal symbols.
Which of the following statements (s) is/are correct? (GATE CS 2015) S1: LL(1)
can parse all strings that are generated using grammar G
S2: LR(1) can parse all strings that are generated using grammar G

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

You might also like