Bottom-Up Parsing in Compiler Design
Bottom-Up Parsing in Compiler Design
Submitted by:
Name: ID
Ayalew Nigatu RDEG 100/10
Abel G/Alif RDEG 097/10
Samrawit Dagim RDEG 115/10
Mahider Teshome RDEG 113/10
A => A + A
A => A – A
A => (A)
A => a
input string:
a1-(a2+a3) if(a1,a2,a3=a)
Parsing Table:
Stack Input Action
$ a1- (a2+a3)$ Shift a1
$a1 -(a2+a3)$ Reduce by A => a
$A -(a2+a3)$ Shift -
$A- (a2+a3)$ Shift (
$A-( a2+a3)$ Shift a2
$A-(a2 +a3)$ Reduce by A => a
$A -(A +a3)$ Shift +
$A -(A + a3)$ Shift +
$A -(A +a3 ) $ Reduce by A =>a
$A -(A + A ) $ Shift )
$A -(A + A) $ Reduce by A => A +A
$A - (A) $ Reduce by A => (A)
$A $ Accept
Table1: Configuration of shift-reduce parser on input a1-(a2+a3)
Operator Precedence Parser
A grammar that is used to generate or define the mathematical
expression with some restrictions on it is known as operator precedence
grammar. Any grammar can be operator precedence grammar if it follows
two properties:
●No two-variable should be adjacent.
●It should not have a null production.
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.
Bottom-Up Parsing in LR Parser
The bottom-up parsing is based on the concept of LR(K)
parsing.
•L stands for left-to-right parsing of the input string.
•R stands for construction of the rightmost derivation in
reverse.
•K stands for the number of input symbols that the parser
will look a head for making a parsing decision.
The LR parser has the following structure:
Similar to predictive parsing the end of the input buffer and end of stack
has $.
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’.
The LR parsing can be classified as:
LR Parser:
Works on complete set of LR Grammar
•Generates large table and large number of states
•Slow construction
token = next_token()
repeat forever
s = top of stack
if action[s, token] = “shift si” then
PUSH token
PUSH si
token = next_token()
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.