SlideShare a Scribd company logo
1 of 14
1
Compiler Construction
(CS-636)
Muhammad Bilal Bashir
UIIT, Rawalpindi
Outline
1. Bottom-Up Parsing
2. LR(0) Items
3. Finite Automata of LR(0) Items
4. LR(0) Parsing
5. LR(0) Parsing Table
6. Summary
2
Bottom-Up Parsing
Lecture: 15-16
3
Bottom-Up Parsing
 A bottom-up parser uses an explicit stack to perform
a parse, similar to LL(1) parser
 The parsing stack will contain both tokens and
nonterminals and also some extra state information
 The stack is empty at the beginning of a bottom-up
parse and will contain the start symbol at the end of
a successful parse
 A bottom-up parse has two possible actions;
1. Shift a terminal from front of the input to top of the stack
2. Reduce a string α at the top of the stack to a nonterminal
A, given the BNF choice A → α
4
Bottom-Up Parsing (Continue…)
 A bottom-up parser is sometimes called a shift-
reduce parser
 Shift actions are indicated by writing the word shift
 Reduce actions are indicated by writing the word
reduce and giving the BNF choice used in reduction
 One further feature of bottom-up parsing is that
grammars are always augmented with a new start
symbol
 Suppose S was the start symbol of the grammar;
S/
→ S
5
Bottom-Up Parsing (Continue…)
6
 Grammar: S’ → S
S → ( S ) S | ε
 Input: ()
No Parsing Stack Input Action
1 $ ( ) $ Shift
2 $ ( ) $ reduce S → ε
3 $ ( S ) $ shift
4 $ ( S ) $ reduce S → ε
5 $ ( S ) S $ reduce S → ( S ) S
6 $ S $ reduce S’ → S
7 $ S’ $ accept
Bottom-Up Parsing (Continue…)
 A bottom-up parser can shift input symbols onto the
stack until it determines what action to perform
 The parser may need to look deeper into the stack then just
the top in order to determine what action to perform
 Thus bottom-up parsing requires arbitrary ‘stack
lookahead’, which is not as serious as input
lookahead because parser builds the stack and can
arrange for the appropriate information to be
available
 The mechanism that will do this is a deterministic
finite automaton of “items”
7
LR(0) Items
 The LR(0) item (or just item) of a CFG is a
production choice with a distinguished position on its
right hand side
 We use period “.” symbol to indicate distinguished position
 If A → α is a production choice, and if β and γ are
any two strings of symbols (including ε) such that βγ
= α, then A → β.γ is an LR(0) item
 Example:
 Write down LR(0) items for the following grammar:
S’ → S
S → ( S ) S | ε
8
Finite Automata of LR(0) Items
 In first pass a NFA is generated for LR(0) items
 In second pass NFA is converted into DFA of LR(0)
items
 States in the DFA are numbered
 The transitions in symbols can be terminals,
nonterminals, and ε (only in NFA)
 There is an initial state
 There is one or more accepting states depending
upon the grammar
9
Construction of NFA &
DFA of LR(0) Items
S’ → S
S → ( S ) S | ε
10
LR(0) Parsing
 LR(0) is a bottom-up parsing algorithm
 The “L” indicates the input will be processed from left to
right
 The “R” indicates rightmost derivation is produced
 The “0” means no lookahead token is used
 LR(0) uses DFA of items to perform parsing
 The basic actions remain the same including shift
and reduce
11
LR(0) Parsing (Continue…)
12
 Grammar: S’ → S
S → ( S ) S | ε
 Input: ()
No Parsing Stack Input Action
1 $ 0 ( ) $ shift
2 $ 0 ( 2 ) $ reduce S → ε
3 $ 0 ( 2 S 3 ) $ shift
4 $ 0 ( 2 S 3 ) 4 $ reduce S → ε
5 $ 0 ( 2 S 3 ) 4 S 5 $ reduce S → ( S ) S
6 $ 0 S 1 $ accept
LR(0) Parsing Table
 A LR(0) parsing table can be constructed against
the DFA of items of a given grammar with columns
 State
 Action
 Rule
 Input
 Goto
13
14
Summary
Any Questions?

More Related Content

What's hot

Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7myasir16
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Skipl List implementation - Part 1
Skipl List implementation - Part 1Skipl List implementation - Part 1
Skipl List implementation - Part 1Amrith Krishna
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Iffat Anjum
 
Csa stack
Csa stackCsa stack
Csa stackPCTE
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptxsandeep54552
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)ROOP SAGAR
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationIIUM
 
Circular linked list
Circular linked listCircular linked list
Circular linked listdchuynh
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointerskinan keshkeh
 

What's hot (20)

Ch4b
Ch4bCh4b
Ch4b
 
Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Skipl List implementation - Part 1
Skipl List implementation - Part 1Skipl List implementation - Part 1
Skipl List implementation - Part 1
 
The Stack And Recursion
The Stack And RecursionThe Stack And Recursion
The Stack And Recursion
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Csa stack
Csa stackCsa stack
Csa stack
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
 
