0% found this document useful (0 votes)
132 views21 pages

Csc3205-Bottomup-Parsing

This document provides an overview of bottom-up parsing techniques. It discusses shift-reduce parsing and how bottom-up parsers work by building the parse tree from the bottom up. It also introduces LR parsers as an efficient method of shift-reduce parsing that uses lookahead to determine reduction rules. Finally, it briefly covers LR parsing techniques like LR(0), SLR(1), LR(1), and LALR(1).

Uploaded by

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

Csc3205-Bottomup-Parsing

This document provides an overview of bottom-up parsing techniques. It discusses shift-reduce parsing and how bottom-up parsers work by building the parse tree from the bottom up. It also introduces LR parsers as an efficient method of shift-reduce parsing that uses lookahead to determine reduction rules. Finally, it briefly covers LR parsing techniques like LR(0), SLR(1), LR(1), and LALR(1).

Uploaded by

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

Bottom-Up Parsing

CSC 3205: Compiler Design

Marriette Katarahweire

CSC 3205: Compiler Design 1/21


Outline

The Idea
Shift-Reduce Parsing
LR Parsers
LR Parsing Tables

CSC 3205: Compiler Design 2/21


Bottomup Parsing

A bottom-up parser builds the parse tree from the bottom to


the top.
Bottom-up parsers make much less extravagant predictions
Can handle grammars that top-down parsers cannot.
More general than top-down parsing and just as efficient
Builds on ideas in top-down parsing
Preferred method in practice
Bottom-up parsers work fine if the grammar is left-recursive,
or not left-factored

CSC 3205: Compiler Design 3/21


Bottom-up Parsing

parsing starts with the input symbols and tries to construct


the parse tree up to the start symbol.
Input string: a + b * c
Production rules:
S →E
E →E +T
E →E ∗T
E →T
T → id

CSC 3205: Compiler Design 4/21


Bottom-up Parsing

Read the input and check if any production matches with the
input:
a+b∗c
T +b∗c
E +b∗c
E +T ∗c
E ∗c
E ∗T
E
S

CSC 3205: Compiler Design 5/21


Bottom-up Parsing

Draw the parse tree


Bottom up parsing can be defined as an attempt to reduce
the input string w to the start symbol of a grammar by
tracing out the rightmost derivations of w in reverse.
A bottom up parser uses an explicit stack to perform a parse.
The parsing stack will contain both tokens and nonterminals
plus some other information
The stack is empty at the beginning of a bottom-up parse and
will contain the start symbol at the end of a successful parse.

CSC 3205: Compiler Design 6/21


Classification of Bottom Up Parsers

CSC 3205: Compiler Design 7/21


Shift-Reduce Parsing

A general style of bottom-up parsing


A shift-reduce parser keeps track of two things:
1. the remaining, unread, part of the input;
2. a stack that holds tokens and nonterminals.
Bottom-up parsing uses only two kinds of actions: Shift and
Reduce
Shift a terminal from the front of the input to the top of the
stack
Reduce a string α at the top of the stack to a nonterminal A ,
given the BNF choice A → α
There are also two minor actions:
1. An accept indicates that the parser has successfully found a
derivation.
2. An error action indicates a syntax error.

CSC 3205: Compiler Design 8/21


Reductions

Think of bottom-up parsing as 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
A reduction is the reverse of a step in a derivation (recall that
in a derivation, a nonterminal in a sentential form is replaced
by the body of one of its productions).
The goal of bottom-up parsing is therefore to construct a
derivation in reverse. This derivation is the rightmost
derivation.

CSC 3205: Compiler Design 9/21


Handle Pruning

A ”handle” is a substring that matches the body of a


production, and whose reduction represents one step along the
reverse of a rightmost derivation
The process of discovering a handle & reducing it to the
appropriate left-hand side is called ”Handle Pruning”
Handle pruning forms the basis for a bottom-up parsing
method

CSC 3205: Compiler Design 10/21


Example

E → E + T |F
T → T ∗ F |F
F → (E )|id
Derive a bottom-up parse tree for the string id ∗ id

CSC 3205: Compiler Design 11/21


Example

For the string id ∗ id

Right Sentential Form Handle Reducing Production


id1 ∗ id2 id1 F → id
F ∗ id2 F T →F
T ∗ id2 id2 F → id
T ∗F T ∗F E →T ∗F

CSC 3205: Compiler Design 12/21


Example - A Bottom-up Parse

Given the grammar:


E→T
E→E+T
T→F
T→T*F
F→n
F→(E)
Perform a bottom-up parse of n * n

CSC 3205: Compiler Design 13/21


Example - A Bottom-up Parse

Stack Input Action


$ n*n$ Shift
$n *n$ Reduce by F → n
$F *n$ Reduce by T → F
$T *n$ Shift
$T* n$ Shift
$T*n $ Reduce by F → n
$T*F $ Reduce by T → T * F
$T $ Reduce by E → T
$E $ Accept

Question: Which are the ”handles”?

CSC 3205: Compiler Design 14/21


Exercise

1. Consider the grammar


S→S+S
S→S*S
S → id
Perform Shift Reduce parsing for input string “id + id + id”.
2. Consider the grammar
E → 2E2
E → 3E3
E→4
Perform Shift Reduce parsing for input string “32423”.

CSC 3205: Compiler Design 15/21


LR Parsing

To have an operational shift-reduce parser, we must


determine:
Whether a handle appears on top of the stack
The reducing production to be used
The choice of actions to be made at each parsing step
LR parsing provides a solution to the above problems
Is a general and efficient method of shift-reduce parsing
Is used in a number of automatic parser generators

CSC 3205: Compiler Design 16/21


LR Parsing

Critical Question: How can we know when we have found a handle


without generating lots of different derivations?
– Answer: we use look ahead in the grammar along with tables
produced as the result of analyzing the grammar.
– LR(1) parsers build a DFA that runs over the stack & finds them

CSC 3205: Compiler Design 17/21


LR Parser

The LR parser is a non-recursive, shift-reduce, bottom-up


parser.
It uses a wide class of context-free grammar which makes it
the most efficient syntax analysis technique.
LR parsers are also known as LR(k) parsers
L means that tokens are read left to right
R means that it constructs a rightmost derivation
k denotes the number of lookahead symbols to make decisions.
LR(0) - Left-to-right scanning, Right-most derivation, “zero”
look-ahead characters

CSC 3205: Compiler Design 18/21


LR Parsers

Four LR parsing techniques will be considered


LR(0) : LR parsing with no lookahead token to make parsing
decisions
SLR(1) : Simple LR, with one token of lookahead
LR(1) : Canonical LR, with one token of lookahead
LALR(1) : Lookahead LR, with one token of lookahead
LALR(1) is the preferable technique used by parser generators

CSC 3205: Compiler Design 19/21


Question

What are the differences and similarities between an LL parser and


an LR parser?

CSC 3205: Compiler Design 20/21


Next Topic

Read about:
-LR Parsing Tables
- Semantic Analysis

CSC 3205: Compiler Design 21/21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy