Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Module 2

The document covers Module II of Compiler Construction, focusing on Syntax Analyzers (Parsing) including the role of parsers, types of parsing (top-down and bottom-up), and the writing of context-free grammars. It outlines objectives for understanding parsing techniques, the assessment methods, and the challenges associated with top-down parsing such as ambiguity and left recursion. Additionally, it provides guidelines for eliminating left recursion and left factoring to ensure effective parsing.

Uploaded by

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

Module 2

The document covers Module II of Compiler Construction, focusing on Syntax Analyzers (Parsing) including the role of parsers, types of parsing (top-down and bottom-up), and the writing of context-free grammars. It outlines objectives for understanding parsing techniques, the assessment methods, and the challenges associated with top-down parsing such as ambiguity and left recursion. Additionally, it provides guidelines for eliminating left recursion and left factoring to ensure effective parsing.

Uploaded by

Shivika Mittal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 142

Amity School of Engineering and Technology

COMPILER CONSTRUCTION
Module II
Syntax Analyzer (Parsing)
Dr A. K. Jayswal
ASET(CSE)
MTech(CSE)-JNU
PhD(CSE)-JNU
BSc(CS), MSc(CS)-BHU
GATE(CS), UGC-NET(CS)
1
Module II:

 Role of the parser


 Top-down and bottom-up Parser
 Various top-down and Bottom-up Parser
 Writing Grammars –Context-Free Grammars

2
Amity School of Engineering and Technology

Recommended Reading​
Textbooks: ​
• Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, “Compilers:
Principles Techniques and Tool”, Second Edition, Pearson Publication, 2007.​

• A.A Putambekar, “Compiler Construction”, Technical Publications, 2009. ​

Reference Book: ​
• Des Watson, A Practical Approach to Compiler Construction, First
Edition, Springer, 2017​

3
Amity School of Engineering and Technology

OBJECTIVES
After completing this section, you will be able to
1.1 Understand the Role of the parse

1.2 Analyze & implement –Context-Free Grammars.

1.3 Differentiate between Top-Down parsing and Bottom-up Parsing

1.4 Analyze and Implement various Top-Down parsing and Bottom-up Parsing

4
Amity School of Engineering and Technology

Module Assessment​
• Quiz (Conceptual and Numerical Based)-2 Marks

• Assignment (module-2)-2 marks

5
Amity School of Engineering and Technology

STEPS OF LANGUAGE PROCESSING SYSTEM

6
Amity School of Engineering and Technology

PHASES
OF
COMPILE
R

7
Amity School of Engineering and Technology

PHASES
OF
COMPILE
R

8
Amity School of Engineering and Technology

Parse tree for w,


if w belongs to G
Input string w Parser
Error message

 Parser for any grammar is program that takes as input string w (obtain set
PARSER of strings of tokens from the lexical analyzer) and produces as output
either a parse tree for w , if w is a valid sentences of grammar or error
message indicating that w is not a valid sentences of given grammar.
 The goal of the parser is to determine the syntactic validity of a source
string is valid, a tree is built for use by the subsequent phases of the
computer. The tree reflects the sequence of derivations or reduction used
during the parser. Hence, it is called parse tree. If string is invalid, the
parse has to issue diagnostic message identifying the nature and cause of
the errors in string. Every elementary subtree in the parse tree
corresponds to a production of the grammar.

9
Amity School of Engineering and Technology

Role of the Parser


The parser obtains a string of tokens from the lexical analyzer and verifies that the
string can be generated by the grammar for the source language.
The parser returns any syntax error for the source language

10
Amity School of Engineering and Technology

Role of
the Parser
(syntax
Analyzer)

11
Amity School of Engineering and Technology

There are two ways to identifying (test) whether the string w


(stream of tokens) is a sentence of a given grammar or not.

1. By deriving a string w from the start symbol (non-terminal) S.


or
2. By reducing a string of symbol w to a start non-terminal, S.

The 1st approach is called a Top-down parsing: which build


parse trees from top(root) to bottom(leaves) and
The 2nd approach is called Bottom-up parsing: which build parse
trees from leaves and work up the root.
12
Amity School of Engineering and Technology

Types of Parser

13
Amity School of Engineering and Technology

Types of
Parser

14
Amity School of Engineering and Technology

Variety of Top-Down Bottom-up


Grammar Parser Parser
Comparison Ambiguous (Except OOP)
of Parsing
Unambiguous
Methods
with Variety LR (Left Recursive)

of Grammar RR (Right Recursive)


ND (Non-deterministic)
Deterministic

15
Amity School of Engineering and Technology

Hierarch
y of
Parsing
methods

• LL(1) is less power than any LR parser.

16
Amity School of Engineering and Technology

Difficulties Associated with TDP:

1. Ambiguity
2. Backtracking
3. Left Recursion
4. Left Factoring
5. Sometimes TDP may reject some valid sentences

17
Amity School of Engineering and Technology

Difficulties with TDP:

1. Ambiguity
2. Backtracking
3. Left Recursion
4. Left Factoring
5. Sometimes TDP may reject some valid sentences

2. G1 : S → aSbS | bSaS | λ
G2: s → aAB A → bBb B → A | λ
G3: s → SS | aSb | bSa | λ

18
Ambiguous Grammar

Definition. A grammar G is ambiguous if there is a word w  L(G) having are


least two different parse trees

SA
SB
S  AB
A  aA
B  bB
Ae
Be

Notice that a has at least two left-most derivations


Ambiguity
Amity School of Engineering and Technology

Rules for Disambiguating a grammar


Generally, productions are ambiguous when they have more than one occurance of
a given non-terminal on their right-hand side.

Rules:
1. Check the precedence of the operators involved.
2. Different precedence operators are treated differently for removing ambiguity.
3. First remove the ambiguity for the minimum precedence operator and so on…

21
Amity School of Engineering and Technology

Rules for Disambiguating a grammar


Example: If a production rule is
S->ASBSy|a1|a2|……|an
Then ambiguity can be removed by rewriting the production rule as
S -> ASBSy|S’
S’-> a1|a2|…|an

If the operator is left associative then change the right most symbol
Eg.- E->E*E|id
E->E*E’|E’
E’-> id
If the operator is right associative then change the Left most symbol
Eg.- E->E⬆E|id
E->E’⬆E|E’
E’-> id 22
Amity School of Engineering and Technology

Difficulties Associated with TDP:


1. Ambiguity 2. Backtracking 3. Left Recursion 4. Left Factoring
5. Sometimes TDP may reject some valid sentences

2. Backtracking:
 Backtracking is a technique in which for expansion of non-terminal symbol, we choose
alternative and if some mismatch occurs then we try another alternative if any.
 If for a non-terminal, there are multiple production rules beginning with the same input symbol
then to get the correct derivation, we need to try all these alternatives.
 Secondly, in backtracking, we need to move some levels upward in order to check the
possibilities. This increases lot of overhead in implementation of parsing.
 Hence, it becomes necessary to eliminate the backtracking by modifying the grammar.
Example: ScAd
Aab/a
Consider w=cad

23
Amity School of Engineering and Technology

Difficulties Associated with TDP:


1. Ambiguity 2. Backtracking 3. Left Recursion 4. Left Factoring
5. Sometimes TDP may reject some valid sentences

3. Left Recursion:
A Grammar G is said to be left recursive, if it has a derivation of the form
It can cause a top-down parser to go into an infinite loop. To use TDP, we have to eliminate left recursion.

24
Amity School of Engineering and Technology

Elimination of Left Recursion

25
Amity School of Engineering and Technology

Elimination of Left Recursion

26
Amity School of Engineering and Technology

Immediate Left Recursion-Example

27
Amity School of Engineering and Technology