(Ai lisp)
(Ai lisp)(Ai lisp)
(Ai lisp)
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Stacks in Data Structure
Stacks in Data StructureStacks in Data Structure
Stacks in Data Structure
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declaration
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers2Bytesprog2 course_2014_c5_pointers
2Bytesprog2 course_2014_c5_pointers
 
Dounly linked list
Dounly linked listDounly linked list
Dounly linked list
 

Similar to Lecture 15 16 (20)

compiler design.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdf
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
BOTTOM UP PARSING GROUP 3.pptx
BOTTOM UP PARSING GROUP 3.pptxBOTTOM UP PARSING GROUP 3.pptx
BOTTOM UP PARSING GROUP 3.pptx
 
LR(0) Parsing.pptx
LR(0) Parsing.pptxLR(0) Parsing.pptx
LR(0) Parsing.pptx
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptx
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Ch5b.pdf
Ch5b.pdfCh5b.pdf
Ch5b.pdf
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
Parsing
ParsingParsing
Parsing
 
LR Parsing
LR ParsingLR Parsing
LR Parsing
 
10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx10-SLR parser practice problems-02-06-2023.pptx
10-SLR parser practice problems-02-06-2023.pptx
 

Recently uploaded

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

Lecture 15 16

  • 2. Outline 1. Bottom-Up Parsing 2. LR(0) Items 3. Finite Automata of LR(0) Items 4. LR(0) Parsing 5. LR(0) Parsing Table 6. Summary 2
  • 4. Bottom-Up Parsing  A bottom-up parser uses an explicit stack to perform a parse, similar to LL(1) parser  The parsing stack will contain both tokens and nonterminals and also some extra state information  The stack is empty at the beginning of a bottom-up parse and will contain the start symbol at the end of a successful parse  A bottom-up parse has two possible actions; 1. Shift a terminal from front of the input to top of the stack 2. Reduce a string α at the top of the stack to a nonterminal A, given the BNF choice A → α 4
  • 5. Bottom-Up Parsing (Continue…)  A bottom-up parser is sometimes called a shift- reduce parser  Shift actions are indicated by writing the word shift  Reduce actions are indicated by writing the word reduce and giving the BNF choice used in reduction  One further feature of bottom-up parsing is that grammars are always augmented with a new start symbol  Suppose S was the start symbol of the grammar; S/ → S 5
  • 6. Bottom-Up Parsing (Continue…) 6  Grammar: S’ → S S → ( S ) S | ε  Input: () No Parsing Stack Input Action 1 $ ( ) $ Shift 2 $ ( ) $ reduce S → ε 3 $ ( S ) $ shift 4 $ ( S ) $ reduce S → ε 5 $ ( S ) S $ reduce S → ( S ) S 6 $ S $ reduce S’ → S 7 $ S’ $ accept
  • 7. Bottom-Up Parsing (Continue…)  A bottom-up parser can shift input symbols onto the stack until it determines what action to perform  The parser may need to look deeper into the stack then just the top in order to determine what action to perform  Thus bottom-up parsing requires arbitrary ‘stack lookahead’, which is not as serious as input lookahead because parser builds the stack and can arrange for the appropriate information to be available  The mechanism that will do this is a deterministic finite automaton of “items” 7
  • 8. LR(0) Items  The LR(0) item (or just item) of a CFG is a production choice with a distinguished position on its right hand side  We use period “.” symbol to indicate distinguished position  If A → α is a production choice, and if β and γ are any two strings of symbols (including ε) such that βγ = α, then A → β.γ is an LR(0) item  Example:  Write down LR(0) items for the following grammar: S’ → S S → ( S ) S | ε 8
  • 9. Finite Automata of LR(0) Items  In first pass a NFA is generated for LR(0) items  In second pass NFA is converted into DFA of LR(0) items  States in the DFA are numbered  The transitions in symbols can be terminals, nonterminals, and ε (only in NFA)  There is an initial state  There is one or more accepting states depending upon the grammar 9
  • 10. Construction of NFA & DFA of LR(0) Items S’ → S S → ( S ) S | ε 10
  • 11. LR(0) Parsing  LR(0) is a bottom-up parsing algorithm  The “L” indicates the input will be processed from left to right  The “R” indicates rightmost derivation is produced  The “0” means no lookahead token is used  LR(0) uses DFA of items to perform parsing  The basic actions remain the same including shift and reduce 11
  • 12. LR(0) Parsing (Continue…) 12  Grammar: S’ → S S → ( S ) S | ε  Input: () No Parsing Stack Input Action 1 $ 0 ( ) $ shift 2 $ 0 ( 2 ) $ reduce S → ε 3 $ 0 ( 2 S 3 ) $ shift 4 $ 0 ( 2 S 3 ) 4 $ reduce S → ε 5 $ 0 ( 2 S 3 ) 4 S 5 $ reduce S → ( S ) S 6 $ 0 S 1 $ accept
  • 13. LR(0) Parsing Table  A LR(0) parsing table can be constructed against the DFA of items of a given grammar with columns  State  Action  Rule  Input  Goto 13