SlideShare a Scribd company logo
PARSING
BY: KHUSHBOO JETHWA
ENROLLMENT NO.: 140950107028
DEFINITION OF PARSING
• A parser is a compiler or interpreter component that breaks
data into smaller elements for easy translation into another
language.
• A parsertakes input in the form of a sequence of tokens or
program instructions and usually builds a data structure in
the form of a parse tree or an abstract syntax tree.
ROLE OF PARSER
•In the compiler model, the parser obtains a
string of tokens from the lexical analyser,
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.
• It collects sufficient number of tokens anf
builds a parse tree
Parsing
• There are basically two types of parser:
• Top-down parser:
• starts at the root of derivation tree and fills in
• picks a production and tries to match the input
• may require backtracking
• some grammars are backtrack-free (predictive)
• Bottom-up parser:
• starts at the leaves and fills in
• starts in a state valid for legal first tokens
• uses a stack to store both state and sentential forms
TOP DOWN PARSING
• A top-down parser starts with the root of the parse tree, labeled
with the start or goal symbol of the grammar.
• To build a parse, it repeats the following steps until the fringe of
the parse tree matches the input string
• STEP1: At a node labeled A, select a production A → α and construct
the appropriate child for each symbol of α
• STEP2: When a terminal is added to the fringe that doesn’t match the
input string, backtrack
• STEP3: Find the next node to be expanded.
• The key is selecting the right production in step 1
EXAMPLE FOR TOP DOWN PARSING
• Supppose the given production rules are as follows:
• S-> aAd|aB
• A-> b|c
• B->ccd
PROBLEMS WITH TOPDOWN
PARSING
1) BACKTRACKING
• Backtracing is a technique in which for expansion of non-terminal
symbol we choose one 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.
EXAMPLE OF BACKTRACKING
• Suppose the given production rules are as follows:
• S->cAd
• A->a|ab
• 2) LEFT RECURSION
• Left recursion is a case when the left-most non-terminal in a
production of a non-terminal is the non-terminal itself( direct left
recursion ) or through some other non-terminal definitions, rewrites
to the non-terminal again(indirect left recursion). Consider these
examples -
• (1) A -> Aq (direct)
• (2) A -> Bq
B -> Ar (indirect)
• Left recursion has to be removed if the parser performs top-down
parsing
REMOVING LEFT RECURSION
• To eliminate left recursion we need to modify the grammar. Let, G be
a grammar having a production rule with left recursion
• A-> Aa
• A->B
• Thus, we eliminate left recursion by rewriting the production rule as:
• A->BA’
• A’->aA’
• A’->c
3) LEFT FACTORING
•Left factoring is removing the common left factor that appears in two
productions of the same non-terminal. It is done to avoid back-tracing
by the parser. Suppose the parser has a look-ahead ,consider this
example-
•A -> qB | qC
where A,B,C are non-terminals and q is a sentence. In this case, the
parser will be confused as to which of the two productions to choose
and it might have to back-trace. After left factoring, the grammar is
converted to-
•A -> qD
•D -> B | C
RECURSIVE DESCENT PARSING
• A recursive descent parser is a kind of top-down parser built from a
set of mutually recursive procedures (or a non-recursive equivalent)
where each such procedure usually implements one of the
productions of the grammar.
EXAMPLE OF RECURSIVE DESCENT
PARSING• Suppose the grammar given is as follows:
• E->iE’
• E’->+iE’
Program:
E()
{
if(l==‘i’)
{
match(‘i’);
E’();
}
} l=getchar();
E’()
{
if(l==‘+”)
{
match(‘+’);
match(‘i’);
E’();
}
else
return ;
}
Match(char t)
{
if(l==t)
l=getchar();
else
printf(“Error”);
}
Main()
{
E();
If(l==‘$’)
{
printf(“parsing successful”);
}
}
PREDICTIVE LL(1) PARSING
• The first “L” in LL(1) refers to the fact that the input is processed from
left to right.
• The second “L” refers to the fact that LL(1) parsing determines a
leftmost derivation for the input string.
• The “1” in parentheses implies that LL(1) parsing uses only one
symbol of input to predict the next grammar rule that should be used.
• The data structures used by LL(1) are 1. Input buffer 2. Stack
3. Parsing table
• The construction of predictive LL(1) parser is bsed on two
very important functions and those are First and Follow.
• For construction of predictive LL(1) parser we have to follow
the following steps:
• STEP1: computate FIRST and FOLLOW function.
• STEP2: construct predictive parsing table using first and follow
function.
• STEP3: parse the input string with the help of predictive parsing
table
FIRST
• If X is a terminal then First(X) is just X!
• If there is a Production X → ε then add ε to first(X)
• If there is a Production X → Y1Y2..Yk then add first(Y1Y2..Yk)
to first(X)
• First(Y1Y2..Yk) is either
• First(Y1) (if First(Y1) doesn't contain ε)
• OR (if First(Y1) does contain ε) then First (Y1Y2..Yk) is everything in
First(Y1) <except for ε > as well as everything in First(Y2..Yk)
• If First(Y1) First(Y2)..First(Yk) all contain ε then add ε to
First(Y1Y2..Yk) as well.
FOLLOW
• First put $ (the end of input marker) in Follow(S) (S is the
start symbol)
• If there is a production A → aBb, (where a can be a whole
string) then everything in FIRST(b) except for ε is placed in
FOLLOW(B).
• If there is a production A → aB, then everything in
FOLLOW(A) is in FOLLOW(B)
• If there is a production A → aBb, where FIRST(b) contains
ε, then everything in FOLLOW(A) is in FOLLOW(B)
EXAMPLE OF FIRST AND FOLLOW
The Grammar
•E → TE'
•E' → +TE'
•E' → ε
•T → FT'
•T' → *FT'
•T' → ε
•F → (E)
•F → id
PROPERTIES OF LL(1) GRAMMARS
1. No left-recursive grammar is LL(1)
2. No ambiguous grammar is LL(1)
3. Some languages have no LL(1) grammar
4. A ε–free grammar where each alternative expansion for A begins with a distinct terminal is a
simple LL(1) grammar.
Example:
S → aS | a
is not LL(1) because FIRST(aS) = FIRST(a) = { a }
S → aS´
S´ → aS | ε
accepts the same language and is LL(1)
PREDICTIVE PARSING TABLE
Method:
1.∀ production A → α:
α) ∀a ∈ FIRST(α), add A → α to M[A,a]
b) If ε ∈ FIRST(α):
Ι. ∀b ∈ FOLLOW(A), add A → α to M[A,b]
II. If $ ∈ FOLLOW(A), add A → α to M[A,$]
2.Set each undefined entry of M to error
If ∃M[A,a] with multiple entries then G is not LL(1).
EXAMPLE OF PREDICTIVE PARSING
LL(1) TABLE
The given grammar is as follows
•S → E
•E → TE´
•E´ → +E | —E | ε
•T → FT´
•T´ → * T | / T | ε
•F → num | id
BOTTOM UP PARSING
• Bottom-up parsing starts from the leaf nodes of a tree and
works in upward direction till it reaches the root node.
• we start from a sentence and then apply production rules in
reverse manner in order to reach the start symbol.
• Here, parser tries to identify R.H.S of production rule and
replace it by corresponding L.H.S. This activity is known as
reduction.
• Also known as LR parser, where L means tokens are read
from left to right and R means that it constructs rightmost
derivative.
EXAMPLE OF BOTTOM-UP PARSER
• E → T + E | T
• T → int * T | int | (E)
• Consider the string: int * int + int
int * int + int T → int
int * T + int T → int * T
T + int T → int
T + T E → T
T + T E → T
E
SHIFT REDUCE PARSING
• Bottom-up parsing uses two kinds of actions: 1.Shift 2.Reduce
• Shift: Move | one place to the right , Shifts a terminal to the left string
ABC|xyz ABCx|yz⇒
• Reduce: Apply an inverse production at the right end of the left string
If A → xy is a production, then Cbxy|ijk CbA|ijk⇒
EXAMPLE OF SHIT REDUCE PARSING
• |int * int + int shift
• int | * int + int shift
• int * | int + int shift
• int * int | + int reduce T → int
• int * T | + int reduce T → int * T
• T | + int shift
• T + | int shift
• T + int | reduce T → int
• T + T | reduce E → T
• T + E | reduce E → T + E
• E |
OPERATOR PRECEDENCE PARSING
• Operator grammars have the property that no production right side
is empty or has two adjacent nonterminals.
• This property enables the implementation of efficient operator-
precedence parsers.
• These parser rely on the following three precedence relations:
Relation Meaning
a <· b a yields precedence to b
a =· b a has the same precedence as b
a ·> b a takes precedence over b
• These operator precedence relations allow to delimit the handles in
the right sentential forms: <· marks the left end, =· appears in
• the interior of the handle, and ·> marks the right end.
• . Suppose that $ is the end of the string, Then for all terminals we can
write: $ <· b and b ·> $
• If we remove all nonterminals and place the correct precedence
relation:<·, =·, ·> between the remaining terminals, there remain
strings that can be analyzed by easily developed parser.
EXAMPLE OF OPERATOR
PRECEDENCE PARSING
id + * $
id ·> ·> ·>
+ <· ·> <· ·>
* <· ·> ·> ·>
$ <· <· <· ·>
For example, the following operator precedence relations
can
be introduced for simple expressions:
Example: The input string:
id1
+ id2
* id3
after inserting precedence relations becomes
$ <· id1
·> + <· id2
·> * <· id3
·> $
THANK YOU

