Syntax Analysis II 2024 Student Copy
Syntax Analysis II 2024 Student Copy
BOTTOM-UP PARSING
Dr. R Guru
Associate Professor
Dept. of Computer Science and Engineering,
SJCE, JSSSTU, Mysuru
BOTTOM-UP PARSING
A bottom-up parse corresponds to the construction of
a parse tree for an input string beginning at the leaves
(bottom) and working up towards the root ( top).
Consider the grammar
E→E+T|T
T→T*F|F
F →( E )| id
The sequence of tree snapshots for a bottom-up parse
tree for the input string id*id
Reductions
Bottom-up parsing is the process of "reducing" a string w
to the start symbol of the grammar.
At each reduction step, a specific substring matching the
body of a production is replaced by the nonterminal at the
head of that production.
The key decisions during bottom-up parsing are about
when to reduce and about what production to apply, as the
parse proceeds
Consider the sequence of strings
id * id, F * id, T * id, T * F, T, E
Consider the bottom up parse for id * id
id * id, F * id, T * id, T * F, T, E
(1) 0 $ id * id $ Shift to 5
(2) 05 $ id * id $ Reduce by F→ id
(3) 03 $F * id $ Reduce by T → F
(4) 02 $T * id $ Shift to 7
(5) 027 $T* id $ Shift to 5
(6) 0275 $ T * id $ Reduce by F→ id
(7) 0 2 7 10 $T*F $ Reduce by T→ T * F
(8) 02 $T $ Reduce by E → T
(9) 01 $E $ accept
The LR-Parsing Algorithm
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 LR-Parsing Algorithm
The driver program is the same for all LR parsers; only
the parsing table changes from one parser to another.
The parsing program reads characters from an input
buffer one at a time. Where a shift-reduce parser would
shift a symbol, an LR parser shifts a state. Each state
summarizes the Information contained in the stack below
it.
The stack holds a sequence of states, s0s1 • • • sm , where
sm is on top.
In the SLR method, the stack holds states from the LR(0)
automaton; the canonical- LR and LALR methods are
similar.
By construction, each state has a corresponding grammar
symbol.
The states correspond to sets of items, and that there is a
transition from state i to state j if GOTO(Ii, X) = Ij.
All transitions to state j must be for the same grammar
symbol X. Thus, each state, except the start state 0, has a
unique grammar symbol associated with it .
Structure of the LR Parsing Table
The parsing table consists of two parts: a parsing-action
function ACTION and a goto function GOTO.
1. The ACTION function takes as arguments a state i and a
terminal a or $. The value of ACTION [ i, a] can have one
of four 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 accepts the input and finishes parsing.
(d) Error. The parser discovers an error in its input and takes
some corrective action.