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

LR 1 Notes

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

LR 1 Notes

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/ 13

21CS51

Automata Theory

and

Compiler Design

Lecture Notes for LR(1) and LALR

Parsers

Shridhar Venkatanarasimhan

Computer Science and Engineering Department

Raja Reddy Institute of Technology

Chikkabanavara, Bengaluru 560090.

February 20, 2024


CONTENTS

1. LR(0) Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.1 LR(0) and LR(1) . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.2 What is k in LR(k)? . . . . . . . . . . . . . . . . . . . . . . . 5

1.3 CLOSURE for an LR(1) set of items . . . . . . . . . . . . . . 6

1.4 Sample Grammar for LR(1) Parsing . . . . . . . . . . . . . . 6

1.5 The GOTO Function . . . . . . . . . . . . . . . . . . . . . . . 7

1.6 LR(1) Automaton . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.7 LR(1) Parsing Table . . . . . . . . . . . . . . . . . . . . . . . 7

1.8 Use of the LR(1) Parsing Table . . . . . . . . . . . . . . . . . 10

2. LALR Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
LIST OF FIGURES

1.1 LR(1) Automaton with Sets of Items (States) and the GOTO

Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.2 LR(1) Parsing Table for the Sample Grammar . . . . . . . . 9

2.1 Merged LALR states formed from LR(1) . . . . . . . . . . . . 12

2.2 LALR Parsing Table . . . . . . . . . . . . . . . . . . . . . . . 12


ABSTRACT

In these notes, we consider two more types of LR parsers, namely LR(1)

parser and LALR parser.

Disclaimer: Students are requested to read the prescribed text-

books and the reference books. There might be errors in these

notes. If you come across any mistake, please let me know.


1. LR(0) PARSERS

1.1 LR(0) and LR(1)

A parsing table for a shift-reduce parser such as an LR(0) parser can

have two types of conflicts:

1. shift-reduce conflict: This is a confusion between a shift action

and a reduce action.

2. reduce-reduce conflict: This is a a confusion between two different

reductions possible.

Point to ponder: why cannot an LR parsing table have a shift-shift

conflict?

LR(1) parser is strictly more powerful than LR(0) parser, that is, if

a grammar has an LR(0) parser without conflicts, it also has an LR(1)

parsing table without conflicts; however, the reverse is not true. We

have grammars with LR(1) parsing tables without conflicts, for which

LR(0) parsing table has conflicts. A grammar is said to be LR(0) (or in

LR(0)), if there is a LR(0) parsing table without conflict. A grammar is

said to be LR(1) (or in LR(1)), if there is a LR(1) parsing table without

conflict. LR(0) is a proper subset of LR(1).

1.2 What is k in LR(k)?

LR(k) stands for Left-to-right scan of input, Rightmost derivation in

reverse and k symbols of lookahead. When k is 0, we get LR(0) which


1. LR(0) Parsers 6

is also called simple LR or SLR or SLR(1). When k is 1, we get LR(1). If

k is omitted, it defaults to 1, so, LR(1) is assumed.

Remember, an LR(0) item is of the form: A → α.β. There is no

lookahead symbol.

An LR(1) item has a lookahead symbol which can be either a termi-

nal or $ (end of input). It is of the form: A → α.β, a where a is either

a terminal or $. If there are multiple lookahead symbols for the same

”item”, they can be combined as in: A → αβ, a|b|c....

1.3 CLOSURE for an LR(1) set of items

Let the set I of items contain A → α.Bβ, a and let B → γ be a production.

Add B → .γ, F IRST (βa) to I. Keep doing these, till no more items can

be added to I.

1.4 Sample Grammar for LR(1) Parsing

1. S → CC

2. C → cC

3. C → d

Note that this grammar is unambiguous, left recursion is not elimi-

nated and the productions are number from 1 to 3.

For constructing LR(1) Parser, we need an augmented grammar.

That is a production S ′ → S is added to the grammar. The sample

grammar after augmentation becomes:

• S′ → S

• S → CC

• C → cC
1. LR(0) Parsers 7

• C→d

The initial set of items after CLOSURE of S ′ → .S, $ is

• S ′ → .S, $

• S → .CC, $

• C → .cC, c|d

• C → .d, c|d

Note how the lookaheads are c and d in the last two items.

1.5 The GOTO Function

If a set of LR(1) items I contains an item A → α.Xβ, a where X is a

grammar symbol (variable or terminal) then GOT O(I, X) contains A →

αX.β, a. Thus from a set of items I we GOTO a set J(J = CLOSU RE(J))

on X.

1.6 LR(1) Automaton

Refer to figure 1.1 for the LR(1) automaton which has these states and

the GOTO functions for the augmented sample grammar.

1.7 LR(1) Parsing Table

An LR(1) Parsing Table has a row for every state. It has a column for

every terminal and a column for $ which signifies end-of-input. It has

columns for every variable too.

Refer to figure 1.2 for the LR(1) parsing table for the sample gram-

mar.

If GOT O(Ii , a) = Ij , then the entry for state i on input a is sj where

s stand for shift. If GOT O(Ii , A) = Ij dfor a non-terminal A then the


1. LR(0) Parsers 8

Fig. 1.1: LR(1) Automaton with Sets of Items (States) and the GOTO Function
1. LR(0) Parsers 9

Fig. 1.2: LR(1) Parsing Table for the Sample Grammar


1. LR(0) Parsers 10

entry for i and column A is j. If a set of items contains the complete

item A → α., a|b|c..., then for the lookaheads a, b, c, ..., the entry for that

row is rk where k is the number of the production. If a set of items Ii

contains the item S ′ → S., $, then the action of state i on $ is accept.

1.8 Use of the LR(1) Parsing Table

The parsing algorithm for LR(1) is just like that of the LR(0), except that

it makes use of the LR(1) parsing table instead. We have covered the

example of parsing for the sample input ccdd$ in class.


2. LALR PARSER

LALR stands for lookahead LR. An LALR parser is intermediate in power

between LR(0) and LR(1). For any grammar, the LR(0)and LALR parser

always have the same number of states which is much less than an

LR(1) parser. For a language like C, LR(1) parser has thousands of

states whereas LR(0) and LALR have hundreds of states.

Note that LALR is the parser of choice for many language

parsers and tools like YACC.

If we notice states I4 and I7 in the LR(1) parser (automaton), they

are the same except that the lookaheads are different. In LALR, we

merge these states to form a single state I47 which has the items with a

union of lookaheads {c, d, $}. Similarly, I36 and I89 are formed as given

in figure 2.1.

— Finally, the final LALR Parsing table is given in figure 2.2.

The parsing algorithm is just like that for LR(0) and LR(1).
2. LALR Parser 12

Fig. 2.1: Merged LALR states formed from LR(1)

Fig. 2.2: LALR Parsing Table


BIBLIOGRAPHY

[1] Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman;

Compilers, Principles, Techniques, & Tools, Second Edition, Pear-

son.

You might also like