Bottom-Up Parsing
Bottom-Up Parsing
Group assignment :
Submitted by:
Name: ID
The bottom-up parser builds a parse tree from the leaf nodes and proceeds
towards the root node of the tree. In this section, we will be discussing bottom-up
parsing along with its types.
The bottom-up parsers are created for the largest class of LR grammars. As the
bottom-up parsing corresponds to the process of reducing the string to the
starting symbol of the grammar.
The substring in the input string is replaced by the non-terminal identified in step
2.
The bottom of the stack and the right end of the input buffer is marked with the
$. Initially, before the parsing starts:
The input buffer holds the input string provided by the lexical analyzer.
As the parser parses the string from left to right then it shifts zero or more input
symbols onto the stack.
The parser continues to shift the input symbol onto the stack until it is filled with a
substring. A substring that matches the production body of a nonterminal in the
grammar. Then the substring is replaced or reduced by the appropriate
nonterminal.
•The parser continues shift-reducing until either of the following condition occurs:
Input Buffer.
Stores the input string.
Stack.
Used for storage and access of the production rules.
It identifies an error
The stack contains the start symbol of the grammar and the input buffer becomes
empty.
To perform the shift-reduce parsing we have to perform the following four
functions.
Shift: This action shifts the next input symbol present on the input buffer onto the
top of the stack.
Reduce: This action is performed if the top of the stack has an input symbol that
denotes a right end of a substring. And within the stack there exist the left end of
the substring. The reduce action replaces the entire substring with the
appropriate non-terminal. The production body of this non-terminal matches the
replaced substring.
Accept: When at the end of parsing the input buffer becomes empty. And the
stack has left with the start symbol of the grammar. The parser announces the
successful completion of parsing.
Error: This action identifies the error and performs an error recovery routine.
Limitations.
Shift-reduce conflicts -> Whereby for every shift-reduce for context free grammar,
a point is reached where the parser cannot decide whether to shift or reduce.
Reduce-reduce conflicts -> Whereby the parser cannot decide which of the several
reductions to make.
Let us take an example of the shift-reduce parser. Consider that we have a string
id+id * id and the grammar for the input string is:
E ->E + T | T
T -> T * F | F
F -> ( E ) | id
Another example
Grammar:
A => A + A
A => A – A
A => (A)
A => a
input string:
a1-(a2+a3)
Parsing Table:
$A -(a2+a3)$ Shift -
$A $ Accept
Table1: Configuration of shift-reduce parser on input a1-(a2+a3)
Note: In the shift-reduce parsing a handle always appears on the top of the stack.
The handle is a substring that matches the body of production in the grammar.
The handle must never appear inside the stack.
•K stands for the number of input symbols that the parser will look ahead for
making a parsing decision.
The input buffer has the input string that has to be parsed.
The stack maintains the sequence of grammar symbols while parsing the input
string.
The parsing table is a two-dimensional array that has two entries ‘Go To’ and
‘Action’.
LR(1) – LR Parser:
Works on complete set of LR(1) Grammar
•Slow construction
CLR Parser :
The CLR parser stands for canonical LR parser.It is a more powerful LR parser.It
makes use of lookahead symbols. This method uses a large set of items called
LR(1) items.The main difference between LR(0) and LR(1) items is that, in LR(1)
items, it is possible to carry more information in a state, which will rule out
useless reduction states.This extra information is incorporated into the state by
the lookahead symbol. The general syntax becomes [A->∝.B, a ]
where A->∝.B is the production and a is a terminal or right end marker $
LR(1) items=LR(0) items + look ahead
LR Parsing Algorithm
token = next_token()
repeat forever
s = top of stack
if action[s, token] = “shift si” then
PUSH token
PUSH si
token = next_token()
s = top of stack
PUSH A
PUSH goto[s,A]
return
else
error()
All kinds of LR parsers are the same they only differ in the construction of their
parsing table. We will discuss each of them in brief in our future contents.
So, this is all about the Bottom-Up parsing. We have discussed the most general
form of bottom-up parsing i.e., shift-reduce bottom-up parsing along with an
example. We have discussed the structure of the LR parser and its types.