Left Recursion (two or more level deep left recursion….

28
Amity School of Engineering and Technology

Left Recursion (two or more level deep left recursion)

29
Amity School of Engineering and Technology

General Algorithm-Removing Left Recursion

30
Amity School of Engineering and Technology

Left Recursion-Example1

31
Amity School of Engineering and Technology

Elimination of Left Recursion-Example2

32
Amity School of Engineering and Technology

Left
Factoring

A TDP without backtracking (Predictive parser) insists that Grammar must be left factored.

33
Amity School of Engineering and Technology

Left
Factoring

34
Amity School of Engineering and Technology

Left Factoring
Example:
S->cAd
A->ab/a
After LF, it can be re-write
as:
S->cAd
A->aA’
A’->b/Null

35
Amity School of Engineering and Technology

Left Factoring- Algorithm

36
Amity School of Engineering and Technology

Left Factoring- Example1

37
Amity School of Engineering and Technology

Left Factoring- Example2

38
Amity School of Engineering and Technology

Check Your Progress-1


Remove left factoring:
1:

39
Amity School of Engineering and Technology

Top-Down Parser
 Top-down parser build parse tree from the top (root) to the bottom (leaves).
 A left-most derivation is applied at each derivation step
 Top-down parser may require (need) Backtracking, that is it may require repeated scan
over the input. However, backtracking is rarely needed to parse programming language
constructs. Backtracking is an extra overhead so generally backtracking parser are not
used frequently.
 Example of TDP :
- Recursive descent parsing,
- Predictive parsing and
- LL(1) parsing.

Note : TDP does not allow ambiguous grammar, No left recursive grammar, no non-deterministic
grammar (can be removed using left factoring)

40
Amity School of Engineering and Technology

Top-Down Parser

41
Amity School of Engineering and Technology

Recursive Descent Parsing


 A Parser that uses a set of recursive procedure for each Nonterminal of the grammar to
recognize its input is called RDP.
 This parsing technique recursively parses the input to make a parse tree, which may or
may not requires backtracking.
 RDP consists of several small functions, one for each nonterminal in the grammar.
 A form of RDP that does not require any backtracking is called a Predictive Parsing.

42
Amity School of Engineering and Technology

Recursive-descent parsing (Example1)


Q.1: Write a RDP for the following grammar:
[No TDP allow ambiguous, Left-recursion, ND grammar].
So, after removing left recursion Grammar is written as:

parse the input string

43
Amity School of Engineering and Technology

Recursive-descent parsing (Example2)


Q.1: Write a RDP for the following T
grammar:
}

E();

input++;

Main()
{
E();
If (input==‘$’)
Write production for each nonterminals. Printf(“ parsing successful);
}

44
Amity School of Engineering and Technology

Top-Down Parsing
Top-Down parsing does not support ambiguous , left recursive and left factoring.

If the grammar is having all the three then


1. Remove ambiguity, if possible, by rewriting the grammar
2. Remove left- recursion, otherwise it may lead to an infinite loop.
3. Do left- factoring.

45
Amity School of Engineering and Technology

Top-Down Parsing
Top Down Parsing involves construction of parse tree from root to leaves using left most
derivation.

Top Down Parsing can be done in two ways:


a) With Backtracking
b) Without Backtracking

46
Amity School of Engineering and Technology

Top-Down Parsing with Backtracking


If a left most non-terminal has more than one production i.e.
A->a1|a2|…|an
Then there is a problem to decide which A-production should be used for derivation.

Example: S->cAd, a->ab|a


For string, cad

47
Amity School of Engineering and Technology

Top-Down Parsing without Backtracking


It is of two types:
1) Recursive Descent Parsing (RDP)
2) Predictive parsing

Recursive Descent Parsing: It is top down method of syntax analysis which uses collection of recursive
procedures to process the input.
Predictive Parsing:
It is a special form of RDP, in which the look-ahead symbol unambiguously determines the procedure
selected for each non-terminal.
The look-ahead symbol guides the selection of the production to be used. Writing a grammar after
eliminating left-recursion and left-factoring guaranteed a RDP with no Backtracking.
It is also called as non recursive predictive parsing because it implements the recursive descent parsing by
maintaining a stack of activation records.

48
Amity School of Engineering and Technology

Predictive parsing:

49
Amity School of Engineering and Technology

Steps for constructing Predictive parsing table:

Step1: For a given grammar eliminate the Left-Recursion (if any).


Step2: Eliminate the Left-factoring (if any).
Step3: Construct the FIRST set and FOLLOW Set.
Step4: Construct Predictive Parsing Table using Algorithm (which uses FIRST and FOLLOW set).

Finally, Parse the given string w using Algorithm and parsing table created in step4.

50
FIRST and FOLLOW set:
FIRST and FOLLOW Set:

Note: FIRST and Follow set are useful in creating parse table in Top-Down and Bottom-Up parsing.
Amity School of Engineering and Technology

Rule2: For any terminal symbol ‘a’ , First(a) = { a }

All 3 Rules Rule3: For a production rule X → Y1Y2Y3,


for
calculating Calculating First(X):
FIRST set
• If ∈ ∉ First(Y1), then First(X) = First(Y1)
• If ∈ ∈ First(Y1), then First(X) = { First(Y1) – ∈ } ∪ First(Y2Y3)

Calculating First(Y2Y3)

• If ∈ ∉ First(Y2), then First(Y2Y3) = First(Y2)


• If ∈ ∈ First(Y2), then First(Y2Y3) = { First(Y2) – ∈ } ∪ First(Y3)

Similarly, we can make expansion for any production rule X → Y 1Y2Y3…..Yn.

