SlideShare a Scribd company logo
1 of 11
Ambiguity Grammar
Subham Mukherjee
BWU/BTS/19/037
Definition
A grammar is said to be ambiguous if there exists more than one leftmost derivation or
more than one rightmost derivation or more than one parse tree for the given input string.
20XX PRESENTATION TITLE
2
Examples
Check whether the given grammar G is ambiguous or not.
1.S → aSb | SS
2.S → ε
Solution:
For the string "aabb" the above grammar can generate two parse trees
Since there are two parse
trees for a single string
"aabb", the grammar G is
ambiguous.
Examples
Check whether the given grammar G is ambiguous or not.
1.E → E + E
2.E → E - E
3.E → id
Solution:
From the above grammar String "id + id - id" can be derived in 2 ways:
First Leftmost derivation
1.E → E + E
2. → id + E
3. → id + E - E
4. → id + id - E
5. → id + id- id
Second Leftmost derivation
1.E → E - E
2. → E + E - E
3. → id + E - E
4. → id + id - E
5. → id + id - id
Since there are two leftmost derivation for a single string "id + id - id", the grammar G is
ambiguous.
Derivations from a Grammar
Strings may be derived from other strings using the production rules in a grammar.
Example:
Let us consider the grammar − G2 = ({S, A}, {a, b}, S, {S → aAb, aA → aaAb, A →ε } )
Some of the strings that can be derived are −
S ⇒ aAb using production S → aAb
⇒ aaAbb using production aA → aAb
⇒ aaaAbbb using production aA → aAb
⇒ aaabbb using production A →ε
Derivation
Derivation is a sequence of production rules used to get the input string.
During parsing, we have to take two decisions. These are as follows:
• We have to decide the non-terminal which is to be replaced.
• We have to decide the production rule by which the non-terminal will be replaced.
Leftmost Derivation
In the leftmost derivation, the input is scanned and replaced with the production rule from
left to right. So in leftmost derivation, we read the input string from left to right.
Example: Production rules: 1. E = E + E 2. E = E - E 3. E = a | b
Input : 1. a - b + c
The leftmost derivation is:
1. E = E + E
2. E = E - E + E
3. E = a - E + E
4. E = a - b + E
5. E = a - b + a
Rightmost Derivation
In rightmost derivation, the input is scanned and replaced with the production rule from
right to left. So in rightmost derivation, we read the input string from right to left.
Example: Production rules: 1. E = E + E 2. E = E - E 3. E = a | b
Input: 1. a - b + a
The rightmost derivation is:
1. E = E - E
2. E = E - E + E
3. E = E - E + a
4. E = E - b + a
5. E = a - b + a
Examples of Derivation
Derive the string "aabbabba" for leftmost derivation and rightmost derivation using a CFG given by,
1. S → aB | bA
2. S → a | aS | bAA
3. S → b | aS | aBB
Solution:
Leftmost derivation:
1. S
2. aB S → aB
3. aaBB B → aBB
4. aabB B → b
5. aabbS B → bS
6. aabbaB S → aB
7. aabbabS B → bS
8. aabbabbA S → bA
9. aabbabba A → a
Rightmost derivation:
1. S
2. 2. aB S → aB
3. 3. aaBB B → aBB
4. 4. aaBbS B → bS
5. 5. aaBbbA S → bA
6. 6. aaBbba A → a
7. 7. aabSbba B → bS
8. 8. aabbAbba S → bA
9. 9. aabbabba A → a
Disadvantages of Ambiguity
Ambiguity of the grammar implies that at least some strings in its language have
different structures (parse trees).
Thus, such a grammar is unlikely to be useful for a programming language, because
two structures for the same string (program) implies two different meanings
(executable equivalent programs) for this program.
If the grammar has ambiguity, then it is not good for compiler construction. No
method can automatically detect and remove the ambiguity, but we can remove
ambiguity by re-writing the whole grammar without ambiguity.
Thank you

More Related Content

Similar to Ambiguity Grammar.pptx

Theory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxTheory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptx
Jisock
 
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptxHow-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
ishawrb
 
Copy of Solve problems involving sets with the use of Venn-Diagram.pptx
Copy of Solve problems involving sets with the use of Venn-Diagram.pptxCopy of Solve problems involving sets with the use of Venn-Diagram.pptx
Copy of Solve problems involving sets with the use of Venn-Diagram.pptx
JosephSPalileoJr
 
Geometry working with line segments
Geometry working with  line segmentsGeometry working with  line segments
Geometry working with line segments
rfarinas
 

Similar to Ambiguity Grammar.pptx (17)

Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Expresiones algebraicas
Expresiones algebraicasExpresiones algebraicas
Expresiones algebraicas
 
