SlideShare a Scribd company logo
1 of 13
Download to read offline
Topics: Ambiguous Grammar, DFA and SLR grammar.
Ismail Mohamed mohamud (maahir)
Abdiwahid farah isse
Abdifitah Awil Ahmed
Abdinor mohaedAhmed
Abdinasir nor kadie
Group five
Ambiguous Grammar
In computer science, a context-free grammar is said to be an ambiguous grammar if
there exists a string which can be generated by the grammar in more than one way (i.e.,
the string admits more than one parse tree or, equivalently, more than one leftmost
derivation). A context-free language is inherently ambiguous if all context- free
grammars generating that language are ambiguous.
Ambiguous Grammar
Some programming languages have ambiguous grammars; in this case, semantic
information is needed to select the intended parse tree of an ambiguous construct.
can be interpreted as either.
the declaration of an identifier named y of type pointer-to-x, or
an expression in which x is multiplied by y and then the result is discarded.
To correctly choose between the two possible interpretations, a compiler must
consult its symbol table to find out whether x has been declared as a type def
name that is visible at this point.
Ambiguous Grammar
A CFG is said to be ambiguous if and only if it contains more than one derivation
trees for same string.
Definition of Ambiguous Grammar: Let G = (N,T, P, S ) be a CFG. A string w
∈ L(G) is said to be “ambiguously derivable “if there are two or more different
derivation trees for that string in G.
Definition of Ambiguous Grammar: A CFG given by G = (N, T, P, S) is said to
be “ambiguous” if there exists at least one string in L(G) which is ambiguously
derivable. Otherwise it is unambiguous.
Ambiguous Grammar
Ambiguity is a property of a grammar, and it is usually, but not always possible to find
an equivalent unambiguous grammar.
An “ inherantly ambiguous language” is a language for which no unambiguous
grammar exists.
Solved Example:
Show that the grammar G with production
S → a | aAb | abSb
A → aAAb | bS
is ambiguous.
Ambiguous Grammar
Solution:
S ⟹ abSb ( ∵ S → abSb)
⟹ abab (∵ S → a)
Similarly,
S ⟹ aAb ( ∵ S → aAb)
⟹ abSb (∵ A → bS)
⟹ abab (∵ S → a)
Since ‘abab’ has two different derivations, the grammar G is ambiguous.
Proof of CFG is ambiguous or not by using parse tree solved example:
Consider the grammar G with production:
S → aSS
S → b
Ambiguous Grammar
• The parse trees are as follows.
• Top down Parsing: Sequence of rules are applied in a leftmost derivation in Top
down parsing.
•
Bottom-up Parsing: Sequence of rules are applied in a rightmost derivation in
Bottom-up parsing.
Deterministic finite Automata
DFArefers to deterministic finite automata. Deterministicrefers to theuniquenessof the
computation.Thefinite automata are called deterministic finite automata if themachineis read an
input string onesymbolat a time.
InDFA,there isonly onepath for specific input from thecurrentstate to thenext state.
DFAdoesnotaccept thenullmove,i.e., theDFAcannotchangestate without any input character
DFAcancontain multiple final states.It isusedinLexicalAnalysisinCompiler.
Deterministic finite Automata
A DFAcanberepresentedby digraphs calledstatediagram.In which:
Thestate isrepresented byvertices.
Thearc labeled with aninput character showthe transitions.
Theinitial state ismarked with anarrow.
Thefinal state isdenoted by a doublecircle.
SIMPLE LR PARSING SLR
SLR refers to simpleLRParsing.It issameasLR(0)parsing. Theonly difference isintheparsing
table. T
oconstructSLR(1) parsing table, weusecanonical collection of LR(0) item.
IntheSLR(1) parsing, weplace thereducemoveonly inthefollow of left hand side.
Various stepsinvolved in theSLR(1) Parsing:
Forthegiven input string write a context free grammar
Checktheambiguity of the grammar
AddAugmentproductioninthegiven grammar
Create Canonicalcollection of LR(0) items
Draw a data flow diagram (DFA)
Constructa SLR(1) parsing table
Constructing SLR Parsing Tables – LR(0) Item
An LR(0) item of a grammar G is a production of G a dot at the
some position of the right side.
Ex:
A → aBb
Possible LR(0) Items:
A → ∙ aBb
(four different possibility)
A → a ∙ Bb
A → aB ∙ b
A → aBb
Constructing SLR Parsing Tables – LR(0) Item
Sets of LR(0) items will be the states of action and goto table of
the SLR parser.
States represent sets of “items”
LR parser makes shift-reduce decision by maintaining states to
keep track of where we are in a parsing process
SLR grammars are a superset of all LR(0) grammars
In the LR parsing,
"L" stands for left-to-right scanning of the input.
"R" stands for constructing a right most derivation in reverse.
Constructing SLR Parsing Tables
A collection of sets of LR(0) items (the canonical LR(0)
collection) is the basis for constructing SLR parsers.
Canonical LR(0) collection provides the basis of constructing a
DFA called LR(0) automaton
This DFA is used to make parsing decisions
Each state of LR(0) automaton represents a set of items in the
canonical LR(0) collection

