Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Parsing and Parsing Strategies: Top-Down vs. Bottom-Up, Lecture notes of Compiler Design

An overview of parsing, focusing on top-down and bottom-up strategies. Top-down parsing starts at the root symbol and works its way down, while bottom-up parsing starts at the input symbols and builds up to the root symbol. topics like recursive descent parsing, predictive parsing, and LL(1) parsing, as well as their differences and limitations.

Typology: Lecture notes

2020/2021

Uploaded on 10/03/2021

subroto-mukherjee
subroto-mukherjee 🇮🇳

2 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMPILER DESIGN
(BCSPC5010)
5th Semester CSE (R-18)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Parsing and Parsing Strategies: Top-Down vs. Bottom-Up and more Lecture notes Compiler Design in PDF only on Docsity!

COMPILER DESIGN

(BCSPC5010)

th Semester CSE (R-18)

The process of deriving the string from the given grammar is known as derivation

( parsing ).

 Parsing or syntactic analysis is the process of analyzing a string of symbols (Tokens),

either in natural language or in computer languages, according to the rules of a

formal grammar.

 A parser is a software component that takes a input string “w” and produces output

either a parse tree for “w” or an error message indicating that “w” is not a sentence

of grammar G.

 Two Basic type of parser for CFG

Top Down Parser

Top down parser starts with the root and work down to the leaves.

Example LL(1) parser

Bottom-up parser

Parsing Parsing Syntax Analysis

DEPARTMENT OF CSE, GIET UNIVERSITY,

Parsing

S.N TOP DOWN PARSING BOTTOM UP PARSING

  1. In this parsing technique we

start parsing from top (start

symbol of parse tree) to down

(the leaf node of parse tree) in

top-down manner.

In this parsing technique we start

parsing from bottom (leaf node of

parse tree) to up (the start symbol

of parse tree) in bottom-up

manner.

  1. This parsing technique uses

Left Most Derivation.

This parsing technique uses Right

Most Derivation.

  1. It’s main decision is to select

what production rule to use in

order to construct the string.

It’s main decision is to select when

to use a production rule to reduce

the string to get the starting

symbol.

DEPARTMENT OF CSE, GIET UNIVERSITY,

Difference between Top-Down & Bottom-Up Parsing

Parsing Syntax Analysis

Parser Parser

Top-Down Parser

Bottom-Up / SR

Parser

Recursive Descent

Parser

LR Parser

Operator

Precedence

Parser

LR(0) SLR(1) LALR(1) CLR(1)

Parsing Classification

Backtracking

Non

Backtracking

Predictive Parser

LL(1)

DEPARTMENT OF CSE, GIET UNIVERSITY,

Parsing Syntax Analysis

Recursive descent parser is a top-down parser.

It may requires backtracking to find the correct production to be

applied.

The parsing program consists of a set of procedures, one for each

non-terminal.

Process begins with the procedure for start symbol.

Start symbol is placed at the root node and on encountering each

non-terminal, the procedure concerned is called to expand the non-

terminal with its corresponding production.

Procedure is called recursively until all non-terminals are expanded.

Successful completion occurs when the scan over entire input

string is done. ie., all terminals in the sentence are derived by

DEPARTMENT OF CSE, GIET UNIVERSITY,

Recursive Descent Parser

Parsing Syntax Analysis

Limitation

When a grammar with left recursive

production is given, then the parser

might get into infinite loop. Hence,

left recursion must be eliminated.

(eg.) Let grammar G be,

S → SAd

A → ab | d

Recursive Descent Parser

S

S A d

S A d

S A d

DEPARTMENT OF CSE, GIET UNIVERSITY,

Recursive Descent Parser

Parsing Syntax Analysis

Recursive descent parser without backtracking

Recursive descent parser without backtracking works in a similar

way as that of recursive descent parser with backtracking with the

difference that each non-terminal should be expanded by its correct

alternative in the first selection itself.

When the correct alternative is not chosen, the parser cannot

backtrack and results in syntactic error.

Advantage

  • Overhead associated with backtracking is eliminated.

Limitation

  • When more than one alternative with common prefixes occur, then

the selection of the correct alternative is highly difficult.

Hence, this process requires a grammar with no common prefixes for

alternatives.

DEPARTMENT OF CSE, GIET UNIVERSITY,

Recursive descent parser without backtracking

Parsing Syntax Analysis

Parser

a $

Input

x

Stack

Output

Parsing Table

Input String: The input

string is loaded into the

buffer and it contains $

symbol at the end which