More Related Content

What's hot (20)

PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PPTX
Lexical Analysis - Compiler Design
Akhil Kaushik
 
DOC
Parsing
ShrikantSharma86
 
PDF
Syntax directed translation
Akshaya Arunan
 
PPTX
Types of Parser
SomnathMore3
 
PPTX
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
PDF
Symbol table in compiler Design
Kuppusamy P
 
PPTX
Decision properties of reular languages
SOMNATHMORE2
 
PPTX
Dynamic programming
Yıldırım Tam
 
PPT
Np cooks theorem
Narayana Galla
 
PPT
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
PPT
Compiler Design
Mir Majid
 
PPTX
Unit1 principle of programming language
Vasavi College of Engg
 
PPTX
Relational algebra ppt
GirdharRatne
 
PPTX
Performance analysis(Time & Space Complexity)
swapnac12
 
PPTX
Syntax Analysis in Compiler Design
MAHASREEM
 
PPTX
Context free grammar
Mohammad Ilyas Malik
 
PDF
Lecture 01 introduction to compiler
Iffat Anjum
 
PPTX
Compiler design
Thakur Ganeshsingh Thakur
 
PPTX
Asymptotic Notations
Rishabh Soni
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Syntax directed translation
Akshaya Arunan
 
Types of Parser
SomnathMore3
 
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Symbol table in compiler Design
Kuppusamy P
 
