SlideShare ist ein Scribd-Unternehmen logo
1 von 19
SYNTAX ANALYSIS
OR
PARSING
Lecture 09
1
ASSIGNMENT 01 (DUE ON 20TH)
1. Derive the (i) LL(1) parse table, (ii) LR(0)
automation, (iii) LR(0) parse table of following
grammar:
S-> SS+
S-> SS*
S-> a
2. Parse the following string using (i) LL (1) parse
table, (ii) LR (0) parse table:
“aa+a*a+”
2
LR(1) ITEM
 To avoid some of invalid reductions, the states need to carry more
information.
 Extra information is put into a state by including a terminal symbol
as a second component in an item.
 A LR(1) item is:
A  .,a where a is the look-head of the LR(1) item
(a is a terminal or end-marker.)
3
LR (1) ITEMS
4
INTUITION BEHIND LR (1) ITEMS
5
INTUITION BEHIND LR (1) ITEMS
6
THE CLOSURE FUNCTION
7
THE CLOSURE FUNCTION
8
LR (1) EXAMPLE
 S -> CC
 C -> cC
 C -> d
9
10
THE GOTO FUNCTION
11
CONSTRUCTION OF LR(1) PARSING TABLES
1. Construct the canonical collection of sets of LR(1) items for G’.
C{I0,...,In}
2. Create the parsing action table as follows
• If a is a terminal, A.a,b in Ii and goto(Ii,a)=Ij then action[i,a] is
shift j.
• If A.,a is in Ii , then action[i,a] is reduce A where AS’.
• If S’S.,$ is in Ii , then action[i,$] is accept.
• If any conflicting actions generated by these rules, the grammar is not
LR(1).
3. Create the parsing goto table
• for all non-terminals A, if goto(Ii,A)=Ij then goto[i,A]=j
4. All entries not defined by (2) and (3) are errors.
5. Initial state of the parser contains S’.S,$
12
LALR Parsing Tables
• LALR stands for LookAhead LR.
• LALR parsers are often used in practice because
LALR parsing tables are smaller than LR(1) parsing
tables.
• The number of states in SLR and LALR parsing tables
for a grammar G are equal.
• But LALR parsers recognize more grammars than
SLR parsers.
• A state of LALR parser will be again a set of LR(1)
items.
CREATING LALR PARSING TABLES
Canonical LR(1) Parser € LALR Parser
shrink # of states
• This shrink process may introduce a reduce/reduce
conflict in the resulting LALR parser (so the grammar
is NOT LALR)
• But, this shrink process does not produce a
shift/reduce conflict.
The Core of A Set of LR(1) Items
• The core of a set of LR(1) items is the set of its
first component.
Ex: S  L =R,$. S  L.=R
R  L.,$ R  L.
• We will find the states (sets of LR(1) items) in a canonical LR(1)
parser with same cores. Then we will merge them as a single
state.
L  id.,$
I1:L  id.,= A new state:I12: L  id.,=
I2:L  id.,$
• We will do this for all states of a canonical LR(1) parser to get
the states of the LALR parser.
• In fact, the number of the states of the LALR parser for a
grammar will be equal to the number of states of the SLR
Core
Same Core
Merge Them
CREATION OF LALR PARSING TABLES
• Create the canonical LR(1) collection of the sets of LR(1) items
for the given grammar.
• Find each core; find all sets having that same core; replace
those sets having same cores with a single set which is their
union.
C={I0,...,In} € C’={J1,...,Jm} where m  n
• Create the parsing tables (action and goto tables) same as the
construction of the parsing tables of LR(1) parser.
– Note that: If J=I1  ...  Iksince I1,...,Ik have same cores
€ cores of goto(I1,X),...,goto(I2,X) must be same.
– So, goto(J,X)=K where K is the union of all sets of items
having same cores as goto(I1,X).
• If no conflict is introduced, the grammar is LALR(1) grammar.
(We may only introduce reduce/reduce conflicts; we cannot
introduce a shift/reduce conflict)
SHIFT/REDUCE CONFLICT
• We say that we cannot introduce a shift/reduce conflict during
the shrink process for the creation of the states of a LALR
parser.
• Assume that we can introduce a shift/reduce conflict. In this
case, a state of LALR parser must have:
A  .,a and B  .a,b
• This means that a state of the canonical LR(1) parser must
have:
A  .,a and B  .a,c
But, this state has also a shift/reduce conflict. i.e. The original
canonical LR(1) parser has a conflict.
(Reason for this, the shift operation does not depend on
lookaheads)
Reduce/Reduce Conflict
• But, we may introduce a reduce/reduce conflict during the
shrink process for the creation of the states of a LALR parser.
I1 : A  .,a
B  .,b

I2: A  .,b
B  .,c
€ reduce/reduce conflictI12: A  .,a/b
B  .,b/c
Any Questions ?
19

Weitere ähnliche Inhalte

Was ist angesagt?

07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
Harish Khodke
 

Was ist angesagt? (19)

Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Ch4b
Ch4bCh4b
Ch4b
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
LR parsing
LR parsingLR parsing
LR parsing
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
 
Module 11
Module 11Module 11
Module 11
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Lecture10 syntax analysis_6
Lecture10 syntax analysis_6Lecture10 syntax analysis_6
Lecture10 syntax analysis_6
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Left factor put
Left factor putLeft factor put
Left factor put
 

Andere mochten auch (15)

Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Syntactic analysis.pptx
Syntactic analysis.pptxSyntactic analysis.pptx
Syntactic analysis.pptx
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Syntactic Analysis
Syntactic AnalysisSyntactic Analysis
Syntactic Analysis
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 

Ähnlich wie Lecture 09 syntax analysis 05

Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
myasir16
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
BapanKar2
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
ECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docx
ECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docxECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docx
ECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docx
SALU18
 
