This document defines context-free grammars and discusses properties like ambiguity. It begins by defining a context-free grammar as a 4-tuple (V, Σ, R, S) where V is nonterminals, Σ is terminals, R is rules, and S is the start symbol. It then gives an example grammar Gpal that generates the language of palindromes over {0,1}. The document proves that L(Gpal) = Lpal by showing inclusion in both directions using induction on derivation lengths. It discusses derivations and the language of a CFG in detail.
In this slide you will explore more about how to make derivations ,design parse tree ,what is ambiguity and how to remove ambiguity ,left recursion ,left factoring .
In this slide you will explore more about how to make derivations ,design parse tree ,what is ambiguity and how to remove ambiguity ,left recursion ,left factoring .
advanced computer architesture-conditions of parallelismPankaj Kumar Jain
This PPT contains Data and Resource Dependencies,Control Dependence,Resource Dependence,Bernstein’s Conditions ,Hardware And Software Parallelism,Types of Software Parallelism
This Lecture Note discusses the followings:
- What is NFA
- DFA vs NFA
- How does NFA compute
- Designing different NFA machines
- Regular operations on NFAs
- Conversion from NFA to DFA
Its the first phase of the compiler,useful in generating lexemes ,tokens and matching of the pattern.Its helpful in solving GATE/ UGCNET problems.For more insight refer http://tutorialfocus.net/
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Mohammad Ilyas Malik
The term "Automata" is derived from the Greek word "αὐτόματα" which means "self-acting". An automaton (Automata in plural) is an abstract self-propelled computing device which follows a predetermined sequence of operations automatically.
On Inherent Complexity of Computation, by Attila SzegediZeroTurnaround
The system you just recently deployed is likely an application processing some data, likely relying on some configuration, maybe using some plugins, certainly relying on some libraries, using services of an operating system running on some physical hardware. The previous sentence names 7 categories into which we compartmentalise various parts of a computation process that’s in the end going on in a physical world. Where do you draw the line of functionality between categories? From what vantage points do these distinctions become blurry? Finally, how does it all interact with the actual physical world in which the computation takes place? (What is the necessary physical minimum required to perform a computation, anyway?) Let’s make a journey from your AOP-assembled, plugin-injected, YAML-configured, JIT compiled, Hotspot-executed, Linux-on-x86 hosted Java application server talking JSON-over-HTTP-over-TCP-over-IP-over-Ethernet all the way down to electrons. And then back. Recorded at GeekOut 2013.
advanced computer architesture-conditions of parallelismPankaj Kumar Jain
This PPT contains Data and Resource Dependencies,Control Dependence,Resource Dependence,Bernstein’s Conditions ,Hardware And Software Parallelism,Types of Software Parallelism
This Lecture Note discusses the followings:
- What is NFA
- DFA vs NFA
- How does NFA compute
- Designing different NFA machines
- Regular operations on NFAs
- Conversion from NFA to DFA
Its the first phase of the compiler,useful in generating lexemes ,tokens and matching of the pattern.Its helpful in solving GATE/ UGCNET problems.For more insight refer http://tutorialfocus.net/
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Mohammad Ilyas Malik
The term "Automata" is derived from the Greek word "αὐτόματα" which means "self-acting". An automaton (Automata in plural) is an abstract self-propelled computing device which follows a predetermined sequence of operations automatically.
On Inherent Complexity of Computation, by Attila SzegediZeroTurnaround
The system you just recently deployed is likely an application processing some data, likely relying on some configuration, maybe using some plugins, certainly relying on some libraries, using services of an operating system running on some physical hardware. The previous sentence names 7 categories into which we compartmentalise various parts of a computation process that’s in the end going on in a physical world. Where do you draw the line of functionality between categories? From what vantage points do these distinctions become blurry? Finally, how does it all interact with the actual physical world in which the computation takes place? (What is the necessary physical minimum required to perform a computation, anyway?) Let’s make a journey from your AOP-assembled, plugin-injected, YAML-configured, JIT compiled, Hotspot-executed, Linux-on-x86 hosted Java application server talking JSON-over-HTTP-over-TCP-over-IP-over-Ethernet all the way down to electrons. And then back. Recorded at GeekOut 2013.
it is the first Homework.
it is about..
1-)The Foundations: Logic and Proofs
2-)Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
3-)Number Theory and Cryptography
4-)Induction and Recursion
Palestine last event orientationfvgnh .pptxRaedMohamed3
An EFL lesson about the current events in Palestine. It is intended to be for intermediate students who wish to increase their listening skills through a short lesson in power point.
Macroeconomics- Movie Location
This will be used as part of your Personal Professional Portfolio once graded.
Objective:
Prepare a presentation or a paper using research, basic comparative analysis, data organization and application of economic information. You will make an informed assessment of an economic climate outside of the United States to accomplish an entertainment industry objective.
Embracing GenAI - A Strategic ImperativePeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
Biological screening of herbal drugs: Introduction and Need for
Phyto-Pharmacological Screening, New Strategies for evaluating
Natural Products, In vitro evaluation techniques for Antioxidants, Antimicrobial and Anticancer drugs. In vivo evaluation techniques
for Anti-inflammatory, Antiulcer, Anticancer, Wound healing, Antidiabetic, Hepatoprotective, Cardio protective, Diuretics and
Antifertility, Toxicity studies as per OECD guidelines
Read| The latest issue of The Challenger is here! We are thrilled to announce that our school paper has qualified for the NATIONAL SCHOOLS PRESS CONFERENCE (NSPC) 2024. Thank you for your unwavering support and trust. Dive into the stories that made us stand out!
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
This presentation provides a briefing on how to upload submissions and documents in Google Classroom. It was prepared as part of an orientation for new Sainik School in-service teacher trainees. As a training officer, my goal is to ensure that you are comfortable and proficient with this essential tool for managing assignments and fostering student engagement.
2024.06.01 Introducing a competency framework for languag learning materials ...Sandy Millin
http://sandymillin.wordpress.com/iateflwebinar2024
Published classroom materials form the basis of syllabuses, drive teacher professional development, and have a potentially huge influence on learners, teachers and education systems. All teachers also create their own materials, whether a few sentences on a blackboard, a highly-structured fully-realised online course, or anything in between. Despite this, the knowledge and skills needed to create effective language learning materials are rarely part of teacher training, and are mostly learnt by trial and error.
Knowledge and skills frameworks, generally called competency frameworks, for ELT teachers, trainers and managers have existed for a few years now. However, until I created one for my MA dissertation, there wasn’t one drawing together what we need to know and do to be able to effectively produce language learning materials.
This webinar will introduce you to my framework, highlighting the key competencies I identified from my research. It will also show how anybody involved in language teaching (any language, not just English!), teacher training, managing schools or developing language learning materials can benefit from using the framework.
Francesca Gottschalk - How can education support child empowerment.pptxEduSkills OECD
Francesca Gottschalk from the OECD’s Centre for Educational Research and Innovation presents at the Ask an Expert Webinar: How can education support child empowerment?
Chapter 3 - Islamic Banking Products and Services.pptx
Ambiguty
1. Context Free Grammars
Ambiguity
CS 373: Theory of Computation
Gul Agha Mahesh Viswanathan
University of Illinois, Urbana-Champaign
Fall 2010
Agha-Viswanathan CS373
3. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Agha-Viswanathan CS373
4. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Σ is a finite set of terminals. Σ is disjoint from V .
Agha-Viswanathan CS373
5. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Σ is a finite set of terminals. Σ is disjoint from V .
R is a finite set of rules or productions of the form A → α
where A ∈V and α ∈(V ∪ Σ)∗
Agha-Viswanathan CS373
6. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Grammars
Definition
Acontext-free grammar(CFG) is G = (V ,Σ, R, S) where
V is a finite set of variables/non-terminals.
Σ is a finite set of terminals. Σ is disjoint from V .
R is a finite set of rules or productions of the form A → α
where A ∈V and α ∈(V ∪ Σ)∗
S ∈V is the start symbol
Agha-Viswanathan CS373
8. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Example: Palindromes
Example
A string w is apalindromeif w = wR .
Gpal = ({S}, {0,1},R, S) defines palindromes over {0,1}, where R
is
S → s
S → 0
S → 1
S → 0S0
S → 1S1
Agha-Viswanathan CS373
9. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Example: Palindromes
Example
A string w is apalindromeif w = wR .
Gpal = ({S}, {0,1},R, S) defines palindromes over {0,1}, where R
is
S → s
S → 0
S → 1
S → 0S0
S → 1S1
Or more briefly, R = {S → s|0 |1 |0S0 |1S1}
Agha-Viswanathan CS373
10. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Language of a CFG
Derivations
Expand the start symbol using one of its rules. Further expand the
resulting string by expanding one of the variables in the string, by
the RHS of one of its rules. Repeat until you get a string of
terminals.
Agha-Viswanathan CS373
11. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Language of a CFG
Derivations
Expand the start symbol using one of its rules. Further expand the
resulting string by expanding one of the variables in the string, by
the RHS of one of its rules. Repeat until you get a string of
terminals. For the grammar
Gpal = ({S}, {0,1},{S → s|0 |1 |0S0 |1S1}, S) we have
S ⇒ 0S0 ⇒ 00S00 ⇒ 001S100 ⇒ 0010100
Agha-Viswanathan CS373
12. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Formal Definition
Definition
Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where
α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G .
Agha-Viswanathan CS373
13. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Formal Definition
Definition
Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where
α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G .
We say α ∗
0 1 n⇒G β if either α = β or there are α , α , . . . α such that
α = α0 ⇒G α1 ⇒G α2 ⇒G · · · ⇒G αn = β
Agha-Viswanathan CS373
14. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Formal Definition
Definition
Let G = (V ,Σ, R, S) be a CFG. We say αAβ ⇒G αγβ, where
α, β, γ ∈(V ∪Σ)∗ and A ∈V if A → γ is a rule of G .
We say α ∗
0 1 n⇒G β if either α = β or there are α , α , . . . α such that
α = α0 ⇒G α1 ⇒G α2 ⇒G · · · ⇒G αn = β
Notation
When G is clear from the context, we will write ⇒ and ∗
instead
⇒
of ⇒G and ∗
.
⇒G
Agha-Viswanathan CS373
15. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Language
Definition
Thelanguage of CFG G = (V , Σ, R, S), denoted L(G ) is the
collection of strings over the terminals derivable from S using the
rules in R. In other words,
L(G ) = {w ∈Σ∗ |S ∗
w}
⇒
Agha-Viswanathan CS373
16. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Context-Free Language
Definition
Thelanguage of CFG G = (V , Σ, R, S), denoted L(G ) is the
collection of strings over the terminals derivable from S using the
rules in R. In other words,
L(G ) = {w ∈Σ∗ |S ∗
w}
⇒
Definition
A language L is said to becontext-freeif there is a CFG G such
that L = L(G).
Agha-Viswanathan CS373
22. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Agha-Viswanathan CS373
23. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol.
Agha-Viswanathan CS373
24. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol. Let w = 0x 0.
Agha-Viswanathan CS373
25. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol. Let w = 0x 0. Now,
wR = 0xR0 = w = 0x0; thus, xR = x.
Agha-Viswanathan CS373
26. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊆ L(Gpal)
Proof.
Let w ∈Lpal. We prove that S ⇒∗
w by induction on |w|.
Base Cases:If |w|= 0 or |w|= 1 then w = sor 0 or 1. And
S → s|0 |1.
Induction Step:If |w|≥ 2 and w = w R then it must begin
and with the same symbol. Let w = 0x 0. Now,
wR = 0xR0 = w = 0x0; thus, xR = x. By induction
hypothesis, S ⇒
∗
x . Hence S ⇒ 0S0 ∗
0x 0. If w = 1x 1 the
⇒
argument is similar. ··→
Agha-Viswanathan CS373
27. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal
Agha-Viswanathan CS373
28. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Agha-Viswanathan CS373
29. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Base Case:If the derivation has only one step then the
derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0
or 1 and is in LPal.
Agha-Viswanathan CS373
30. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Base Case:If the derivation has only one step then the
derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0
or 1 and is in LPal.
Induction Step:Consider an ( n + 1)-step derivation of w . It
must be of the form S ⇒ 0S0 ∗
0x 0 = w or
⇒
S ⇒ 1S1 ∗
1x 1 = w .
⇒
Agha-Viswanathan CS373
31. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Proving Correctness of CFG
Lpal ⊇ L(Gpal)
Proof (contd).
Let w ∈L(G ), i.e., S ⇒∗
w . We will show w ∈Lpal by induction
on the number of derivation steps.
Base Case:If the derivation has only one step then the
derivation must be S ⇒ s, S ⇒ 0 or S ⇒ 1. Thus w = sor 0
or 1 and is in LPal.
Induction Step:Consider an ( n + 1)-step derivation of w . It
must be of the form S ⇒ 0S0 ∗
0x 0 = w or
⇒
S ⇒ 1S1 ∗
1x 1 = w . In either case S
⇒
∗
⇒ x in n-steps. Hence
x ∈LPal and so w = w R. Q
Agha-Viswanathan CS373
32. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
33. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
34. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
Each leaf is labeled by either a
variable, a terminal or s;a leaf
labeled by smust be the only child
of its parent.
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
35. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
Each leaf is labeled by either a
variable, a terminal or s;a leaf
labeled by smust be the only child
of its parent.
If an interior node labeled by A
with children labeled by
X1, X2, ...Xk (from the left), then
A → X1X2 · · · Xk mustbe a rule.
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Agha-Viswanathan CS373
36. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees
For CFG G = (V , Σ, R, S), aparse tree(orderivationtree) of G is
a tree satisfying the following conditions:
Each interior node is labeled by a
variable in V
Each leaf is labeled by either a
variable, a terminal or s;a leaf
labeled by smust be the only child
of its parent.
If an interior node labeled by A
with children labeled by
X1, X2, ...Xk (from the left), then
A → X1X2 · · · Xk mustbe a rule.
S
0 S 0
1 S 1
1 S 1
s
Example Parse Tree withyield
011110
Yield of a parse treeis the concatenation of leaf labels (left–right)
Agha-Viswanathan CS373
37. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees and Derivations
Proposition
A ∗
Let G = (V , Σ, R, S) be a CFG. For any A ∈V and α ∈(V ∪Σ)∗,
⇒ α iffthereis a parse treewith root labeled A and whose yield is
α.
Agha-Viswanathan CS373
38. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees and Derivations
Proposition
A ∗
Let G = (V , Σ, R, S) be a CFG. For any A ∈V and α ∈(V ∪Σ)∗,
⇒ α iffthereis a parse treewith root labeled A and whose yield is
α.
Proof.
(⇒):Proof by induction on the number of steps in the derivation.
Base Case:If A ⇒ α then A → α
is a rule in G . There is a tree of
height 1, with root A and leaves
the symbols in α. ··→
A
α1 α2 · · · αn
Parse Tree for Base Case
Agha-Viswanathan CS373
39. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Agha-Viswanathan CS373
40. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Then A ∗
α1Xα2 ⇒ α1γα2 =α,
⇒
where X → X1 · · · Xn = γ is arule
Agha-Viswanathan CS373
41. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Then A ∗
α1Xα2 ⇒ α1γα2 =α,
⇒
where X → X1 · · · Xn = γ is arule
By ind. hyp., there is a tree with
root A and yield α1Xα2.
A
α1 X α2
Parse Tree for Induction Step
Agha-Viswanathan CS373
42. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Parse Trees for Derivations
Proof (contd).
(⇒):Proof by induction on the number of steps in the derivation.
Induction Step:Let A ∗
α in
⇒
k + 1steps.
Then A ∗
α1Xα2 ⇒ α1γα2 =α,
⇒
where X → X1 · · · Xn = γ is arule
By ind. hyp., there is a tree with
root A and yield α1Xα2.
Add leaves X1, . . . Xn and make
them children of X . New tree isa
parse tree with desired yield. ··→
A
α1 X α2
γ
Parse Tree for Induction Step
Agha-Viswanathan CS373
43. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α.
⇒
··→
Agha-Viswanathan CS373
44. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α. Proof by induction on the number of⇒
internal nodes in the tree.
··→
Agha-Viswanathan CS373
45. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α. Proof by induction on the number of⇒
internal nodes in the tree.
Base Case:If tree has only one
internal node, then it has the form
as in picture
A
α1 α2 · · · αn
Parse Tree with one internal
node
··→
Agha-Viswanathan CS373
46. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐):Assume that there is a parse tree with root A and yield α.
Need to show that A ∗
α. Proof by induction on the number of⇒
internal nodes in the tree.
Base Case:If tree has only one
internal node, then it has the form
as in picture
Then, α = X1 ···Xn and A → α is
a rule. Thus, A ∗
α.
⇒
A
α1 α2 · · · αn
Parse Tree with one internal
node
··→
Agha-Viswanathan CS373
47. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes. Let X1, X2, . . . Xn be the children of the root
ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn
must be a rule.
A
X1 X2 Xn···
···
α1 α2 αn
Tree with k+1 internal nodes
Agha-Viswanathan CS373
48. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes. Let X1, X2, . . . Xn be the children of the root
ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn
must be a rule.
Let αi be the yield of the tree
rooted at Xi ; so Xi is a leaf αi = Xi
A
X1 X2 Xn···
···
α1 α2 αn
Tree with k+1 internal nodes
Agha-Viswanathan CS373
49. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes. Let X1, X2, . . . Xn be the children of the root
ordered from the left. Not all Xi are leaves, and A → X1X2 · · · Xn
must be a rule.
Let αi be the yield of the tree
rooted at Xi ; so Xi is a leaf αi = Xi
Now if j< i then all the
descendents of Xj are to the left of
the descendents of Xi . So
α = α1α2 ···αn. ··→
A
X1 X2 Xn···
···
α1 α2 αn
Tree with k+1 internal nodes
Agha-Viswanathan CS373
50. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
α is the yield of a tree with k + 1(⇐) Induction Step:Suppose
interior nodes.
A
X1 X2 Xn···
···
α1 α2 αn
Agha-Viswanathan CS373
51. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes.
∗
αi and if Xi is not a leaf
Each subtree rooted at Xi has at
most k internal nodes. So if Xi is a
leaf Xi ⇒
then Xi ⇒∗
αi (ind. hyp.).
A
X1 X2 Xn···
···
α1 α2 αn
Agha-Viswanathan CS373
52. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Derivations for Parse Trees
Proof (contd).
(⇐) Induction Step:Suppose α is the yield of a tree with k + 1
interior nodes.
∗
αi and if Xi is not a leaf
∗
αi (ind. hyp.).
Each subtree rooted at Xi has at
most k internal nodes. So if Xi is a
leaf Xi ⇒
then Xi ⇒
Thus
A ⇒ X1X2 ···Xn ⇒
∗∗
α1X2 · · · Xn ⇒
α1α2 ···Xn ⇒∗
α1 ···αn = α Q
A
X1 X2 Xn···
···
α1 α2 αn
Agha-Viswanathan CS373
53. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Recap ...
For a CFG G with variable A the following are equivalent
1 A ∗
⇒ w
2 There is a parse tree with root A and yield w
Agha-Viswanathan CS373
54. Context Free Grammars
Ambiguity
Definitions
Proving Properties
Parse Trees
Recap ...
For a CFG G with variable A the following are equivalent
1 A ∗
⇒ w
2 There is a parse tree with root A and yield w
Context-free-ness
CFGs have the property that if X ⇒
∗
γ then αXβ ∗
αγβ
⇒
Agha-Viswanathan CS373
55. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Example: English Sentences
English sentences can be described as
(S) →(NP)(VP)
(NP) → (CN)|(CN)(PP)
(VP) → (CV )|(CV )(PP)
(PP) →(P)(CN)
(CN) →(A)(N)
(CV ) → (V) |(V )(NP)
(A) → a|the
(N) → boy |girl |bat
(V ) → hits |likes |sees
(P) →with
Agha-Viswanathan CS373
56. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Multiple Parse Trees
Example 1
The sentence “the girl hits the boy with the bat” has the following
parse tree
(S)
(NP)
(CN)
(A)
the girl
(VP)
(V )
(N) hits
(NP)
the
(CN) (PP)
(A) (N)(P) (CN)
boywith (A) (N)
the bat
Agha-Viswanathan CS373
57. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
the girl
the
the girl hits
boywith (A) (N) (A) (N)
the bat the boy
Multiple Parse Trees
Example 1
The sentence “the girl hits the boy with the bat” has the following
parse trees
(S) (S)
(NP) (VP) (NP) (VP)
(CN) (V ) (NP) (CN) (CV ) (PP)
(A) (N) hits (CN) (PP) (A) (N) (V ) (NP) (P) (CN)
(A) (N)(P) (CN) (CN) with (A) (N)
the bat
Agha-Viswanathan CS373
58. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Example: Arithmetic Expressions
Consider the language of all arithmetic expressions (E ) built out of
integers (N) and identifiers (I ), using only + and ∗
Agha-Viswanathan CS373
59. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Example: Arithmetic Expressions
Consider the language of all arithmetic expressions (E ) built out of
integers (N) and identifiers (I ), using only + and ∗
Gexp = ({E,I,N},{a,b,0,1,(,),+,∗,−},R, E ) where R is
E → I |N | − N |E + E |E ∗E |(E)
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
Agha-Viswanathan CS373
60. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Multiple Parse Trees
Example 2
The parse tree for expression a + b ∗a in the grammar Gexp is
E
E
E +
I E
a I
b
∗ E
I
a
Agha-Viswanathan CS373
61. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Multiple Parse Trees
Example 2
The parse trees for expression a + b ∗a in the grammar Gexp is
E
E
E +
I E
a I
b
∗ E
I
a
E
E
I
a
E
+
∗ E
E I
I a
b
Agha-Viswanathan CS373
62. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity
Definition
A grammar G = (V ,Σ, R, S) is said to beambiguousif there is
w ∈Σ∗ for which there are two different parse trees.
Agha-Viswanathan CS373
63. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity
Definition
A grammar G = (V ,Σ, R, S) is said to beambiguousif there is
w ∈Σ∗ for which there are two different parse trees.
Warning!
Existence of two derivations for a string does not mean the
grammar is ambiguous!
Agha-Viswanathan CS373
65. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules.
Agha-Viswanathan CS373
66. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules. For example, if we
knew who had the bat (the girl or the boy) from the context,
we would know which is the right interpretation.
Agha-Viswanathan CS373
67. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules. For example, if we
knew who had the bat (the girl or the boy) from the context,
we would know which is the right interpretation.
Adding precedence to operators.
Agha-Viswanathan CS373
68. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Removing Ambiguity
Ambiguity maybe removed either by
Using the semantics to change the rules. For example, if we
knew who had the bat (the girl or the boy) from the context,
we would know which is the right interpretation.
Adding precedence to operators. For example, ∗binds more
tightly than +, or “else” binds with the innermost “if”.
Agha-Viswanathan CS373
69. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
An Example
Recall, Gexp has the following rules
E → I |N | − N |E + E |E ∗E |(E)
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
Agha-Viswanathan CS373
70. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
An Example
Recall, Gexp has the following rules
E → I |N | − N |E + E |E ∗E |(E)
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
New CFG Gt
exp has the
rules
I → a |b |Ia |Ib
N → 0 |1 |N0 |N1
F → I |N | − N |(E)
T → F |T ∗F
E → T |E + T
Agha-Viswanathan CS373
71. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
Agha-Viswanathan CS373
72. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
There is no algorithm that can solve the above problem!
Agha-Viswanathan CS373
73. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
There is no algorithm that can solve the above problem!
Deciding Ambiguity
Problem:Given CFG G , determine if G is ambiguous.
Agha-Viswanathan CS373
74. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Ambiguity: Computational Problems
Removing Ambiguity
Problem:Given CFG G , find CFG Gt such that L(G ) = L(Gt) and
Gt is unambiguous.
There is no algorithm that can solve the above problem!
Deciding Ambiguity
Problem:Given CFG G , determine if G is ambiguous.
The problem is undecidable.
Agha-Viswanathan CS373
75. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Problem:Is it the case that for every CFG G , there is a grammar
Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt
cannot be constructed algorithmically?
Agha-Viswanathan CS373
76. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
Problem:Is it the case that for every CFG G , there is a grammar
Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt
cannot be constructed algorithmically?
No! There are context-free languages L such that every grammar
for L is ambiguous.
Agha-Viswanathan CS373
77. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
Problem:Is it the case that for every CFG G , there is a grammar
Gt such that L(G ) = L(Gt) and Gt is unambiguous, even ifGt
cannot be constructed algorithmically?
No! There are context-free languages L such that every grammar
for L is ambiguous.
Definition
A context-free language L is said to beinherently ambiguousif
every grammar G for L is ambiguous.
Agha-Viswanathan CS373
78. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
Agha-Viswanathan CS373
79. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
One can show that any CFG G for L will have two parse trees on
anbncn, for all but finitely many values of n
Agha-Viswanathan CS373
80. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
One can show that any CFG G for L will have two parse trees on
anbncn, for all but finitely many values of n
One that checks that number of a’s = number of b’s
Agha-Viswanathan CS373
81. Context Free Grammars
Ambiguity
The Concept
Removing Ambiguity
Inherently Ambiguous Languages
An Example
Consider
L = {aibjck |i = j or j = k}
One can show that any CFG G for L will have two parse trees on
anbncn, for all but finitely many values of n
One that checks that number of a’s = number of b’s
Another that checks that number of b’s = number of c ’s
Agha-Viswanathan CS373