GRE - Algebra
GRE - AlgebraGRE - Algebra
GRE - Algebra
 
Chomsky Normal Form
Chomsky Normal FormChomsky Normal Form
Chomsky Normal Form
 
Theory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxTheory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptx
 
Algebra 2 Section 1-1
Algebra 2 Section 1-1Algebra 2 Section 1-1
Algebra 2 Section 1-1
 
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptxHow-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
How-to-convert-a-left-linear-grammar-to-a-right-linear-grammar.pptx
 
Algebra leonel gutierrez
Algebra leonel gutierrez Algebra leonel gutierrez
Algebra leonel gutierrez
 
Copy of Solve problems involving sets with the use of Venn-Diagram.pptx
Copy of Solve problems involving sets with the use of Venn-Diagram.pptxCopy of Solve problems involving sets with the use of Venn-Diagram.pptx
Copy of Solve problems involving sets with the use of Venn-Diagram.pptx
 
Msm1 fl ch11_05
Msm1 fl ch11_05Msm1 fl ch11_05
Msm1 fl ch11_05
 
07_product_matrix.pdf
07_product_matrix.pdf07_product_matrix.pdf
07_product_matrix.pdf
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
 
Operator Precedence Grammar
Operator Precedence GrammarOperator Precedence Grammar
Operator Precedence Grammar
 
Geometry working with line segments
Geometry working with  line segmentsGeometry working with  line segments
Geometry working with line segments
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Expresiones algebraicas
Expresiones algebraicasExpresiones algebraicas
Expresiones algebraicas
 

Recently uploaded

如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证
ugzga
 
Resume all my skills and educations and achievement
Resume all my skills and educations and  achievement Resume all my skills and educations and  achievement
Resume all my skills and educations and achievement
210303105569
 
如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证
ugzga
 
如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证
ugzga
 
一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样
一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样
一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样
awasv46j
 
How to Build a Simple Shopify Website
How to Build a Simple Shopify WebsiteHow to Build a Simple Shopify Website
How to Build a Simple Shopify Website
mark11275
 
In Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in Riyadh
In Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in RiyadhIn Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in Riyadh
In Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in Riyadh
ahmedjiabur940
 
如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证
ugzga
 
如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
ugzga
 
如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证
ugzga
 

Recently uploaded (20)

Essential UI/UX Design Principles: A Comprehensive Guide
Essential UI/UX Design Principles: A Comprehensive GuideEssential UI/UX Design Principles: A Comprehensive Guide
Essential UI/UX Design Principles: A Comprehensive Guide
 
Edward Boginsky's Trailblazing Contributions to Printing
Edward Boginsky's Trailblazing Contributions to PrintingEdward Boginsky's Trailblazing Contributions to Printing
Edward Boginsky's Trailblazing Contributions to Printing
 
如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UoB毕业证书)伯明翰大学毕业证成绩单本科硕士学位证留信学历认证
 
Gamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad IbrahimGamestore case study UI UX by Amgad Ibrahim
Gamestore case study UI UX by Amgad Ibrahim
 
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
 
Branding in the Psychedelic Landscape Report.pdf
Branding in the Psychedelic Landscape Report.pdfBranding in the Psychedelic Landscape Report.pdf
Branding in the Psychedelic Landscape Report.pdf
 
Spring Summer 26 Colors Trend Book Peclers Paris
Spring Summer 26 Colors Trend Book Peclers ParisSpring Summer 26 Colors Trend Book Peclers Paris
Spring Summer 26 Colors Trend Book Peclers Paris
 
Resume all my skills and educations and achievement
Resume all my skills and educations and  achievement Resume all my skills and educations and  achievement
Resume all my skills and educations and achievement
 
如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Bath毕业证书)巴斯大学毕业证成绩单本科硕士学位证留信学历认证
 
如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(Columbia College毕业证书)纽约市哥伦比亚大学毕业证成绩单本科硕士学位证留信学历认证
 
Morgenbooster: Storytelling in Identity Design
Morgenbooster: Storytelling in Identity DesignMorgenbooster: Storytelling in Identity Design
Morgenbooster: Storytelling in Identity Design
 
Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Riyadh +966572737505 <> buy cytotec <> unwanted kit Saudi A...
 
一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样
一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样
一比一原版(WLU毕业证)罗瑞尔大学毕业证成绩单留信学历认证原版一模一样
 
How to Build a Simple Shopify Website
How to Build a Simple Shopify WebsiteHow to Build a Simple Shopify Website
How to Build a Simple Shopify Website
 
In Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in Riyadh
In Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in RiyadhIn Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in Riyadh
In Saudi Arabia Jeddah (+918761049707)) Buy Abortion Pills For Sale in Riyadh
 
如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(RUG毕业证书)格罗宁根大学毕业证成绩单本科硕士学位证留信学历认证
 
