![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Algorithm of Predictive Parsing and compute FIRST and FOLLOW for the following Grammar
S → L = R
S → R
L →* R
L → id
R → L
Solution
Computation of FIRST
- S → L = R
∵ L does not derive ε. By rule (4b)of FIRST
∴ FIRST(S) = {FIRST(L)} (1)
- S → R
∵ R does not derive ε. By rule (4b)of FIRST
∴ FIRST(S) = {FIRST(R)} (2)
- L →* R
Applying Rule (3) of FIRST
FIRST(L) = {*} (3)
- L → id
Applying Rule (3) of FIRST
FIRST(L) = {id} (4)
- R → L
Applying Rule (4b) of FIRST
FIRST(R) = {FIRST(L)} (5)
Combining (1) to (5) statements
FIRST(L) = {*, id}
FIRST(S) = {FIRST(L)}
FIRST(R) = {FIRST(L)}
FIRST(S) = {FIRST(R)}
∴ FIRST(L) = FIRST(S) = FIRST(R) = {*, id}
Computation of FOLLOW
S → L = R
S → R
L →∗ R
L → id
R → L
Applying Rule (1) of FOLLOW
FOLLOW (S) ={$} (1)
S → L = R
Applying Rule (2) of FOLLOW
S → | ε | L | = R |
A → | α | B | β |
∵ FIRST(β) = FIRST(= R) = {=}does not contain ε.
∴ Rule (2a) of FOLLOW
∴ FOLLOW(L) = {=} (2)
Applying Rule (3) of FOLLOW
S → | L = | R |
A → | α | B |
∴ FOLLOW(R) = {FOLLOW(S)} (3)
- S → R
We cannot apply Rule(2) on this production as A → 𝛼 B β does not match with S → R.
Applying Rule (3)
S → | ε | R |
A → | α | B |
∴ FOLLOW(R) = {FOLLOW(S)} (4)
- L →* R
Rule (2) does not apply to this production
Applying Rule (3)
L → | * | R |
A → | α | B |
∴ FOLLOW(R) = {FOLLOW(L)} (5)
- R → L
Rule (2) cannot be applied.
∴ Applying Rule (3)
R → | ε | l |
A → | α | B |
∴ FOLLOW(L) = {FOLLOW(R)} (6)
Combining (1) to (6) statements
FOLLOW(S) = {$} (1)
FOLLOW(L) = {=} (2)
FOLLOW(R) = {FOLLOW(S)} (3)
FOLLOW(R) = {FOLLOW(S)} (4)
FOLLOW(R) = {FOLLOW(L)} (5)
FOLLOW(L) = {FOLLOW(R)} (6)
∴ FOLLOW(S) = {$}
FOLLOW(L) = FOLLOW(R) = {$, =}
Algorithm for Predictive Parsing
Input − A string to be parsed & Parsing table 'M' for Grammar.
Output − Stack will become empty, or it will give an error.
Method
While Stack is not empty do
{
Let X be top symbol on Stack
Let a be next input symbol
If X is Terminal or $ then
If X = a then
Pop X form stack & remove a from input
else error ( );
else
If M [X, a] = A → Y1Y2 … … . . Yn
Pop X and Push Yn Yn−1 … . . Y1 onto stack
else error ( );
}