More Related Content

What's hot (19)

Swift
SwiftSwift
Swift
 
Generalized transition graphs
Generalized transition graphsGeneralized transition graphs
Generalized transition graphs
 
Lecture 8
Lecture 8Lecture 8
Lecture 8
 
Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of Computation
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2
 
LISP:Predicates in lisp
LISP:Predicates in lispLISP:Predicates in lisp
LISP:Predicates in lisp
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
LISP: Type specifiers in lisp
LISP: Type specifiers in lispLISP: Type specifiers in lisp
LISP: Type specifiers in lisp
 
To lec 03
To lec 03To lec 03
To lec 03
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Lesson 05
Lesson 05Lesson 05
Lesson 05
 
Parsing
ParsingParsing
Parsing
 

Similar to Ambiguous grammar, dfa and slr grammar.

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfry54321288
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdfkenilpatel65
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCSR2011
 
Class7
 Class7 Class7
Class7issbp
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
theory of computation lecture 02
theory of computation lecture 02theory of computation lecture 02
theory of computation lecture 028threspecter
 
BLOGIC. (ISWC 2009 Invited Talk)
BLOGIC.  (ISWC 2009 Invited Talk)BLOGIC.  (ISWC 2009 Invited Talk)
BLOGIC. (ISWC 2009 Invited Talk)Pat Hayes
 
Qedia - Natural Language Queries on DBPedia
Qedia - Natural Language Queries on DBPediaQedia - Natural Language Queries on DBPedia
Qedia - Natural Language Queries on DBPedialucianb
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.pptAmrita Sharma
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammarcjsmann
 
Lecture Notes-Are Natural Languages Regular.pdf
Lecture Notes-Are Natural Languages Regular.pdfLecture Notes-Are Natural Languages Regular.pdf
Lecture Notes-Are Natural Languages Regular.pdfDeptii Chaudhari
 
Efficient Query Answering against Dynamic RDF Databases
Efficient Query Answering against Dynamic RDF DatabasesEfficient Query Answering against Dynamic RDF Databases
Efficient Query Answering against Dynamic RDF DatabasesAlexandra Roatiș
 

Similar to Ambiguous grammar, dfa and slr grammar. (20)

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
 
Class7
 Class7 Class7
Class7
 
cfl2.pdf
cfl2.pdfcfl2.pdf
cfl2.pdf
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Module 11
Module 11Module 11
Module 11
 
theory of computation lecture 02
theory of computation lecture 02theory of computation lecture 02
theory of computation lecture 02
 
AI-09 Logic in AI
AI-09 Logic in AIAI-09 Logic in AI
AI-09 Logic in AI
 
BLOGIC. (ISWC 2009 Invited Talk)
BLOGIC.  (ISWC 2009 Invited Talk)BLOGIC.  (ISWC 2009 Invited Talk)
BLOGIC. (ISWC 2009 Invited Talk)
 
Qedia - Natural Language Queries on DBPedia
Qedia - Natural Language Queries on DBPediaQedia - Natural Language Queries on DBPedia
Qedia - Natural Language Queries on DBPedia
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.ppt
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammar
 
Lecture Notes-Are Natural Languages Regular.pdf
Lecture Notes-Are Natural Languages Regular.pdfLecture Notes-Are Natural Languages Regular.pdf
Lecture Notes-Are Natural Languages Regular.pdf
 
Efficient Query Answering against Dynamic RDF Databases
Efficient Query Answering against Dynamic RDF DatabasesEfficient Query Answering against Dynamic RDF Databases
Efficient Query Answering against Dynamic RDF Databases
 
00 ruby tutorial
00 ruby tutorial00 ruby tutorial
00 ruby tutorial
 

Recently uploaded

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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
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
 
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
 
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
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
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
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
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
 
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)

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
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
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
 
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)
 
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
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
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, ...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
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...
 
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
 
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...
 

