SlideShare a Scribd company logo
8
Most read
13
Most read
17
Most read
Welcome My Presentation
Md.Riazul Islam
CE-14047
Introduction to LR Parsing: Simple LR
• The most prevalent type of bottom-up parser today is based on a
concept called LR(k) parsing; the "L" is for left-to-right scanning of
the input, the "R" for constructing a rightmost derivation in reverse,
and the k for the number of input symbols of lookahead that are used
in making parsing decisions. The cases k = 0 or k = 1 are of practical
interest, and we shall only consider LR parsers with k 5 1 here. When
(k) is omitted, k is assumed to be 1
• This section introduces the basic concepts of LR parsing and the
easiest method for constructing shift-reduce parsers, called "simple
LR" (or SLR, for short). Some familiarity with the basic concepts is
helpful even if the LR parser itself is constructed using an automatic
parser generator. We begin with "items" and "parser states;" the
diagnostic output from an LR parser generator typically includes
parser states, which can be used to isolate the sources of parsing
conflicts.
Examples:
• Example : Consider the augmented expression grammar:
• E' + E
• E -+ E+T (T
• T + T*F 1 F
• E -+ (E) I id
• If I is the set of one item {[E' -+ .El}, then CLOSURE(I) contains the set
of items I. in Fig. 4.3
Compiler Design LR parsing SLR ,LALR CLR
• To see how the closure is computed, E' -+ -E is put in CLOSURE(I) by
rule (1). Since there is an E immediately to the right of a dot, we add
the E-productions with dots at the left ends: E -+ .E + T and E -+ ST.
Now there is a T immediately to the right of a dot in the latter item,
so we add T -+ ST * F and T -+ .F. Next, the F to the right of a dot
forces us to add F + .(E) and F -+ -id, but no other items need to be
added
• 1. Kernel items: the initial item, S' -+ .S, and all items whose dots are
not at the left end.
• 2. Nonkernel items: all items with their dots at the left end, except
for S' -+ .S.
Table
• The codes for the actions are:
• 1. si means shift and stack state i,
• 2. rj means reduce by the production numbered j,
• 3. acc means accept,
• 4. blank means error.
• Note that the value of GOTO[S, a] for terminal a is found in the ACTION
• field connected with the shift action on input a for state s. The GOTO field
• gives GOTO[S, A] for nonterminals A. Although we have not yet explained
how the entries for Fig. 4.37 were selected, we shall deal with this issue
short
More Powerful LR Parsers:
• we shall extend the previous LR parsing techniques to use one symbol of
lookahead on the input. There are two different methods:
• 1. The "canonical-LR" or just "LR" method, which makes full use of the
lookahead symbol(s). This method uses a large set of items, called the
LR(1) items.
• 2. The "lookahead-LR" or "LALR" method, which is based on the LR(0) sets
of items, and has many fewer states than typical parsers based on the LR(1)
items. By carefully introducing lookaheads into the LR(0) items, we can
handle many more grammars with the LALR method than with the SLR
method, and build parsing tables that are no bigger than the SLR tables.
LALR is the method of choice in most situations.
Canonical LR(1) Items :
• We shall now present the most general technique for constructing an
LR parsing table from a grammar. Recall that in the SLR method, state
i calls for reduction by A -+ a if the set of items Ii contains item [A --+
as] and a is in FOLLOW(A). In some situations, however, when state i
appears on top of the stack, the viable prefix pa on the stack is such
that PA cannot be followed by a in any right-sentential form. Thus,
the reduction by A -+ a should be invalid on input a.
Examples:
• Example : Consider the following augmented grammar.
• We begin by computing the closure of {[St -+ -S, $1). To close, we
match the item [St -+ -S, $1 with the item [A -+ a-BP, a] in the
procedure CLOSURE.
• That is, A = St, a = e, B = S, P = e, and a = $. Function CLOSURE tells us
to add [B -+ .y, b] for each production B -+ y and terminal b in
FIRST(P~). In terms of the present grammar, B -+ y must be S -+ CC,
and since ,8 is c and a is $, b may only be $. Thus we add [S -+ .CC, $1
Compiler Design LR parsing SLR ,LALR CLR
• We continue to compute the closure by adding all items [C -+ .y, b] for b in
FIRST(C$). That is, matching [S -+ .CC, $1 against [A -+ a.B,O, a], we have
• A = S, a = 6, B = C, p = C, and a = $. Since C does not derive the empty
string, FIRST(C$) = FIRST(C). Since FIRST@) contains terminals c and d, we
add items [C -+ -cC, c], [C -+ .cC, dl, [C -t -d, c] and [C -+ -d, dl. None of the
new items has a nonterminal immediately to the right of the dot, so we
have
• completed our first set of LR(1) items. The initial set of items is I0:
• S->.S,$
• S -> .CC, $
• C -> .cC, c/d
• C -> .d, c/d
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Constructing LALR Parsing Table
• We now introduce our last parser construction method, the LALR
(Eoolcahead- LR) technique. This method is often used in practice,
because the tables obtained by it are considerably smaller than the
canonical LR tables, yet most common syntactic constructs of
programming languages can be expressed conveniently by an LALR
grammar. The same is almost true for SLR grammars, but there are a
few constructs that cannot be conveniently handled by SLR
techniques
Example
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR

More Related Content

What's hot (20)

PPT
Bottom - Up Parsing
kunj desai
 
PPTX
Top down and botttom up Parsing
Gerwin Ocsena
 
PPTX
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
PDF
Bottom up parser
Akshaya Arunan
 
PDF
Ch3 4 regular expression and grammar
meresie tesfay
 
PPTX
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
PPTX
Three Address code
Pooja Dixit
 
PPTX
Context free grammar
Mohammad Ilyas Malik
 
PPTX
Graph coloring using backtracking
shashidharPapishetty
 
PDF
Formal Languages and Automata Theory Unit 1
Srimatre K
 
PPT
Backtracking
Pranay Meshram
 
PPTX
Compiler construction tools
Akhil Kaushik
 
PPT
Parsing
khush_boo31
 
PPTX
COMPILER DESIGN
Vetukurivenkatashiva
 
PPTX
Specification-of-tokens
Dattatray Gandhmal
 
PPTX
Input-Buffering
Dattatray Gandhmal
 
PPTX
Dag representation of basic blocks
Jothi Lakshmi
 
PPTX
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 
PPTX
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
PPTX
Compiler design syntax analysis
Richa Sharma
 
Bottom - Up Parsing
kunj desai
 
Top down and botttom up Parsing
Gerwin Ocsena
 
Parsing LL(1), SLR, LR(1)
Nitin Mohan Sharma
 
Bottom up parser
Akshaya Arunan
 
Ch3 4 regular expression and grammar
meresie tesfay
 
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Three Address code
Pooja Dixit
 
Context free grammar
Mohammad Ilyas Malik
 
Graph coloring using backtracking
shashidharPapishetty
 
Formal Languages and Automata Theory Unit 1
Srimatre K
 
Backtracking
Pranay Meshram
 
Compiler construction tools
Akhil Kaushik
 
Parsing
khush_boo31
 
COMPILER DESIGN
Vetukurivenkatashiva
 
Specification-of-tokens
Dattatray Gandhmal
 
Input-Buffering
Dattatray Gandhmal
 
Dag representation of basic blocks
Jothi Lakshmi
 
Finite automata-for-lexical-analysis
Dattatray Gandhmal
 
Automata theory - Push Down Automata (PDA)
Akila Krishnamoorthy
 
Compiler design syntax analysis
Richa Sharma
 

Similar to Compiler Design LR parsing SLR ,LALR CLR (20)

PPTX
Bottom-up parsingn_CompilationTechnics.pptx
FeliciaHandoyo
 
PPT
Presentation mam saima kanwal
Abdul Qadir Pattal
 
PPTX
Compilers section 4.7
myasir16
 
PDF
Lecture11 syntax analysis_7
Mahesh Kumar Chelimilla
 
PPT
LR-Parsing.ppt
BapanKar2
 
PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
DOCX
How do you learn Bottom-up Parsing Algorithm.docx
Tri AB Refardi
 
PPTX
LR(1) and SLR(1) parsing
R Islam
 
PPT
CC Week 07-08.ppt
KamranAli649587
 
PDF
Bottomupparser
Royalzig Luxury Furniture
 
PDF
Bottomupparser
Royalzig Luxury Furniture
 
PDF
Bottomupparser
Royalzig Luxury Furniture
 
PPTX
CLR AND LALR PARSER
Akila Krishnamoorthy
 
PPTX
10-SLR parser practice problems-02-06-2023.pptx
venkatapranaykumarGa
 
PPT
Chapter Five(2)
bolovv
 
PPTX
LR PARSE.pptx
Aishwarya SenthilNathan
 
PDF
Syntax Analysis (Bottom-Up Parser) PPTs for Third Year Computer Science Engin...
DrRajurkarArchanaMil
 
PDF
Syntax Analysis ( Bottom up Parser) PPTs
DrRajurkarArchanaMil
 
PPTX
Compiler Design Bottom Up Parsing Technique S
poojasharmabu
 
Bottom-up parsingn_CompilationTechnics.pptx
FeliciaHandoyo
 
Presentation mam saima kanwal
Abdul Qadir Pattal
 
Compilers section 4.7
myasir16
 
Lecture11 syntax analysis_7
Mahesh Kumar Chelimilla
 
LR-Parsing.ppt
BapanKar2
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
How do you learn Bottom-up Parsing Algorithm.docx
Tri AB Refardi
 
LR(1) and SLR(1) parsing
R Islam
 
CC Week 07-08.ppt
KamranAli649587
 
CLR AND LALR PARSER
Akila Krishnamoorthy
 
10-SLR parser practice problems-02-06-2023.pptx
venkatapranaykumarGa
 
Chapter Five(2)
bolovv
 
Syntax Analysis (Bottom-Up Parser) PPTs for Third Year Computer Science Engin...
DrRajurkarArchanaMil
 
Syntax Analysis ( Bottom up Parser) PPTs
DrRajurkarArchanaMil
 
Compiler Design Bottom Up Parsing Technique S
poojasharmabu
 
Ad

More from Riazul Islam (7)

PPTX
Introduction wireless communication network
Riazul Islam
 
PDF
Data link control in computer networks
Riazul Islam
 
PDF
Optical communication in communication engineering
Riazul Islam
 
PDF
Channel capacity and coding in communication engineering
Riazul Islam
 
PPTX
Algorithm in computer science
Riazul Islam
 
PPTX
Finite Automata in compiler design
Riazul Islam
 
PPTX
Regular Expression in Compiler design
Riazul Islam
 
Introduction wireless communication network
Riazul Islam
 
Data link control in computer networks
Riazul Islam
 
Optical communication in communication engineering
Riazul Islam
 
Channel capacity and coding in communication engineering
Riazul Islam
 
Algorithm in computer science
Riazul Islam
 
Finite Automata in compiler design
Riazul Islam
 
Regular Expression in Compiler design
Riazul Islam
 
Ad

Recently uploaded (20)

PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 

Compiler Design LR parsing SLR ,LALR CLR

  • 2. Introduction to LR Parsing: Simple LR • The most prevalent type of bottom-up parser today is based on a concept called LR(k) parsing; the "L" is for left-to-right scanning of the input, the "R" for constructing a rightmost derivation in reverse, and the k for the number of input symbols of lookahead that are used in making parsing decisions. The cases k = 0 or k = 1 are of practical interest, and we shall only consider LR parsers with k 5 1 here. When (k) is omitted, k is assumed to be 1
  • 3. • This section introduces the basic concepts of LR parsing and the easiest method for constructing shift-reduce parsers, called "simple LR" (or SLR, for short). Some familiarity with the basic concepts is helpful even if the LR parser itself is constructed using an automatic parser generator. We begin with "items" and "parser states;" the diagnostic output from an LR parser generator typically includes parser states, which can be used to isolate the sources of parsing conflicts.
  • 4. Examples: • Example : Consider the augmented expression grammar: • E' + E • E -+ E+T (T • T + T*F 1 F • E -+ (E) I id • If I is the set of one item {[E' -+ .El}, then CLOSURE(I) contains the set of items I. in Fig. 4.3
  • 6. • To see how the closure is computed, E' -+ -E is put in CLOSURE(I) by rule (1). Since there is an E immediately to the right of a dot, we add the E-productions with dots at the left ends: E -+ .E + T and E -+ ST. Now there is a T immediately to the right of a dot in the latter item, so we add T -+ ST * F and T -+ .F. Next, the F to the right of a dot forces us to add F + .(E) and F -+ -id, but no other items need to be added
  • 7. • 1. Kernel items: the initial item, S' -+ .S, and all items whose dots are not at the left end. • 2. Nonkernel items: all items with their dots at the left end, except for S' -+ .S.
  • 9. • The codes for the actions are: • 1. si means shift and stack state i, • 2. rj means reduce by the production numbered j, • 3. acc means accept, • 4. blank means error. • Note that the value of GOTO[S, a] for terminal a is found in the ACTION • field connected with the shift action on input a for state s. The GOTO field • gives GOTO[S, A] for nonterminals A. Although we have not yet explained how the entries for Fig. 4.37 were selected, we shall deal with this issue short
  • 10. More Powerful LR Parsers: • we shall extend the previous LR parsing techniques to use one symbol of lookahead on the input. There are two different methods: • 1. The "canonical-LR" or just "LR" method, which makes full use of the lookahead symbol(s). This method uses a large set of items, called the LR(1) items. • 2. The "lookahead-LR" or "LALR" method, which is based on the LR(0) sets of items, and has many fewer states than typical parsers based on the LR(1) items. By carefully introducing lookaheads into the LR(0) items, we can handle many more grammars with the LALR method than with the SLR method, and build parsing tables that are no bigger than the SLR tables. LALR is the method of choice in most situations.
  • 11. Canonical LR(1) Items : • We shall now present the most general technique for constructing an LR parsing table from a grammar. Recall that in the SLR method, state i calls for reduction by A -+ a if the set of items Ii contains item [A --+ as] and a is in FOLLOW(A). In some situations, however, when state i appears on top of the stack, the viable prefix pa on the stack is such that PA cannot be followed by a in any right-sentential form. Thus, the reduction by A -+ a should be invalid on input a.
  • 12. Examples: • Example : Consider the following augmented grammar. • We begin by computing the closure of {[St -+ -S, $1). To close, we match the item [St -+ -S, $1 with the item [A -+ a-BP, a] in the procedure CLOSURE. • That is, A = St, a = e, B = S, P = e, and a = $. Function CLOSURE tells us to add [B -+ .y, b] for each production B -+ y and terminal b in FIRST(P~). In terms of the present grammar, B -+ y must be S -+ CC, and since ,8 is c and a is $, b may only be $. Thus we add [S -+ .CC, $1
  • 14. • We continue to compute the closure by adding all items [C -+ .y, b] for b in FIRST(C$). That is, matching [S -+ .CC, $1 against [A -+ a.B,O, a], we have • A = S, a = 6, B = C, p = C, and a = $. Since C does not derive the empty string, FIRST(C$) = FIRST(C). Since FIRST@) contains terminals c and d, we add items [C -+ -cC, c], [C -+ .cC, dl, [C -t -d, c] and [C -+ -d, dl. None of the new items has a nonterminal immediately to the right of the dot, so we have • completed our first set of LR(1) items. The initial set of items is I0: • S->.S,$ • S -> .CC, $ • C -> .cC, c/d • C -> .d, c/d
  • 19. Constructing LALR Parsing Table • We now introduce our last parser construction method, the LALR (Eoolcahead- LR) technique. This method is often used in practice, because the tables obtained by it are considerably smaller than the canonical LR tables, yet most common syntactic constructs of programming languages can be expressed conveniently by an LALR grammar. The same is almost true for SLR grammars, but there are a few constructs that cannot be conveniently handled by SLR techniques