0% found this document useful (0 votes)
15 views15 pages

Bottom Up Parser

Bottom-up parsers, specifically shift-reduce parsers, construct parse trees from the leaves to the root by reducing the input string to the start symbol of the grammar. LR parsing is a type of bottom-up parsing that scans input from left to right while constructing rightmost derivations in reverse. The document also discusses augmented grammars, LR(0) items, closure and goto operations, and the construction of LR(0) parsing tables, highlighting potential conflicts that can arise in the parsing process.

Uploaded by

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

Bottom Up Parser

Bottom-up parsers, specifically shift-reduce parsers, construct parse trees from the leaves to the root by reducing the input string to the start symbol of the grammar. LR parsing is a type of bottom-up parsing that scans input from left to right while constructing rightmost derivations in reverse. The document also discusses augmented grammars, LR(0) items, closure and goto operations, and the construction of LR(0) parsing tables, highlighting potential conflicts that can arise in the parsing process.

Uploaded by

Pradnya Vikhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Bottom Up Parser

Bottom-up Parsers
• Bottom-up Parsers / Shift Reduce Parsers Build the
parse tree from leaves to root.
• Bottom-up parsing can be defined as an attempt to
reduce the input string w to the start symbol of
grammar.
• It is done by tracing out the rightmost derivations of w in
reverse.
A general shift reduce parsing is LR parsing.
The L stands for scanning the input from left to right and R stands
for constructing a rightmost derivation in reverse.
• Augmented Grammar: If G is a grammar with start symbol S
then G’, the augmented grammar for G, is the grammar with
new start symbol S’ and a production S’ -> S.
Let a grammar be S -> AA
A -> aA | b,
The augmented grammar for the above grammar will be
S’ -> S
S -> AA
A -> aA | b.
• LR(0) Items: An LR(0) is the item of a grammar G is a production of
G with a dot at some position in the right side.
S -> ABC yields four items
S -> .ABC
S -> A.BC
S -> AB.C
S -> ABC. The production A -> ? generates only one item A -> .?
• Closure Operation: 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. Initially every item in I is added to closure(I).
2. If A -> ?.B? is in closure(I) and B -> ? is a production then add
the item B -> .? to I, If it is not already there.
We apply this rule until no more items can be added to
closure(I).
Goto Operation : Goto(I, X) =
Add I by moving dot after X.
Apply closure to first step.
Construction of GOTO graph-
• State I0 – closure of augmented LR(0) item
• Using I0 find all collection of sets of LR(0) items with the help of DFA
• Convert DFA to LR(0) parsing table
• Construction of LR(0) parsing table:
• The action function takes as arguments a state i and a
terminal a (or $ , the input end marker). The value of
ACTION[i, a] can have one of four forms:
• Shift j, where j is a state.

• Reduce A -> ?.

• Accept

• Error

• We extend the GOTO function, defined on sets of items,


to states: if GOTO[Ii , A] = Ij then GOTO also maps a
state i and a nonterminal A to state j.
Consider the grammar
• S ->AA
• A -> aA | b

Step 1:
• Augmented grammar
• S’ -> S
• S -> AA
• A -> aA | b

• The LR(0) parsing table for above GOTO graph will be –


The LR(0) parsing table for above GOTO graph will be –
Note:
• 1. Two reduced productions in one state – RR conflict.
• 2. One reduced and one shifted production in one state
– SR conflict.
If no SR or RR conflict present in the parsing
table then the grammar is LR(0) grammar.

You might also like