Decision properties of reular languages
SOMNATHMORE2
 
Dynamic programming
Yıldırım Tam
 
Np cooks theorem
Narayana Galla
 
Sum of subsets problem by backtracking 
Hasanain Alshadoodee
 
Compiler Design
Mir Majid
 
Unit1 principle of programming language
Vasavi College of Engg
 
Relational algebra ppt
GirdharRatne
 
Performance analysis(Time & Space Complexity)
swapnac12
 
Syntax Analysis in Compiler Design
MAHASREEM
 
Context free grammar
Mohammad Ilyas Malik
 
Lecture 01 introduction to compiler
Iffat Anjum
 
Compiler design
Thakur Ganeshsingh Thakur
 
Asymptotic Notations
Rishabh Soni
 

Similar to Parsing (20)

PPTX
3. Syntax Analyzer.pptx
Mattupallipardhu
 
PDF
Compiler unit 2&3
BBDITM LUCKNOW
 
PPT
Cd2 [autosaved]
BBDITM LUCKNOW
 
PDF
Syntax Analysis_73 pages notes IIIT Manipur.pdf
venkyvenky7674886497
 
PPTX
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
Padamata Rameshbabu
 
PPT
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
Padamata Rameshbabu
 
PDF
Syntactic analysis in NLP
kartikaVashisht
 
PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
PPT
PARSING.ppt
ayyankhanna6480086
 
PDF
ACD-U2-TopDown..pdf it hhepls inall the the
akshay04282004
 
PPT
Top down parsing
ASHOK KUMAR REDDY
 
PPTX
Parsing (Automata)
ROOP SAGAR
 
PDF
Theory of automata and formal language lab manual
Nitesh Dubey
 
PPTX
BOTTOM_UP_Parsing techniques_compiler design5.pptx
salaja2
 
PPTX
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
PPT
Lexical 2
ASHOK KUMAR REDDY
 
PPTX
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
PDF
Left factor put
siet_pradeep18
 
PPT
Compiler Design_Syntax Analyzer_Bottom Up Parsers.ppt
RushaliDeshmukh2
 
3. Syntax Analyzer.pptx
Mattupallipardhu
 
Compiler unit 2&3
BBDITM LUCKNOW
 
Cd2 [autosaved]
BBDITM LUCKNOW
 
Syntax Analysis_73 pages notes IIIT Manipur.pdf
venkyvenky7674886497
 
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
Padamata Rameshbabu
 
51114.-Compiler-Design-Syntax-Analysis-Top-down.ppt
Padamata Rameshbabu
 
Syntactic analysis in NLP
kartikaVashisht
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
PARSING.ppt
ayyankhanna6480086
 
ACD-U2-TopDown..pdf it hhepls inall the the
akshay04282004
 
Top down parsing
ASHOK KUMAR REDDY
 
Parsing (Automata)
ROOP SAGAR
 
Theory of automata and formal language lab manual
Nitesh Dubey
 
BOTTOM_UP_Parsing techniques_compiler design5.pptx
salaja2
 
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
Left factor put
siet_pradeep18
 
Compiler Design_Syntax Analyzer_Bottom Up Parsers.ppt
RushaliDeshmukh2
 
Ad

More from khush_boo31 (6)

PPTX
L attribute in compiler design
khush_boo31
 
PPTX
Classification of data mart
khush_boo31
 
PPT
statement interface
khush_boo31
 
PPT
context free language
khush_boo31
 
PPTX
parameter passing in c#
khush_boo31
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
L attribute in compiler design
khush_boo31
 
Classification of data mart
khush_boo31
 
statement interface
khush_boo31
 
context free language
khush_boo31
 
parameter passing in c#
khush_boo31
 
Knapsack problem using dynamic programming
khush_boo31
 
Ad

Recently uploaded (20)

PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
Precedence and Associativity in C prog. language
Mahendra Dheer
 
PPTX
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PPTX
quantum computing transition from classical mechanics.pptx
gvlbcy
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
Precedence and Associativity in C prog. language
Mahendra Dheer
 
filteration _ pre.pptx 11111110001.pptx
awasthivaibhav825
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Inventory management chapter in automation and robotics.
atisht0104
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
Jual GPS Geodetik CHCNAV i93 IMU-RTK Lanjutan dengan Survei Visual
Budi Minds
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
quantum computing transition from classical mechanics.pptx
gvlbcy
 