18+ Young ℂall Girls Vadodara Book Esha 7427069034 Top Class ℂall Girl Serviℂ...
18+ Young ℂall Girls Vadodara Book Esha 7427069034 Top Class ℂall Girl Serviℂ...18+ Young ℂall Girls Vadodara Book Esha 7427069034 Top Class ℂall Girl Serviℂ...
18+ Young ℂall Girls Vadodara Book Esha 7427069034 Top Class ℂall Girl Serviℂ...
 
如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
如何办理(UW毕业证书)华盛顿大学毕业证成绩单本科硕士学位证留信学历认证
 
如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证
如何办理(Birmingham毕业证书)伯明翰大学学院毕业证成绩单本科硕士学位证留信学历认证
 
Spring Summer 2026 Inspirations trend book Peclers Paris
Spring Summer 2026 Inspirations trend book Peclers ParisSpring Summer 2026 Inspirations trend book Peclers Paris
Spring Summer 2026 Inspirations trend book Peclers Paris
 

Ambiguity Grammar.pptx

  • 2. Definition A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. 20XX PRESENTATION TITLE 2
  • 3. Examples Check whether the given grammar G is ambiguous or not. 1.S → aSb | SS 2.S → ε Solution: For the string "aabb" the above grammar can generate two parse trees Since there are two parse trees for a single string "aabb", the grammar G is ambiguous.
  • 4. Examples Check whether the given grammar G is ambiguous or not. 1.E → E + E 2.E → E - E 3.E → id Solution: From the above grammar String "id + id - id" can be derived in 2 ways: First Leftmost derivation 1.E → E + E 2. → id + E 3. → id + E - E 4. → id + id - E 5. → id + id- id Second Leftmost derivation 1.E → E - E 2. → E + E - E 3. → id + E - E 4. → id + id - E 5. → id + id - id Since there are two leftmost derivation for a single string "id + id - id", the grammar G is ambiguous.
  • 5. Derivations from a Grammar Strings may be derived from other strings using the production rules in a grammar. Example: Let us consider the grammar − G2 = ({S, A}, {a, b}, S, {S → aAb, aA → aaAb, A →ε } ) Some of the strings that can be derived are − S ⇒ aAb using production S → aAb ⇒ aaAbb using production aA → aAb ⇒ aaaAbbb using production aA → aAb ⇒ aaabbb using production A →ε
  • 6. Derivation Derivation is a sequence of production rules used to get the input string. During parsing, we have to take two decisions. These are as follows: • We have to decide the non-terminal which is to be replaced. • We have to decide the production rule by which the non-terminal will be replaced.
  • 7. Leftmost Derivation In the leftmost derivation, the input is scanned and replaced with the production rule from left to right. So in leftmost derivation, we read the input string from left to right. Example: Production rules: 1. E = E + E 2. E = E - E 3. E = a | b Input : 1. a - b + c The leftmost derivation is: 1. E = E + E 2. E = E - E + E 3. E = a - E + E 4. E = a - b + E 5. E = a - b + a
  • 8. Rightmost Derivation In rightmost derivation, the input is scanned and replaced with the production rule from right to left. So in rightmost derivation, we read the input string from right to left. Example: Production rules: 1. E = E + E 2. E = E - E 3. E = a | b Input: 1. a - b + a The rightmost derivation is: 1. E = E - E 2. E = E - E + E 3. E = E - E + a 4. E = E - b + a 5. E = a - b + a
  • 9. Examples of Derivation Derive the string "aabbabba" for leftmost derivation and rightmost derivation using a CFG given by, 1. S → aB | bA 2. S → a | aS | bAA 3. S → b | aS | aBB Solution: Leftmost derivation: 1. S 2. aB S → aB 3. aaBB B → aBB 4. aabB B → b 5. aabbS B → bS 6. aabbaB S → aB 7. aabbabS B → bS 8. aabbabbA S → bA 9. aabbabba A → a Rightmost derivation: 1. S 2. 2. aB S → aB 3. 3. aaBB B → aBB 4. 4. aaBbS B → bS 5. 5. aaBbbA S → bA 6. 6. aaBbba A → a 7. 7. aabSbba B → bS 8. 8. aabbAbba S → bA 9. 9. aabbabba A → a
  • 10. Disadvantages of Ambiguity Ambiguity of the grammar implies that at least some strings in its language have different structures (parse trees). Thus, such a grammar is unlikely to be useful for a programming language, because two structures for the same string (program) implies two different meanings (executable equivalent programs) for this program. If the grammar has ambiguity, then it is not good for compiler construction. No method can automatically detect and remove the ambiguity, but we can remove ambiguity by re-writing the whole grammar without ambiguity.