Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser...

55
Compiler Design 1 Bottom-UP Parsing Lect 6 Goutam Biswas

Transcript of Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser...

Page 1: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 1✬

Bottom-UP Parsing

Lect 6 Goutam Biswas

Page 2: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 2✬

The Process

• The parse tree is built starting from the leaf

nodes labeled by the terminals (tokens).

• The parser tries to discover appropriate

reductions, and introduces the internal nodes

that are labeled by non-terminals

corresponding to the reductions.

• The process ends at the root node labeled by

the start symbol, or ends with an error

condition.

Lect 6 Goutam Biswas

Page 3: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 3✬

Note

At any intermediate point there is a sequence ofroots of partially constructed trees (from left toright). This sequence is called an upper frontierof the parse tree.

Lect 6 Goutam Biswas

Page 4: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 4✬

Note

At every step the parser tries to find anappropriate β in the upper frontier, which canbe reduced by a rule A → β, so that it leads toacceptance of input.If no such β is available, the parser either callsthe scanner to get a new token, creates a leafnode and extend the frontier, or it reports anerror.

Lect 6 Goutam Biswas

Page 5: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 5✬

Upper Frontier

+ ic * ic

F

T

ic

F

T

ic

F

T

* + ic * ic

F

T

ic

F

T

ic

F

T

*

F

old frontier new frontier

F −−> ic

Lect 6 Goutam Biswas

Page 6: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 6✬

Note

A parser reads input from left-to-right. In abottom-up parser, the first reduction discoveredis the last step of derivation at the left-mostend.Input further away from the left-end areproduced by earlier steps of derivation. Thisindicates a natural sequence of rightmostderivations.

Lect 6 Goutam Biswas

Page 7: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 7✬

Rightmost Derivation of id1 + id2 ∗ id3

E → E + T

→ E + T ∗ F

→ E + T ∗ id3

→ E + F ∗ id3

→ E + id2 ∗ id3

→ T + id2 ∗ id3

→ F + id2 ∗ id3

→ id1 + id2 ∗ id3

Lect 6 Goutam Biswas

Page 8: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 8✬

Reduction of id1 + id2 ∗ id3

id1 + id2 ∗ id3

→ F + id2 ∗ id3

→ T + id2 ∗ id3

→ E + id2 ∗ id3

→ E + F ∗ id3

→ E + T ∗ id3

→ E + T ∗ F

→ E + T

→ E

Lect 6 Goutam Biswas

Page 9: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 9✬

Note

A bottom-up parser follows the derivationsequence in reverse order as it performsreduction. So an upper frontier is a prefix of aright sentential form.

Lect 6 Goutam Biswas

Page 10: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 10✬

Upper Frontiers of id1 + id2 ∗ id3

id1 one new token

→ F

→ T

→ E + id2 two new tokens

→ E + F

→ E + T ∗ id3 two new tokens

→ E + T ∗ F

→ E + T

→ E

Lect 6 Goutam Biswas

Page 11: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 11✬

Handle

Let αβx be a right sentential form, A → β be aproduction rule, and αAx be the previous rightsentential form (x ∈ Σ∗). If k indicates theposition of β in αβx, the doublet (A → β, k) iscalled a handle of the frontier αβ or rightsentential form αβx.

Lect 6 Goutam Biswas

Page 12: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 12✬

Handle

In an unambiguous grammar the rightmostderivation is unique, so a handle of a rightsentential form is unique. But that need not betrue for an ambiguous grammar.

Lect 6 Goutam Biswas

Page 13: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 13✬

Example

In our first example (ic ∗ ic+ ic · · · ), after thereduction of T + F to T + T , the parser doesnot find any other handle in the frontier andinvokes the scanner. It supplies the token for‘∗’. The parser forms the corresponding leafnode and includes it in the frontier (T + T∗).Still there is no handle and the scanner isinvoked again to get the next token ‘ic’. Theparser detects the handle (F → ic, T + T∗ic)and reduces it to F .

Lect 6 Goutam Biswas

Page 14: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 14✬

Shift-Reduce Parsing

The parser essentially takes two types of

actions,

• it detects a handle in the frontier and

reduces it to get a new frontier, or

• if the handle is not present, it calls the

scanner, gets a new token and extends

(shifts) the frontier.

Lect 6 Goutam Biswas

Page 15: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 15✬

Note

The parser may fail to detect a handle and mayreport an error. But if discovered, the handle isalways present at the right end of the upperfrontier.

