0% found this document useful (0 votes)
4 views

Compiler Design 4

Uploaded by

ruhinalmuhit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Compiler Design 4

Uploaded by

ruhinalmuhit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2020-8-28

Sy nt a x A n a l y s i s
—— Z a k i a Z i n at C h o u d h u r y
Lecturer
Department of Computer Science & Engineering
U n i ve r s i t y o f R a j s h a h i

CONTENTS

1. Left Factoring
2. Types of Parsing
3. FIRST and FOLLOW
4. Top-down Parsing

LL Parser
8. Predictive Parsering Table

1
2020-8-28

Left Factoring
If more than one grammar production rules has a common prefix string, then
the top-down parser cannot make a choice as to which of the production it
should take to parse the string in hand.

Then it cannot determine which production to follow to parse the string as both
productions are starting from the same terminal (or non-terminal). To remove
this confusion, a technique is used that is called left factoring.

Left factoring transforms the grammar to make it useful for top-down parsers.
In this technique, one production is made for each common prefixes and the
rest of the derivation is added by new productions.

Types of Parsing
Syntax analyzers follow production rules defined
by means of context-free grammar. The way the
production rules are implemented (derivation)
divides parsing into two types : top-down parsing
and bottom-up parsing.

Parsing

Top-down Parsing Bottom-up Parsing

Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU 4

2
2020-8-28

FIRST and FOLLOW


An important part of parser table construction is to create first and follow sets. These sets can provide
the actual position of any terminal in the derivation.

The construction of both top-down and bottom-up parsers is helped by two functions, FIRST and
FOLLOW, associated with a grammar G. During top-down parsing, FIRST and FOLLOW allow us to
choose which production to apply, based on the next input symbol.

Define FIRST(α), where α is any string of grammar symbols, to be the set of terminals that begin
strings derived from α. If α → Ꜫ, then Ꜫ is also in FIRST (α).

Define FOLLOW(A), for nonterminal A, to be the set of terminals a that can appear immediately to the
right of A in some sentential form; that is, the set of terminals a such that there exists a derivation of the
form S→ αAaβ, for some α and β.

Top-down Parsing
When the parser starts constructing the parse tree from the start symbol and then tries to
transform the start symbol to the input, it is called top-down parsing.
Equivalently, top-down parsing can be viewed as finding a leftmost derivation for an input
string.
The types of top-down parsing are discussed below:
Top-down Parsing

Recursive Descent Parsing

Back-tracking Non Back-tracking

Predictive Parser

LL Parser
6

3
2020-8-28

Recursive descent is a top-down parsing technique that constructs the parse tree from
the top and the input is read from left to right. It uses procedures for every terminal and
non-terminal entity. This parsing technique recursively parses the input to make a parse
tree, which may or may not require back-tracking. But the grammar associated with it (if
not left factored) cannot avoid back-tracking. A form of recursive-descent parsing that
does not require any back-tracking is known as predictive parsing.

This parsing technique is regarded recursive as it uses context-free grammar which is


recursive in nature.

Top- down parsers start from the root node (start symbol) and match the input string
against the production rules to replace them (if matched).

4
2020-8-28

Predictive Parser
Predictive parser is a recursive descent parser, which has the capability to predict which production is to
be used to replace the input string. The predictive parser does not suffer from backtracking.

To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to the next input
symbols. To make the parser back-tracking free, the predictive parser puts some constraints on the
grammar and accepts only a class of grammar known as LL(k) grammar.

Predictive parsing uses a stack and a parsing


table to parse the input and generate a
parse tree. Both the stack and the input
contains an end symbol $ to denote that the
stack is empty and the input is consumed.
The parser refers to the parsing table to take
any decision on the input and stack element
combination.

LL Parser
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 aheads. Generally k = 1, so LL(k) may also be written as LL(1).

5
2020-8-28

Predictive Parsering Table

Algorithm of Construction of a predictive parsing table

Top-down Parser

Remove Left Recursion


Left Factored Grammar

Recursive Descent Parser

Remove Back-Tracking

Predictive Parser

Use Predictive Parsing Table


Remove Recursion

LL Parser

6
2020-8-28

Thank you
——

You might also like