55
Amity School of Engineering and Technology

Example:
SACB/CbB/Ba
Ada/BC
Bg/∈
Ch/∈

Compute FIRST
set.

56
FOLLOW(A):
[where A is any nonterminal of G]
Follow(A) is a function which gives a set of all terminals that can appear immediately to the right of
symbol A. That is [to calculate follow we always focus on RHS of “arrow” nonterminal].

Note: symbol is not written (allowed) in follow set of any nonterminal.


In Rule2: , then Follow(B)=FIRST()-
In Rule3: (a) means Follow(B)=Follow(A)
(b) where , then Follow(B)=(First()- ) Follow(A)
 If A can be last symbol (any way) in some sentential form, then will be included in Follow of that nonterminal.
Amity School of Engineering and Technology

Example1:
SaSbS/bSaS/∈

Compute FIRST and


Follow set.

58
Amity School of Engineering and Technology

Example2:
SABC
ADEF
B/b
C/c
D /d
E /e
F /f
Compute FIRST and
FOLLOW set.

59
Amity School of Engineering and Technology

Example3:

Compute FIRST and


FOLLOW set.

60
Amity School of Engineering and Technology

Note: if grammar is Left recursive then first remove it then calculate FIRST and FOLLOW SET,
before creating a TDP table (i.e. predictive parsing)

67
Amity School of Engineering and Technology

We have-
•The given grammar is left recursive.
•So, we first remove left recursion from the given grammar.

After eliminating left recursion, we get the following grammar-


Example10:
Compute FIRST and
FOLLOW set.

Now, the first and follow functions are calculated as follows-

•First(E) = First(T) = First(F) = { ( , id }


•First(E’) = { + , ∈ }
• Follow(E) = { $ , ) }
•First(T) = First(F) = { ( , id } • Follow(E’) = Follow(E) = { $ , ) }
•First(T’) = { * , ∈ } • Follow(T) = { First(E’) – ∈ } ∪ Follow(E) ∪ Follow(E’) = { + , $ , ) }
•First(F) = { ( , id } • Follow(T’) = Follow(T) = { + , $ , ) }
• Follow(F) = { First(T’) – ∈ } ∪ Follow(T) ∪ Follow(T’) = { * , + , $ , ) }

78
Amity School of Engineering and Technology

Check Your Progress-2

79
Amity School of Engineering and Technology

Check Your Progress-3

80
Amity School of Engineering and Technology

Check Your Progress-4

81
Amity School of Engineering and Technology

Check Your Progress-5

82
Amity School of Engineering and Technology

Steps for constructing Predictive parsing table:

Step1: For a given grammar eliminate the Left-Recursion (if any).


Step2: Eliminate the Left-factoring (if any).
Step3: Construct the FIRST set and FOLLOW Set.
Step4: Construct Predictive Parsing Table using Algorithm (which uses FIRST and FOLLOW set).

Finally, Parse the given string w using Algorithm and parsing table created in step4.

83
Amity School of Engineering and Technology

Predictive
Parser

84
Amity School of Engineering and Technology

Predictive
Parser

85
Amity School of Engineering and Technology

Parsing Table M:
It is a 2D table
where A is a
Nonterminal and a
is a terminal
Predictive symbol or .

Parser

86
Amity School of Engineering and Technology

Construction of Predictive Parsing Table

87
Amity School of Engineering and Technology

Predictive Parser Program

88
Amity School of Engineering and Technology

Predictive Parser

89
Amity School of Engineering and Technology

Predictive Parser

90
Amity School of Engineering and Technology

Example:
Table-driven
Predictive
Parser

91
Amity School of Engineering and Technology

Construction of Predictive Parsing Table

92
Amity School of Engineering and Technology

Note: Since there is no multiple entries in Table M, so the grammar is LL(1).


Amity School of Engineering and Technology

94
Amity School of Engineering and Technology

Predictive Parser (Parsing Algorithm using Table)


Predictive Parsers is controlled by a program which behaves as follows.
This program determines (X: top element of stack) and (a - current input symbol) and
perform the following actions:-
 If X=a=$, then parser halts and return successful completion of parsing.
 If X=a≠$, then parser pops X from the stack and advance the input
pointer to next input symbol.
 If X is a Nonterminal, then it checks in the parsing table entry for .
If , then parser replace X by with at the top of the stack.
else if
then it’s an Error.

95
Amity School of Engineering and Technology

Predictive Parser (Parsing Algorithm using Table)