Lect 6 Goutam Biswas

Page 16: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 16✬

Shift-Reduce Parsing

This is called a shift-reduce parser. It uses astack to hold the upper frontier (left end at thebottom of the stack). The upper frontier is aprefix of a right-sentential form at most up tothe right-most handle. A prefix of the frontieris also called a viable prefix of the rightsentential form.

Lect 6 Goutam Biswas

Page 17: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 17✬

Accept

If the parser can successfully reduce the wholeinput to the start symbol of the grammar. Itreports acceptance of the input i.e. the inputstring is syntactically (grammatically) correct.

Lect 6 Goutam Biswas

Page 18: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 18✬

Example

Consider our old grammar:

1 P → main DL SL end

2 DL → D DL | D

4 D → T VL ;

5 VL → id VL | id

7 T → int | float

9 SL → S SL | ε

Lect 6 Goutam Biswas

Page 19: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 19✬

Production Rules

11 S → ES | IS | WS | IOS

15 ES → id := E ;

16 IS → if be then SL end |

if be then SL else SL end

18 WS → while be do SL end

19 IOS → scan id ; | print e ;

a

aWe are considering BE and E as terminals.

Lect 6 Goutam Biswas

Page 20: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 20✬

Input

Let the input bemain

int id ;id := E ;print E ;

end$The end of input is marked by eof ($) and thebottom-of-stack is also marked by $.

Lect 6 Goutam Biswas

Page 21: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 21✬

Parsing

Value Stack Next Input Handle Action

$ main nil shift

$ main int nil shift

$ main int id (T → int) reduce

$ main T id nil shift

$ main T id ; (VL → id) reduce

$ main T VL ; nil shift

$ main T VL ; id (D → T VL ;) reduce

$ main D id (DL → D) reduce

Lect 6 Goutam Biswas

Page 22: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 22✬

Note

The position of the handle is always on thetop-of-stack. But the main problem is thedetection of handle - when to ask for a newtoken from the scanner and push it in the stack;and when to reduce using a grammar rule.

Lect 6 Goutam Biswas

Page 23: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 23✬

Automaton of Viable Prefixes

It is interesting to note that the viable prefixesof any CFG is a regular language. For someclass of CFG it is possible to design a DFA thatcan be useda to make the shift-reduce decisionof a parser on the basis of the DFA state and afixed number of look-ahead tokens.

aAlong with some heuristic information.

Lect 6 Goutam Biswas

Page 24: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 24✬

LR(k) Parsing

LR(k) is an important class of CFG where abottom-up parsing technique can be usedefficientlya.The ‘L’ is for left-to-right scanning of input,and ‘R’ is for discovering the rightmostderivation in reverse order (reduction) bylooking ahead at most k input tokens.

aOperator precedence parsing is another bottom-up technique that we shall

not discuss. The time complexity of LR(k) is O(n) where n is the length of the

input.

Lect 6 Goutam Biswas

Page 25: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 25✬

Note

We shall consider the cases where k = 0 andk = 1. We shall also consider two other specialcases, simple LR(1) or SLR and look-ahead LRor LALR. An LR(0) parser does not look-aheadto decide its shift or reduce actionsa.

aIt may look-ahead for early detection of error.

Lect 6 Goutam Biswas

Page 26: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 26✬

Note

• An LR parser decides about shift or reduce

actions depending on the state of the

automaton accepting the viable prefixes and

examining a fixed number of current input

tokens (look-ahead).

• The states of the automaton are subsets of

items defined as follows.

Lect 6 Goutam Biswas

Page 27: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 27✬

LR(0) Items

Given a context-free grammar G, an LR(0)item corresponding to a production rule A → αis A → β • γ where α = βγ.As an example, the LR(0) items correspondingto the production rule E → E + T areE → •E + T , E → E •+T , E → E + •T ,E → E + T•.If the length of α is k, it can generate (k + 1)LR(0) items. If A → ε, then the only LR(0)item is A → •.

Lect 6 Goutam Biswas

Page 28: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 28✬

Viable Prefix and Valid Item

An LR(0) item A → α1 • α2 is said to be valid

for a viable prefix αα1 if there is a right-most

sentential form αα1α2x, where x ∈ Σ∗. It

essentially means that during parsing the viable

prefix αα1 may be extended to a handle α1α2,

S ⇒∗rm αAx ⇒rm αα1α2x.

Lect 6 Goutam Biswas