cd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdfcd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdf
ClaraBenedotti
 

Ähnlich wie Lecture 09 syntax analysis 05 (20)

Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
compiler design.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdf
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
 
parsing.pptx
parsing.pptxparsing.pptx
parsing.pptx
 
Lisp
LispLisp
Lisp
 
CD
CDCD
CD
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
 
ECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docx
ECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docxECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docx
ECS140A-F16-07 October 27, 2016ASSIGNMENT 5 LISPDue .docx
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
CD-1.pptx
CD-1.pptxCD-1.pptx
CD-1.pptx
 
cd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdfcd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdf
 
LISP: Introduction To Lisp
LISP: Introduction To LispLISP: Introduction To Lisp
LISP: Introduction To Lisp
 
Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence) Advance LISP (Artificial Intelligence)
Advance LISP (Artificial Intelligence)
 

Mehr von Iffat Anjum

Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
Iffat Anjum
 

Mehr von Iffat Anjum (20)

Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentation
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radio
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcare
 
Data link control
Data link controlData link control
Data link control
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
 
Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
 
A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...
 
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
 
Quality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networksQuality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networks
 
Library system
Library systemLibrary system
Library system
 

KĂźrzlich hochgeladen

KĂźrzlich hochgeladen (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 

Lecture 09 syntax analysis 05

  • 2. ASSIGNMENT 01 (DUE ON 20TH) 1. Derive the (i) LL(1) parse table, (ii) LR(0) automation, (iii) LR(0) parse table of following grammar: S-> SS+ S-> SS* S-> a 2. Parse the following string using (i) LL (1) parse table, (ii) LR (0) parse table: “aa+a*a+” 2
  • 3. LR(1) ITEM  To avoid some of invalid reductions, the states need to carry more information.  Extra information is put into a state by including a terminal symbol as a second component in an item.  A LR(1) item is: A  .,a where a is the look-head of the LR(1) item (a is a terminal or end-marker.) 3
  • 5. INTUITION BEHIND LR (1) ITEMS 5
  • 6. INTUITION BEHIND LR (1) ITEMS 6
  • 9. LR (1) EXAMPLE  S -> CC  C -> cC  C -> d 9
  • 10. 10
  • 12. CONSTRUCTION OF LR(1) PARSING TABLES 1. Construct the canonical collection of sets of LR(1) items for G’. C{I0,...,In} 2. Create the parsing action table as follows • If a is a terminal, A.a,b in Ii and goto(Ii,a)=Ij then action[i,a] is shift j. • If A.,a is in Ii , then action[i,a] is reduce A where AS’. • If S’S.,$ is in Ii , then action[i,$] is accept. • If any conflicting actions generated by these rules, the grammar is not LR(1). 3. Create the parsing goto table • for all non-terminals A, if goto(Ii,A)=Ij then goto[i,A]=j 4. All entries not defined by (2) and (3) are errors. 5. Initial state of the parser contains S’.S,$ 12
  • 13. LALR Parsing Tables • LALR stands for LookAhead LR. • LALR parsers are often used in practice because LALR parsing tables are smaller than LR(1) parsing tables. • The number of states in SLR and LALR parsing tables for a grammar G are equal. • But LALR parsers recognize more grammars than SLR parsers. • A state of LALR parser will be again a set of LR(1) items.
  • 14. CREATING LALR PARSING TABLES Canonical LR(1) Parser € LALR Parser shrink # of states • This shrink process may introduce a reduce/reduce conflict in the resulting LALR parser (so the grammar is NOT LALR) • But, this shrink process does not produce a shift/reduce conflict.
  • 15. The Core of A Set of LR(1) Items • The core of a set of LR(1) items is the set of its first component. Ex: S  L =R,$. S  L.=R R  L.,$ R  L. • We will find the states (sets of LR(1) items) in a canonical LR(1) parser with same cores. Then we will merge them as a single state. L  id.,$ I1:L  id.,= A new state:I12: L  id.,= I2:L  id.,$ • We will do this for all states of a canonical LR(1) parser to get the states of the LALR parser. • In fact, the number of the states of the LALR parser for a grammar will be equal to the number of states of the SLR Core Same Core Merge Them
  • 16. CREATION OF LALR PARSING TABLES • Create the canonical LR(1) collection of the sets of LR(1) items for the given grammar. • Find each core; find all sets having that same core; replace those sets having same cores with a single set which is their union. C={I0,...,In} € C’={J1,...,Jm} where m  n • Create the parsing tables (action and goto tables) same as the construction of the parsing tables of LR(1) parser. – Note that: If J=I1  ...  Iksince I1,...,Ik have same cores € cores of goto(I1,X),...,goto(I2,X) must be same. – So, goto(J,X)=K where K is the union of all sets of items having same cores as goto(I1,X). • If no conflict is introduced, the grammar is LALR(1) grammar. (We may only introduce reduce/reduce conflicts; we cannot introduce a shift/reduce conflict)
  • 17. SHIFT/REDUCE CONFLICT • We say that we cannot introduce a shift/reduce conflict during the shrink process for the creation of the states of a LALR parser. • Assume that we can introduce a shift/reduce conflict. In this case, a state of LALR parser must have: A  .,a and B  .a,b • This means that a state of the canonical LR(1) parser must have: A  .,a and B  .a,c But, this state has also a shift/reduce conflict. i.e. The original canonical LR(1) parser has a conflict. (Reason for this, the shift operation does not depend on lookaheads)
  • 18. Reduce/Reduce Conflict • But, we may introduce a reduce/reduce conflict during the shrink process for the creation of the states of a LALR parser. I1 : A  .,a B  .,b  I2: A  .,b B  .,c € reduce/reduce conflictI12: A  .,a/b B  .,b/c