97
Amity School of Engineering and Technology

Moves made
by Predictive
Parser for
Input:

98
Example2:
Amity School of Engineering and Technology

Check your progress-2


Q.1 Design a TDP for the following Grammar:

Using a parse table, check whether the string belongs to grammar or not.

Q.2 Construct a predictive Parsing table for the following grammar. Also, check whether the grammar
is LL(1) or not?

Parse the string


101
Amity School of Engineering and Technology

LL(1) Grammar
Ambiguous Grammar or Left Recursive Grammar cannot be LL(1) grammar.

103
Amity School of Engineering and Technology

Note point

1. If a Grammar is Ambiguous, then it can not be LL(1).


For example:

2. If a Grammar contains left recursion, then it can not be LL(1).


For example:

3. If a Grammar contains any factoring, then it can not be LL(1).


For example:

104
Amity School of Engineering and Technology

Check Your Progress-1


Check Whether the following grammar is LL(1) or not?
Q1.

Q2.

Q.3

Q.5
Q.4. SiCtSS’/a
S’eS/
Cb
109
Amity School of Engineering and Technology

Types of Parsing

110
Amity School of Engineering and Technology

Shift Reduce Parser

111
Amity School of Engineering and Technology

Bottom-Up Parsing

112
Amity School of Engineering and Technology

Bottom-Up Parsing

113
Amity School of Engineering and Technology

Bottom-Up Parsing

114
Amity School of Engineering and Technology

Bottom-Up Parsing

115
Amity School of Engineering and Technology

Bottom-Up Parsing

116
Amity School of Engineering and Technology

Bottom-Up Parsing

117
Amity School of Engineering and Technology

Bottom-Up Parsing

118
Amity School of Engineering and Technology

Practice Questions
Ques1: Consider the grammar
S->TL;
T->int|float
L->L,id|id

Parse the input string int id, id; using shift reduce parser.

Ques2: Consider the grammar


S->TL;
T->int|float
L->L,id|id
Check if the sentence “ float id,id,id;”is syntactically correct using predictive parsing

119
Amity School of Engineering and Technology

Practice Questions
Ques3: Consider the grammar
E->E+T
T-> id|id[]|id[X]
X-> E, E|E
Find FIRST and FOLLOW of all the non-terminals.
Ques4: Consider the grammar
S->(A)|0
A->SB
B->,SB| e
Is the above grammar LL(1)? Justify your answer.
Ques5: Consider the grammar
E->+EE| *EE |num
Construct LL(1) parsing table for it.
120
Amity School of Engineering and Technology

Practice Questions
Ques 7: Construct the predictive parser for the following grammar
S->a|^|(T)
T->T,S| S
Show the behavior of the parser in the sentences:
i) (a,(a,a))
ii) (((a,a),^,(a),a)
Ques 8: Construct the predictive parser for the following grammar
S->(L)|a
L-> L,S| S
Parse the sentence (a,a) using predictive parsing for the above grammar.

121
Amity School of Engineering and Technology

122
Amity School of Engineering and Technology

123
Amity School of Engineering and Technology

124
Amity School of Engineering and Technology

125
Amity School of Engineering and Technology

Check Your Progress-1

Check whether the following grammar can be


converted into
an Operator Grammar or not?

126
Amity School of Engineering and Technology

127
Amity School of Engineering and Technology

Note: By using LEADING and TRAILING set, we can also construct a Precedence relation Table.
128
Amity School of Engineering and Technology

Leading and Trailing Set:

Note: By using LEADING and TRAILING set, we can also construct a Precedence relation Table.

129
Amity School of Engineering and Technology

Algorithm for constructing Operator Precedence Relation table (OPRT) using Leading and
Trailing Set

130
Amity School of Engineering and Technology

Creating OPPT using


Leading and trailing Set

131
Amity School of Engineering and Technology

132
Amity School of Engineering and Technology

In other words

133
Amity School of Engineering and Technology

134
Amity School of Engineering and Technology

Consider a string w=

135
Amity School of Engineering and Technology

136
Amity School of Engineering and Technology

137
Amity School of Engineering and Technology

Algorithm for Precedence Function

138
Amity School of Engineering and Technology

139
Amity School of Engineering and Technology

Advantages/disadvantages of Operator function table

1. Table size is reduced from to O(n) since there are only 2n entries in operator
function table.
2. But the Error capability is less than the precedence table.

*************************************************************************

140
Amity School of Engineering and Technology

142

You might also like