Page 29: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 29✬

Note

Given a viable prefix there may be more thanone valid items.As an example, in the expression grammar, thevalid items corresponding to the viable prefixE + T are E → E + T• and T → T • ∗F .Using the first one the prefix can be extendedto right sentential form asE + Tε = E + T,E + T + ic, · · · . Using thesecond one the prefix can be extended asE + T ∗ ic, · · · .

Lect 6 Goutam Biswas

Page 30: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 30✬

Note

An item A → α • β in the state of theautomaton indicates that the parser has alreadyseen the string of terminals derived from α(α → x) and it expects to see a string ofterminals derivable from β.If β = Bµ i.e. A → α •Bµ, where B is anon-terminal; then the parser also expects tosee any string generated by ‘B’. So all theitems of the form B → •γ are included in thestate of A → α •Bβa.

aThis is actually ε-closure of A → α •Bµ.

Lect 6 Goutam Biswas

Page 31: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 31✬

Main Theorem

The main theorem of LR parsing claims that,the set of valid items of a viable prefix α formsthe state of the deterministic finite automatonthat can be reached from the start state by apath labelled by α.

Lect 6 Goutam Biswas

Page 32: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 32✬

Canonical LR(0) Collection

The set of states of the the DFA of the viableprefix automaton is a collection of the set ofLR(0) items and is known as the canonicalLR(0) collectiona.

aIt is a set of sets.

Lect 6 Goutam Biswas

Page 33: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 33✬

Example

Consider the following grammar:

1 : P → m L s e

2 : L → D L

3 : L → D

4 : D → T V ;

5 : V → d V

6 : V → d

7 : T → i

8 : T → f

Lect 6 Goutam Biswas

Page 34: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 34✬

Closure()

If i is an LR(0) item, then Closure(i) is defined

as follows:

• i ∈ Closure(i) - basis,

• If A → α • Bβ ∈ Closure(i) and B → γ is a

production rule, then B → •γ ∈ Closure(i).

The closure of I, a set of LR(0) items, isdefined as Closure(I) =

⋃i∈I Closure(i).

Lect 6 Goutam Biswas

Page 35: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 35✬

Example

Let i = P → m • L s e,

Closure(i) = {

P → m • L s e

L → •D L

L → •D

D → •T V ;

T → •i

T → •f

}

Lect 6 Goutam Biswas

Page 36: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 36✬

Goto(I,X)

Let I be a set of LR(0) items and X ∈ Σ ∪N .

The set of LR(0) items, Goto(I,X) is

Closure ({A → α X • β : A → α •X β ∈ I}) .

Goto() is the state transition function δ of theDFA.

Lect 6 Goutam Biswas

Page 37: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 37✬

Example

From our previous exampleGoto(Closure(P → m • L s e), D) is

{L → D • L

L → D•

L → •DL

L → •D

D → •TV ;

T → •i

T → •f}

Lect 6 Goutam Biswas

Page 38: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 38✬

Augmented Grammar

We augment the original grammar with a newstart symbol, say S′, that has only oneproduction rule S′ → S$, where S is the startsymbol of the original grammar. When wecome to a state corresponding to (S′ → S$, S)or with the LR(0) item S′ → S • $, we knowthat the input string is well-formed and theparser accepts.

Lect 6 Goutam Biswas

Page 39: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 39✬

LR(0) Automaton

The alphabet of the automaton is Σ ∪N .The start state of the automaton is s0 =Closure(S′ → •S$), the automaton expects tosee the string generated by S.All constructed states are final statesa of theautomaton as it accepts a prefix language.For every X ∈ Σ ∪N and for all states salready constructed, we compute Goto(s,X)b

to build the automaton.aThe constructed automaton is incompletely specified and all unspecified

transitions lead to the only non-final state.bThis nothing but δ(s,X).

Lect 6 Goutam Biswas

Page 40: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 40✬

Example: States

s0 : S′ → •P$ P → •m L s e

s1 : S′ → P • $

s2 : P → m • L s e L → •D L L → •D

D → •T V ; T → •i T → •f

s3 : P → m L • s e

s4 : L → D • L L → D• L → •D L

L → •D D → •T V ; T → •i

T → •f

Lect 6 Goutam Biswas

Page 41: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 41✬

States

s5 : D → T • V ; V → •d V V → •d

s6 : T → i•

s7 : T → f•

s8 : P → m L s • e

s9 : L → D L•

s10 : D → T V •;