Parsing

  • 2. DEFINITION OF PARSING • A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. • A parsertakes input in the form of a sequence of tokens or program instructions and usually builds a data structure in the form of a parse tree or an abstract syntax tree.
  • 4. •In the compiler model, the parser obtains a string of tokens from the lexical analyser, 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. • It collects sufficient number of tokens anf builds a parse tree
  • 6. • There are basically two types of parser: • Top-down parser: • starts at the root of derivation tree and fills in • picks a production and tries to match the input • may require backtracking • some grammars are backtrack-free (predictive) • Bottom-up parser: • starts at the leaves and fills in • starts in a state valid for legal first tokens • uses a stack to store both state and sentential forms
  • 7. TOP DOWN PARSING • A top-down parser starts with the root of the parse tree, labeled with the start or goal symbol of the grammar. • To build a parse, it repeats the following steps until the fringe of the parse tree matches the input string • STEP1: At a node labeled A, select a production A → α and construct the appropriate child for each symbol of α • STEP2: When a terminal is added to the fringe that doesn’t match the input string, backtrack • STEP3: Find the next node to be expanded. • The key is selecting the right production in step 1
  • 8. EXAMPLE FOR TOP DOWN PARSING • Supppose the given production rules are as follows: • S-> aAd|aB • A-> b|c • B->ccd
  • 9. PROBLEMS WITH TOPDOWN PARSING 1) BACKTRACKING • Backtracing is a technique in which for expansion of non-terminal symbol we choose one 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.
  • 10. EXAMPLE OF BACKTRACKING • Suppose the given production rules are as follows: • S->cAd • A->a|ab
  • 11. • 2) LEFT RECURSION • Left recursion is a case when the left-most non-terminal in a production of a non-terminal is the non-terminal itself( direct left recursion ) or through some other non-terminal definitions, rewrites to the non-terminal again(indirect left recursion). Consider these examples - • (1) A -> Aq (direct) • (2) A -> Bq B -> Ar (indirect) • Left recursion has to be removed if the parser performs top-down parsing
  • 12. REMOVING LEFT RECURSION • To eliminate left recursion we need to modify the grammar. Let, G be a grammar having a production rule with left recursion • A-> Aa • A->B • Thus, we eliminate left recursion by rewriting the production rule as: • A->BA’ • A’->aA’ • A’->c
  • 13. 3) LEFT FACTORING •Left factoring is removing the common left factor that appears in two productions of the same non-terminal. It is done to avoid back-tracing by the parser. Suppose the parser has a look-ahead ,consider this example- •A -> qB | qC where A,B,C are non-terminals and q is a sentence. In this case, the parser will be confused as to which of the two productions to choose and it might have to back-trace. After left factoring, the grammar is converted to- •A -> qD •D -> B | C
  • 14. RECURSIVE DESCENT PARSING • A recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure usually implements one of the productions of the grammar.
  • 15. EXAMPLE OF RECURSIVE DESCENT PARSING• Suppose the grammar given is as follows: • E->iE’ • E’->+iE’ Program: E() { if(l==‘i’) { match(‘i’); E’(); } } l=getchar();
  • 18. PREDICTIVE LL(1) PARSING • The first “L” in LL(1) refers to the fact that the input is processed from left to right. • The second “L” refers to the fact that LL(1) parsing determines a leftmost derivation for the input string. • The “1” in parentheses implies that LL(1) parsing uses only one symbol of input to predict the next grammar rule that should be used. • The data structures used by LL(1) are 1. Input buffer 2. Stack 3. Parsing table
  • 19. • The construction of predictive LL(1) parser is bsed on two very important functions and those are First and Follow. • For construction of predictive LL(1) parser we have to follow the following steps: • STEP1: computate FIRST and FOLLOW function. • STEP2: construct predictive parsing table using first and follow function. • STEP3: parse the input string with the help of predictive parsing table
  • 20. FIRST • If X is a terminal then First(X) is just X! • If there is a Production X → ε then add ε to first(X) • If there is a Production X → Y1Y2..Yk then add first(Y1Y2..Yk) to first(X) • First(Y1Y2..Yk) is either • First(Y1) (if First(Y1) doesn't contain ε) • OR (if First(Y1) does contain ε) then First (Y1Y2..Yk) is everything in First(Y1) <except for ε > as well as everything in First(Y2..Yk) • If First(Y1) First(Y2)..First(Yk) all contain ε then add ε to First(Y1Y2..Yk) as well.
  • 21. FOLLOW • First put $ (the end of input marker) in Follow(S) (S is the start symbol) • If there is a production A → aBb, (where a can be a whole string) then everything in FIRST(b) except for ε is placed in FOLLOW(B). • If there is a production A → aB, then everything in FOLLOW(A) is in FOLLOW(B) • If there is a production A → aBb, where FIRST(b) contains ε, then everything in FOLLOW(A) is in FOLLOW(B)
  • 22. EXAMPLE OF FIRST AND FOLLOW The Grammar •E → TE' •E' → +TE' •E' → ε •T → FT' •T' → *FT' •T' → ε •F → (E) •F → id
  • 23. PROPERTIES OF LL(1) GRAMMARS 1. No left-recursive grammar is LL(1) 2. No ambiguous grammar is LL(1) 3. Some languages have no LL(1) grammar 4. A ε–free grammar where each alternative expansion for A begins with a distinct terminal is a simple LL(1) grammar. Example: S → aS | a is not LL(1) because FIRST(aS) = FIRST(a) = { a } S → aS´ S´ → aS | ε accepts the same language and is LL(1)
  • 24. PREDICTIVE PARSING TABLE Method: 1.∀ production A → α: α) ∀a ∈ FIRST(α), add A → α to M[A,a] b) If ε ∈ FIRST(α): Ι. ∀b ∈ FOLLOW(A), add A → α to M[A,b] II. If $ ∈ FOLLOW(A), add A → α to M[A,$] 2.Set each undefined entry of M to error If ∃M[A,a] with multiple entries then G is not LL(1).
  • 25. EXAMPLE OF PREDICTIVE PARSING LL(1) TABLE The given grammar is as follows •S → E •E → TE´ •E´ → +E | —E | ε •T → FT´ •T´ → * T | / T | ε •F → num | id
  • 26. BOTTOM UP PARSING • Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it reaches the root node. • we start from a sentence and then apply production rules in reverse manner in order to reach the start symbol. • Here, parser tries to identify R.H.S of production rule and replace it by corresponding L.H.S. This activity is known as reduction. • Also known as LR parser, where L means tokens are read from left to right and R means that it constructs rightmost derivative.
  • 27. EXAMPLE OF BOTTOM-UP PARSER • E → T + E | T • T → int * T | int | (E) • Consider the string: int * int + int int * int + int T → int int * T + int T → int * T T + int T → int T + T E → T T + T E → T E
  • 28. SHIFT REDUCE PARSING • Bottom-up parsing uses two kinds of actions: 1.Shift 2.Reduce • Shift: Move | one place to the right , Shifts a terminal to the left string ABC|xyz ABCx|yz⇒ • Reduce: Apply an inverse production at the right end of the left string If A → xy is a production, then Cbxy|ijk CbA|ijk⇒
  • 29. EXAMPLE OF SHIT REDUCE PARSING • |int * int + int shift • int | * int + int shift • int * | int + int shift • int * int | + int reduce T → int • int * T | + int reduce T → int * T • T | + int shift • T + | int shift • T + int | reduce T → int • T + T | reduce E → T • T + E | reduce E → T + E • E |
  • 30. OPERATOR PRECEDENCE PARSING • Operator grammars have the property that no production right side is empty or has two adjacent nonterminals. • This property enables the implementation of efficient operator- precedence parsers. • These parser rely on the following three precedence relations: Relation Meaning a <· b a yields precedence to b a =· b a has the same precedence as b a ·> b a takes precedence over b
  • 31. • These operator precedence relations allow to delimit the handles in the right sentential forms: <· marks the left end, =· appears in • the interior of the handle, and ·> marks the right end. • . Suppose that $ is the end of the string, Then for all terminals we can write: $ <· b and b ·> $ • If we remove all nonterminals and place the correct precedence relation:<·, =·, ·> between the remaining terminals, there remain strings that can be analyzed by easily developed parser.
  • 32. EXAMPLE OF OPERATOR PRECEDENCE PARSING id + * $ id ·> ·> ·> + <· ·> <· ·> * <· ·> ·> ·> $ <· <· <· ·> For example, the following operator precedence relations can be introduced for simple expressions: Example: The input string: id1 + id2 * id3 after inserting precedence relations becomes $ <· id1 ·> + <· id2 ·> * <· id3 ·> $