Ambiguous grammar, dfa and slr grammar.

  • 1. Topics: Ambiguous Grammar, DFA and SLR grammar. Ismail Mohamed mohamud (maahir) Abdiwahid farah isse Abdifitah Awil Ahmed Abdinor mohaedAhmed Abdinasir nor kadie Group five
  • 2. Ambiguous Grammar In computer science, a context-free grammar is said to be an ambiguous grammar if there exists a string which can be generated by the grammar in more than one way (i.e., the string admits more than one parse tree or, equivalently, more than one leftmost derivation). A context-free language is inherently ambiguous if all context- free grammars generating that language are ambiguous.
  • 3. Ambiguous Grammar Some programming languages have ambiguous grammars; in this case, semantic information is needed to select the intended parse tree of an ambiguous construct. can be interpreted as either. the declaration of an identifier named y of type pointer-to-x, or an expression in which x is multiplied by y and then the result is discarded. To correctly choose between the two possible interpretations, a compiler must consult its symbol table to find out whether x has been declared as a type def name that is visible at this point.
  • 4. Ambiguous Grammar A CFG is said to be ambiguous if and only if it contains more than one derivation trees for same string. Definition of Ambiguous Grammar: Let G = (N,T, P, S ) be a CFG. A string w ∈ L(G) is said to be “ambiguously derivable “if there are two or more different derivation trees for that string in G. Definition of Ambiguous Grammar: A CFG given by G = (N, T, P, S) is said to be “ambiguous” if there exists at least one string in L(G) which is ambiguously derivable. Otherwise it is unambiguous.
  • 5. Ambiguous Grammar Ambiguity is a property of a grammar, and it is usually, but not always possible to find an equivalent unambiguous grammar. An “ inherantly ambiguous language” is a language for which no unambiguous grammar exists. Solved Example: Show that the grammar G with production S → a | aAb | abSb A → aAAb | bS is ambiguous.
  • 6. Ambiguous Grammar Solution: S ⟹ abSb ( ∵ S → abSb) ⟹ abab (∵ S → a) Similarly, S ⟹ aAb ( ∵ S → aAb) ⟹ abSb (∵ A → bS) ⟹ abab (∵ S → a) Since ‘abab’ has two different derivations, the grammar G is ambiguous. Proof of CFG is ambiguous or not by using parse tree solved example: Consider the grammar G with production: S → aSS S → b
  • 7. Ambiguous Grammar • The parse trees are as follows. • Top down Parsing: Sequence of rules are applied in a leftmost derivation in Top down parsing. • Bottom-up Parsing: Sequence of rules are applied in a rightmost derivation in Bottom-up parsing.
  • 8. Deterministic finite Automata DFArefers to deterministic finite automata. Deterministicrefers to theuniquenessof the computation.Thefinite automata are called deterministic finite automata if themachineis read an input string onesymbolat a time. InDFA,there isonly onepath for specific input from thecurrentstate to thenext state. DFAdoesnotaccept thenullmove,i.e., theDFAcannotchangestate without any input character DFAcancontain multiple final states.It isusedinLexicalAnalysisinCompiler.
  • 9. Deterministic finite Automata A DFAcanberepresentedby digraphs calledstatediagram.In which: Thestate isrepresented byvertices. Thearc labeled with aninput character showthe transitions. Theinitial state ismarked with anarrow. Thefinal state isdenoted by a doublecircle.
  • 10. SIMPLE LR PARSING SLR SLR refers to simpleLRParsing.It issameasLR(0)parsing. Theonly difference isintheparsing table. T oconstructSLR(1) parsing table, weusecanonical collection of LR(0) item. IntheSLR(1) parsing, weplace thereducemoveonly inthefollow of left hand side. Various stepsinvolved in theSLR(1) Parsing: Forthegiven input string write a context free grammar Checktheambiguity of the grammar AddAugmentproductioninthegiven grammar Create Canonicalcollection of LR(0) items Draw a data flow diagram (DFA) Constructa SLR(1) parsing table
  • 11. Constructing SLR Parsing Tables – LR(0) Item An LR(0) item of a grammar G is a production of G a dot at the some position of the right side. Ex: A → aBb Possible LR(0) Items: A → ∙ aBb (four different possibility) A → a ∙ Bb A → aB ∙ b A → aBb
  • 12. Constructing SLR Parsing Tables – LR(0) Item Sets of LR(0) items will be the states of action and goto table of the SLR parser. States represent sets of “items” LR parser makes shift-reduce decision by maintaining states to keep track of where we are in a parsing process SLR grammars are a superset of all LR(0) grammars In the LR parsing, "L" stands for left-to-right scanning of the input. "R" stands for constructing a right most derivation in reverse.
  • 13. Constructing SLR Parsing Tables A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers. Canonical LR(0) collection provides the basis of constructing a DFA called LR(0) automaton This DFA is used to make parsing decisions Each state of LR(0) automaton represents a set of items in the canonical LR(0) collection