s11 : V → d • V V → d• V → •d V

V → •d

Lect 6 Goutam Biswas

Page 42: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 42✬

States

s12 : P → m L s e•

s13 : D → T V ; •

s14 : V → d V •

Lect 6 Goutam Biswas

Page 43: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 43✬

State Transitions

Lect 6 Goutam Biswas

Page 44: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 44✬

CS NS (Input)

m s e ; d i f P L D V T

0 2 1

2 6 7 3 4 5

3 8

4 6 7 9 4 5

5 11 10

8 12

10 13

11 11 14

Lect 6 Goutam Biswas

Page 45: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 45✬

Items

• kernel item:

{S′ → •S$} ∪ {A → α • β : α 6= ε}.

• non-kernel item: {A → •α} \ {S′ → •S$}.

Every non-kernel item in a state comes fromthe closure operation and can be generatedfrom the kernel items. So it is not necessary tostore them explicitly.

Lect 6 Goutam Biswas

Page 46: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 46✬

Note

If a state has an item of the form A → α•, itindicates that the parser has possibly seen ahandle and it may reduce the current rightsentential form to the previous right sententialform. But there may be other complicationsthat we shall take up.

Lect 6 Goutam Biswas

Page 47: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 47✬

Structure of LR Parser

Every LR-parser has a similar structure with acore parsing program, a stack to store thestates of the DFA and a parsing table. Thecontent of the table is different for differenttypes of LR parsers.

Lect 6 Goutam Biswas

Page 48: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 48✬

Structure of LR Parsing Table

The parsing table has two parts, action andgoto.The action(i, a) is a function of two parameters,the current state i of the DFAa and the currentinput symbol, a. The table is indexed by ‘i’ and‘a’. The outcome or the value, stored in thetable, are of four different types.

aThis state is stored on the top of the stack.

Lect 6 Goutam Biswas

Page 49: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 49✬

Action-1

Push Goto(i, a) = j in the stack. This isencoded as sj

a - shift j.The parser has not yet found the handle andaugments the upper frontier by including thenext token (in the leaf node).

aIn fact the input token and the related attribute are also pushed in the

same or a different stack (value stack) for the semantic actions. But that is not

required for parsing.

Lect 6 Goutam Biswas

Page 50: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 50✬

Action-2

Reduce by the rule number j : A → α. Let thelength of α = α1α2 · · ·αk be k. The top k stateson the stack $ · · · qqi1qi2 · · · qik, corresponding tothis αa, are popped out and Goto(q, A) = p ispushed. This is encoded as rj - reduce by rule j.Old stack: $ · · · qqi1qi2 · · · qikNew stack: $ · · · qp

aGoto(q, α1) = qi1 , · · · , Goto(qik−1, αk) = qik .

Lect 6 Goutam Biswas

Page 51: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 51✬

goto Portion

After a reduction (action 2) by the rule A → α,the top-of-stack is q and we have to pushGoto(q, A) = p on the stack. This informationis stored in the goto portion of the table. Thisis the state-transition function restricted to thenon-terminals.

Lect 6 Goutam Biswas

Page 52: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 52✬

Action-3 & 4

The parser accepts the input on some statewhen the only input left is the eof ($). Theparser rejects the input on some state wherethe table entry is undefined.

Lect 6 Goutam Biswas

Page 53: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 53✬

Configuration

The configuration of the parser is specified bythe content of the stack and the remaininginput. If the parser starts with the initial stateof the DFA in the stack, the top-of-stack alwayscontains the current state of the DFA. So theconfiguration is ($q0qi1 · · · qik, ajaj+1 · · · an$). Interms of the sentential form it isα1α2 · · ·αkajaj+1 · · · an$.

Lect 6 Goutam Biswas

Page 54: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 54✬

Initial and Final Configurations

Initial Config.: ($q0, a1 · · · ajaj+1 · · · an$).Final Config.: ($q0qf , $),where Goto(q0, S) = qf and the token stream isempty.

Lect 6 Goutam Biswas

Page 55: Bottom-UP Parsingcse.iitkgp.ac.in/~goutam/bbsCompiler/lect/lect6.pdfCompiler Design 6 Note A parser reads input from left-to-right. In a bottom-up parser, the first reduction discovered

Compiler Design 55✬

Error Configuration

Error Config.: ($q0 · · · q, ajaj+1 · · · an$),where Action(q, aj) is not defined.

Lect 6 Goutam Biswas