indicates end of the input.

Stack: It usually contains a set of

symbols of the given grammar and

bottom of the stack contains $ which

indicates that stack is empty

Parsing Table: It is a table or matrix M [ A , a ] where "A" represents non-terminal

symbols representing the rows and "a" represents terminal symbols indicating

columns including $ symbol.

Output: The resultant of LL(1) parsing can be either a parse tree or an Error. DEPARTMENT OF CSE, GIET UNIVERSITY,

Predictive Parser

Parsing Syntax Analysis

 An LL Parser accepts LL grammar. LL grammar is a subset of context-free grammar

but with some restrictions to get the simplified version, in order to achieve easy

implementation. LL grammar can be implemented by means of both algorithms

namely, recursive-descent or table-driven.

 LL parser is denoted as LL(k). The first L in LL(k) is parsing the input from left to

right, the second L in LL(k) stands for left-most derivation and k itself represents the

number of look ahead. Generally k = 1, so LL(k) may also be written as LL(1).

LL( 1 )

Left-to-right

Left-most-derivation

No of Look ahead symbols

DEPARTMENT OF CSE, GIET UNIVERSITY,

LL(1) Parser

Parsing Syntax Analysis

An important part of LL(1) parser table construction is to create first

and follow sets.

These sets can provide the actual position of any terminal in the

derivation. This is done to create the parsing table where the decision of

replacing T[A, t] = α with some production rule.

First and Follow sets are needed so that the parser can properly apply

the needed production rule at the correct position.

To construct the Parsing table, we have two functions:

1: First(): If there is a variable, and from that variable if we try to

drive all the strings then the beginning Terminal Symbol is called

the first.

2: Follow(): What is the Terminal Symbol which follow a variable in

LL(1) Parser

DEPARTMENT OF CSE, GIET UNIVERSITY,

LL(1) Parser

Parsing Syntax Analysis

First(Terminal) = {Terminal}

1. For any production rule A → aα

(where a is terminal & α is a set of terminal / Non-terminal)

First(A) = {a}

2. For any production rule A → Ɛ

First(A) = {Ɛ}

3. For any production rule A → Bα

(where Ɛ ∉ First(B))

Then First(A) = First(B)

(where Ɛ ∈ First(B))

Then First(A) = {{First(B)- Ɛ} ∪ {First(α)}}

Compute FIRST( )

DEPARTMENT OF CSE, GIET UNIVERSITY,

Compute FIRST( )

Parsing Syntax Analysis

If α = XYZ

FIRST( α ) = FIRST (XYZ) = {x} - if x is a terminal

Otherwise FIRST(α ) = FIRST(XYZ) = FIRST(X) – if X does not

contain Ɛ

Else if X contains Ɛ than

FIRST(α ) = FIRST(XYZ) = FIRST(X) - Ɛ U FIRST(YZ)

Now FIRST(YZ) can be computed in identical manner

Note: if X Ɛ is a production than FIRST(X) contains Ɛ

Compute FIRST( )

10/03/

DEPARTMENT OF CSE, GIET UNIVERSITY,

17

Rule for Compute FIRST( )

Parsing Syntax Analysis

10/03/2021 DEPARTMENT OF CSE, GIET UNIVERSITY, GUNUPUR 19

DEPARTMENT OF CSE, GIET UNIVERSITY,

Example -

Q. 2: Consider the following grammar and compute FIRST() of all non-terminal

symbols present :

S →ACB | Cbb | Ba

A → da| BC

B → g| Ɛ

C → h| Ɛ

Solution:

FIRST(S) = FIRST(A) U FIRST(C) U FIRST(B) = { d, g, h, Ɛ }

FIRST(A) = { d } U FIRST(B) = { d, g, h, Ɛ }

FIRST(B) = { g, Ɛ }

FIRST(C) = { h, Ɛ }

Parsing Syntax Analysis

Q. 2: Consider the following grammar and compute FIRST() of all non-terminal

symbols present :

E →TE’

E’ → +T E’| Ɛ

T → F T’

T’ → *F T’ | Ɛ

F → (E) | id

Solution:

FIRST(E) = FIRST(T) = FIRST(F) = { ( , id }

FIRST(E’) = { +, Ɛ}

FIRST(T) = FIRST(F) = { ( , id }

FIRST(T’) = { *, Ɛ }

FIRST(F) = { ( , id }

Example to Compute FIRST

DEPARTMENT OF CSE, GIET UNIVERSITY,

Recursive descent parser with backtracking

Parsing Syntax Analysis