Shift, Reduce Conflicts - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Shift reduce conflicts, LR table size, Ambiguous grammar, DFA state, Reduce reduce conflicts, Parsing table for simple language, Core of set of LR items, Merged state are the points from this lecture. You can find series of lecture notes for compiler construction here.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

asim.amjid
asim.amjid 🇵🇰

4.4

(47)

41 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sohail Aslam Compiler Construction Notes Set:2-1
1
L
Le
ec
ct
tu
ur
re
e
2
29
9
Shift/Reduce Conflicts
Consider the ambiguous grammar
E E + E | E × E | int
We will DFA state containg
[E E × E, +]
[E E + E, +]
Again we have a shift/reduce conflict. We need to reduce because × has precedence
over +
Reduce/Reduce Conflicts
If a DFA states contains both [X α •, a] and [Y β •, a], then on input “a” we don’t
know which production to reduce with. This is called a reduce-reduce conflict. Usually
due to gross ambiguity in the grammar.
LR(1) Table Size
LR(1) parsing table for even a simple language can be extremely large with thousands of
entries. It is possible to reduce the size of the table. Many states in the DFA are similar.
The core of set of LR items is the set of first components without the lookahead
terminals. For example the core of the item { [X αβ, b], [Y γδ, d] } is
{ X α•β, Y γδ }. Consider the LR(1) states
{ [X α •, a], [Y β •, c] }
{ [X α •, b], [Y β •, d] }
They have the same core and can be merged. The merged state contains
{ [X α •, a/b], [Y β , c/d]}
These are called the LALR(1) states. LALR(1) stands for LookAhead LR(1). This leads
to tables that have 10 times fewer states than LR(1).
pf2

Partial preview of the text

Download Shift, Reduce Conflicts - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:2-

Le Leccttuurree 2 299

Shift/Reduce Conflicts

Consider the ambiguous grammar

E → E + E | E × E | int

We will DFA state containg

[E → E × E•, +]

[E → E • + E, +]

Again we have a shift/reduce conflict. We need to reduce because × has precedence over +

Reduce/Reduce Conflicts

If a DFA states contains both [X → α •, a] and [Y → β •, a], then on input “a” we don’t know which production to reduce with. This is called a reduce-reduce conflict. Usually due to gross ambiguity in the grammar.

LR(1) Table Size

LR(1) parsing table for even a simple language can be extremely large with thousands of entries. It is possible to reduce the size of the table. Many states in the DFA are similar.

The core of set of LR items is the set of first components without the lookahead terminals. For example the core of the item { [X → α•β, b], [Y → γ•δ, d] } is { X → α•β, Y → γ•δ }. Consider the LR(1) states

{ [X → α •, a], [Y → β •, c] } { [X → α •, b], [Y → β •, d] }

They have the same core and can be merged. The merged state contains

{ [X → α •, a/b], [Y → β • , c/d] }

These are called the LALR(1) states. LALR(1) stands for L ook A head LR (1). This leads to tables that have 10 times fewer states than LR(1).

Sohail Aslam Compiler Construction Notes Set:2-

Here is the algorithm to generate LALR(1) DFA.

Repeat until all states have distinct code choose two distinct states with same core merge states by creating a new one with the union of all the items point edges from predecessors to new state new state points to all the previous successors

LALR languages are not natural. They are an efficiency hack on LR languages. Any reasonable programming language has a LALR(1) grammar. LALR(1) has become a standard for programming languages and for parser generators.