SlideShare a Scribd company logo
Context-Free Grammars
(CFG)
SITE : http://www.sir.blois.univ-tours.fr/˜mirian/
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 1/2
An informal example
Language of palindromes: Lpal
A palindrome is a string that reads the same forward and backward
Ex: otto, madamimadam, 0110, 11011, ǫ
Lpal is not a regular language (can be proved by using the pumping lemma)
We consider Σ = {0, 1}. There is a natural, recursive definition of when a string
of 0 and 1 is in Lpal.
Basis: ǫ, 0 and 1 are palindromes
Induction: If w is a palindrome, so are 0w0 and 1w1. No string is palindrome of
0 and 1, unless it follows from this basis and inductive rule.
A CFG is a formal notation for expressing such recursive definitions of languages
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 2/2
What is a grammar?
A grammar consists of one or more variables that represent classes of strings
(i.e., languages)
There are rules that say how the strings in each class are constructed. The
construction can use :
1. symbols of the alphabet
2. strings that are already known to be in one of the classes
3. or both
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 3/2
A grammar for palindromes
In the example of palindromes, we need one variable P which represents the set
of palindromes; i.e., the class of strings forming the language Lpal
Rules:
P → ǫ
P → 0
P → 1
P → 0P0
P → 1P1
The first three rules form the basis.
They tell us that the class of palindromes includes the strings ǫ, 0 and 1
None of the right sides of theses rules contains a variable, which is why
they form a basis for the definition
The last two rules form the inductive part of the definition.
For instance, rule 4 says that if we take any string w from the class P, then 0w0
is also in class P.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 4/2
Definition of Context-Free Grammar
A GFG (or just a grammar) G is a tuple G = (V, T, P, S) where
1. V is the (finite) set of variables (or nonterminals or syntactic categories).
Each variable represents a language, i.e., a set of strings
2. T is a finite set of terminals, i.e., the symbols that form the strings of the
language being defined
3. P is a set of production rules that represent the recursive definition of the
language.
4. S is the start symbol that represents the language being defined.
Other variables represent auxiliary classes of strings that are used to help define
the language of the start symbol.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 5/2
Production rules
Each production rule consists of:
1. A variable that is being (partially) defined by the production. This variable is often
called the head of the production.
2. The production symbol →.
3. A string of zero or more terminals and variables. This string, called the body of
the production, represents one way to form strings in the language of the variable
of the head.
In doing so, we leave terminals unchanged and substitute for each variable of the
body any string that is known to be in the language of that variable
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 6/2
Compact Notation for Productions
We often refers to the production whose head is A as “productions for A” or
“A-productions”
Moreover, the productions
A → α1, A → α2 . . . A → αn
can be replaced by the notation
A → α1 | α2 | . . . | αn
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 7/2
Examples: CFG for palindromes
Gpal = ({P}, {0, 1}, A, P)
where A represents the production rules:
P → ǫ
P → 0
P → 1
P → 0P0
P → 1P1
We can also write: P → ǫ | 0 | 1 | 0P0 | 1P1
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 8/2
Examples: CFG for expressions in a typical
programming language
Operators: + (addition) and ∗ (multiplication)
Identifiers: must begin with a or b, which may be followed by any string in {a, b, 0, 1}∗
We need two variables:
E: represents expressions. It is the start symbol.
I: represents the identifiers. Its language is regular and is the language of the regular
expression: (a + b)(a + b + 0 + 1)∗
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 9/2
Exemples (cont.): The grammar
Grammar G1 = ({E, I}, T, P, E) where: T = {+, ∗, (, ), a, b, 0, 1} and P is the set of
productions:
1 E → I
2 E → E + E
3 E → E ∗ E
4 E → (E)
5 I → a
6 I → b
7 I → Ia
8 I → Ib
9 I → I0
10 I → I1
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 10/2
Derivations Using a Grammar
We apply the productions of a CFG to infer that certain strings are in the
language of a certain variable
Two inference approaches:
1. Recursive inference, using productions from body to head
2. Derivations, using productions from head to body
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 11/2
Recursive inference - an exemple
We consider some inferences we can make using G1
Recall G1:
E → I | E + E | E ∗ E | (E)
I → a | b | Ia | Ib | I0 | I1
String Lang Prod String(s) used
(i) a I 5 -
(ii) b I 6 -
(iii) b0 I 9 (ii)
(iv) b00 I 9 (iii)
(v) a E 1 (i)
(vi) b00 E 1 (iv)
(vii) a + b00 E 2 (v), (vi)
(viii) (a + b00) E 4 (vii)
(ix) a (a + b00) E 3 (v), (viii)
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 12/2
Derivations
Applying productions from head to body requires the definition of a new relational
symbol: ⇒
Let:
G = (V, T, P, S) be a CFG
A ∈ V
α, β ⊂ (V ∪ T)∗ and
A → γ ∈ P
Then we write
αAβ ⇒G αγβ
or, if G is understood
αAβ ⇒ αγβ
and say that αAβ derives αγβ.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 13/2
Zero or more derivation steps
We define
∗
⇒ to be the reflexive and transitive closure of ⇒ (i.e., to denote zero or more
derivation steps):
Basis: Let α ∈ (V ∪ T)∗. Then α
∗
⇒ α.
Induction: If α
∗
⇒ β and β ⇒ γ , then α
∗
⇒ γ .
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 14/2
Examples of derivation
Derivation of a ∗ (a + b000) by G1
E ⇒ E ∗ E ⇒ I ∗ E ⇒ a ∗ E ⇒ a ∗ (E) ⇒
a ∗ (E + E) ⇒ a ∗ (I + E) ⇒ a ∗ (a + E) ⇒ a ∗ (a + I) ⇒
a ∗ (a + I0) ⇒ a ∗ (a + I00) ⇒ a ∗ (a + b00)
Note 1: At each step we might have several rules to choose from, e.g.
I ∗ E ⇒ a ∗ E ⇒ a ∗ (E), versus
I ∗ E ⇒ I ∗ (E) ⇒ a ∗ (E).
Note 2: Not all choices lead to successful derivations of a particular string, for instance
E ⇒ E + E (at the first step)
won’t lead to a derivation of a ∗ (a + b000).
Important: Recursive inference and derivation are equivalent. A string of terminals w is
infered to be in the language of some variable A iff A
∗
⇒ w
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 15/2
Leftmost and Rightmost derivation
In other to restrict the number of choices we have in deriving a string, it is often
useful to require that at each step we replace the leftmost (or rightmost) variable
by one of its production rules
Leftmost derivation ⇒lm : Always replace the left-most variable by one of its
rule-bodies
Rightmost derivation ⇒rm : Always replace the rightmost variable by one of
its rule-bodies.
EXAMPLES
1− Leftmost derivation: previous example
2− Rightmost derivation:
E ⇒rm E ∗ E ⇒rm E ∗ (E) ⇒rm
E ∗ (E + E) ⇒rm E ∗ (E + I) ⇒rm E ∗ (E + I0) ⇒rm
E ∗ (E + I00) ⇒rm E ∗ (E + b00) ⇒rm E ∗ (I + b00) ⇒rm
E ∗ (a + b00) ⇒rm I ∗ (a + b00) ⇒rm a ∗ (a + b00)
We can conclude that E
∗
⇒rm a ∗ (a + b00)
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 16/2
The Language of the Grammar
If G(V, T, P, S) is a CFG, then the language of G is
L(G) = {w in T∗ | S
∗
⇒G w}
i.e., the set of strings over T derivable from the start symbol.
If G is a CFG, we call L(G) a context-free language.
Example: L(Gpal) is a context-free language.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 17/2
Theorem
A string w ∈ {0, 1}∗ is in L(Gpal) iff w = wR.
Proof: (⊇-direction.) Suppose w = wR, i.e., that w is a palindrome. We show by
induction on |w| that w ∈ L(Gpal)
Basis: Basis: |w| = 0, or |w| = 1. Then w is ǫ , 0, or 1. Since P → ǫ, P → 0 , and
P → 1 are productions, we conclude that P
∗
⇒ w in all base cases.
Induction: Suppose |w| ≥ 2. Since w = wR, we have w = 0x0, or w = 1x1, and
x = xR.
If w = 0x0 we know from the IH that P
∗
⇒ x. Then
P ⇒ 0P0
∗
⇒ 0x0 = w
Thus w ∈ L(Gpal). The case for w = 1x1 is similar.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 18/2
Proof (cont.)
(⊆-direction.) We assume w ∈ L(Gpal) and we prove that w = wR.
Since w ∈ L(Gpal), we have P
∗
⇒ w. We do an induction of the length of
∗
⇒.
Basis: The derivation P
∗
⇒ w is done in one step. Then w must be ǫ,0, or 1, all
palindromes.
Induction: Let n ≥ 1, and suppose the derivation takes n + 1 steps. Then we must have
w = 0x0
∗
⇐ 0P0 ⇐ P
or
w = 1x1
∗
⇐ 1P1 ⇐ P
where the second derivation is done in n steps. By the IH x is a palindrome, and the
inductive proof is complete.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 19/2
Sentential Forms
Let G = (V, T, P, S) be a CFG, and α ∈ (V ∪ T)∗. If
S
∗
⇒ α
we say α is a sentential form.
If S⇒lmα we say that α is a left-sentential form, and if S⇒rmα we say that is a
right-sentential form.
Note: L(G) is those sentential forms that are in T∗.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 20/2
Example
Recall G1:
E → I | E + E | E ∗ E | (E)
I → a | b | Ia | Ib | I0 | I1
1− Then E ∗ (I + E) is a sentential form since
E ⇒ E ∗ E ⇒ E ∗ (E) ⇒ E ∗ (E + E) ⇒ E(I + E)
This derivation is neither leftmost, nor right-most.
2− a ∗ E left-sentential form, since
E ⇒ E ∗ E ⇒ I ∗ E ⇒ a ∗ E
3− E ∗ (E + E) is a right-sentential form since
E ⇒ E ∗ E ⇒ E ∗ (E) ⇒ E ∗ (E + E)
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 21/2
Parse Trees
If w ∈ L(G), for some CFG, then w has a parse tree, which tells us the
(syntactic) struc- ture of w.
w could be a program, a SQL-query, an XML- document, etc.
Parse trees are an alternative representation to derivations and recursive
inferences.
There can be several parse trees for the same string.
Ideally there should be only one parse tree (the "true" structure) for each string,
i.e. the language should be unambiguous.
Unfortunately, we cannot always remove the ambiguity.
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 22/2
Gosta Grahne...
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 23/2
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 24/2
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 25/2
Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 26/2

More Related Content

What's hot

Context free languages
Context free languagesContext free languages
Context free languages
Jahurul Islam
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
Rajendran
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
VenkataRaoS1
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
Sujata Pardeshi
 
Big omega
Big omegaBig omega
Big omega
Rajesh K Shukla
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
FellowBuddy.com
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
Clase afd
Clase afdClase afd
Clase afd
edeciofreitez
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
Shiela Rani
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Autómatas de pila
Autómatas de pila Autómatas de pila
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfa
deepinderbedi
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
ElakkiyaS11
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
Soujanya V
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
AayushSingh233965
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Mohammad Ilyas Malik
 
Push down automata
Push down automataPush down automata
Push down automata
Ratnakar Mikkili
 

What's hot (20)

Context free languages
Context free languagesContext free languages
Context free languages
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Big omega
Big omegaBig omega
Big omega
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Clase afd
Clase afdClase afd
Clase afd
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Simplification of cfg ppt
Simplification of cfg pptSimplification of cfg ppt
Simplification of cfg ppt
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
Autómatas de pila
Autómatas de pila Autómatas de pila
Autómatas de pila
 
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfa
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
language , grammar and automata
language , grammar and automatalanguage , grammar and automata
language , grammar and automata
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
 
Push down automata
Push down automataPush down automata
Push down automata
 

Viewers also liked

Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
Rajendran
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Shiraz316
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
Marina Santini
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Ronak Thakkar
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
Drishti Bhalla
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
Abhimanyu Mishra
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
lavishka_anuj
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
sudhir sharma
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
Marina Santini
 
Reverse polish notation
Reverse polish notationReverse polish notation
Reverse polish notation
grahamwell
 
Cs2303 theory of computation may june 2016
Cs2303 theory of computation may june 2016Cs2303 theory of computation may june 2016
Cs2303 theory of computation may june 2016
appasami
 
Automata
AutomataAutomata
Automata
Gaurav Sinha
 
WMJ&GMBwosc08-Effective Learning & Production Via Modelling
WMJ&GMBwosc08-Effective Learning & Production Via ModellingWMJ&GMBwosc08-Effective Learning & Production Via Modelling
WMJ&GMBwosc08-Effective Learning & Production Via Modelling
Gary Boyd
 
Grammar
GrammarGrammar
Grammar
lavishka_anuj
 
Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notations
jyoti_lakhani
 
Chomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanChomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel Khan
M Khan
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
Rajendran
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
Leyo Stephen
 
Cs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersCs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papers
appasami
 
Introduction to automata
Introduction to automataIntroduction to automata
Introduction to automata
Shubham Bansal
 

Viewers also liked (20)

Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
CNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of ComputationCNF & Leftmost Derivation - Theory of Computation
CNF & Leftmost Derivation - Theory of Computation
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Reverse polish notation
Reverse polish notationReverse polish notation
Reverse polish notation
 
Cs2303 theory of computation may june 2016
Cs2303 theory of computation may june 2016Cs2303 theory of computation may june 2016
Cs2303 theory of computation may june 2016
 
Automata
AutomataAutomata
Automata
 
WMJ&GMBwosc08-Effective Learning & Production Via Modelling
WMJ&GMBwosc08-Effective Learning & Production Via ModellingWMJ&GMBwosc08-Effective Learning & Production Via Modelling
WMJ&GMBwosc08-Effective Learning & Production Via Modelling
 
Grammar
GrammarGrammar
Grammar
 
Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notations
 
Chomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanChomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel Khan
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Cs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papersCs2303 theory of computation all anna University question papers
Cs2303 theory of computation all anna University question papers
 
Introduction to automata
Introduction to automataIntroduction to automata
Introduction to automata
 

Similar to L3 cfg

Theory of computing presentation
Theory of computing presentationTheory of computing presentation
Theory of computing presentation
Md. Touhidur Rahman
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
PEzhumalai
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
viswanath kani
 
hghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggghghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggg
adugnanegero
 
A new technique for proving non regularity based on the measure of a language
A new technique for proving non regularity based on the measure of a languageA new technique for proving non regularity based on the measure of a language
A new technique for proving non regularity based on the measure of a language
Ryoma Sin'ya
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
Srimatre K
 
Thoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's ClassificationThoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's Classification
PrafullMisra
 
practice-final-soln.pdf
practice-final-soln.pdfpractice-final-soln.pdf
practice-final-soln.pdf
T17Rockstar
 
Winter 9 Decidability.pptx
Winter 9 Decidability.pptxWinter 9 Decidability.pptx
Winter 9 Decidability.pptx
HarisPrince
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logic
Darío Garigliotti
 
Computational logic First Order Logic_part2
Computational logic First Order Logic_part2Computational logic First Order Logic_part2
Computational logic First Order Logic_part2
banujahir1
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5
Dr. Maamoun Ahmed
 
Bodecoban ngobaochau
Bodecoban ngobaochauBodecoban ngobaochau
Bodecoban ngobaochau
Duong Tran
 
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptxa7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
pivap22198
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptx
ssuser039bf6
 
Du Calcul des prédicats vers Prolog
Du Calcul des prédicats vers PrologDu Calcul des prédicats vers Prolog
Du Calcul des prédicats vers Prolog
Serge Garlatti
 
Predicate Calculus
Predicate CalculusPredicate Calculus
Predicate Calculus
Serge Garlatti
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...
RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...
RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...
RuleML
 
A Machine-Assisted Proof of Gödel's Incompleteness Theorems
A Machine-Assisted Proof of Gödel's Incompleteness TheoremsA Machine-Assisted Proof of Gödel's Incompleteness Theorems
A Machine-Assisted Proof of Gödel's Incompleteness Theorems
Lawrence Paulson
 

Similar to L3 cfg (20)

Theory of computing presentation
Theory of computing presentationTheory of computing presentation
Theory of computing presentation
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
 
hghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggghghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggg
 
A new technique for proving non regularity based on the measure of a language
A new technique for proving non regularity based on the measure of a languageA new technique for proving non regularity based on the measure of a language
A new technique for proving non regularity based on the measure of a language
 
Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3Formal Languages and Automata Theory unit 3
Formal Languages and Automata Theory unit 3
 
Thoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's ClassificationThoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's Classification
 
practice-final-soln.pdf
practice-final-soln.pdfpractice-final-soln.pdf
practice-final-soln.pdf
 
Winter 9 Decidability.pptx
Winter 9 Decidability.pptxWinter 9 Decidability.pptx
Winter 9 Decidability.pptx
 
Predicate Logic
Predicate LogicPredicate Logic
Predicate Logic
 
Computational logic First Order Logic_part2
Computational logic First Order Logic_part2Computational logic First Order Logic_part2
Computational logic First Order Logic_part2
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5
 
Bodecoban ngobaochau
Bodecoban ngobaochauBodecoban ngobaochau
Bodecoban ngobaochau
 
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptxa7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptx
 
Du Calcul des prédicats vers Prolog
Du Calcul des prédicats vers PrologDu Calcul des prédicats vers Prolog
Du Calcul des prédicats vers Prolog
 
Predicate Calculus
Predicate CalculusPredicate Calculus
Predicate Calculus
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...
RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...
RuleML2015: Similarity-Based Strict Equality in a Fully Integrated Fuzzy Logi...
 
A Machine-Assisted Proof of Gödel's Incompleteness Theorems
A Machine-Assisted Proof of Gödel's Incompleteness TheoremsA Machine-Assisted Proof of Gödel's Incompleteness Theorems
A Machine-Assisted Proof of Gödel's Incompleteness Theorems
 

Recently uploaded

Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 

Recently uploaded (20)

Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 

L3 cfg

  • 1. Context-Free Grammars (CFG) SITE : http://www.sir.blois.univ-tours.fr/˜mirian/ Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 1/2
  • 2. An informal example Language of palindromes: Lpal A palindrome is a string that reads the same forward and backward Ex: otto, madamimadam, 0110, 11011, ǫ Lpal is not a regular language (can be proved by using the pumping lemma) We consider Σ = {0, 1}. There is a natural, recursive definition of when a string of 0 and 1 is in Lpal. Basis: ǫ, 0 and 1 are palindromes Induction: If w is a palindrome, so are 0w0 and 1w1. No string is palindrome of 0 and 1, unless it follows from this basis and inductive rule. A CFG is a formal notation for expressing such recursive definitions of languages Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 2/2
  • 3. What is a grammar? A grammar consists of one or more variables that represent classes of strings (i.e., languages) There are rules that say how the strings in each class are constructed. The construction can use : 1. symbols of the alphabet 2. strings that are already known to be in one of the classes 3. or both Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 3/2
  • 4. A grammar for palindromes In the example of palindromes, we need one variable P which represents the set of palindromes; i.e., the class of strings forming the language Lpal Rules: P → ǫ P → 0 P → 1 P → 0P0 P → 1P1 The first three rules form the basis. They tell us that the class of palindromes includes the strings ǫ, 0 and 1 None of the right sides of theses rules contains a variable, which is why they form a basis for the definition The last two rules form the inductive part of the definition. For instance, rule 4 says that if we take any string w from the class P, then 0w0 is also in class P. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 4/2
  • 5. Definition of Context-Free Grammar A GFG (or just a grammar) G is a tuple G = (V, T, P, S) where 1. V is the (finite) set of variables (or nonterminals or syntactic categories). Each variable represents a language, i.e., a set of strings 2. T is a finite set of terminals, i.e., the symbols that form the strings of the language being defined 3. P is a set of production rules that represent the recursive definition of the language. 4. S is the start symbol that represents the language being defined. Other variables represent auxiliary classes of strings that are used to help define the language of the start symbol. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 5/2
  • 6. Production rules Each production rule consists of: 1. A variable that is being (partially) defined by the production. This variable is often called the head of the production. 2. The production symbol →. 3. A string of zero or more terminals and variables. This string, called the body of the production, represents one way to form strings in the language of the variable of the head. In doing so, we leave terminals unchanged and substitute for each variable of the body any string that is known to be in the language of that variable Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 6/2
  • 7. Compact Notation for Productions We often refers to the production whose head is A as “productions for A” or “A-productions” Moreover, the productions A → α1, A → α2 . . . A → αn can be replaced by the notation A → α1 | α2 | . . . | αn Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 7/2
  • 8. Examples: CFG for palindromes Gpal = ({P}, {0, 1}, A, P) where A represents the production rules: P → ǫ P → 0 P → 1 P → 0P0 P → 1P1 We can also write: P → ǫ | 0 | 1 | 0P0 | 1P1 Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 8/2
  • 9. Examples: CFG for expressions in a typical programming language Operators: + (addition) and ∗ (multiplication) Identifiers: must begin with a or b, which may be followed by any string in {a, b, 0, 1}∗ We need two variables: E: represents expressions. It is the start symbol. I: represents the identifiers. Its language is regular and is the language of the regular expression: (a + b)(a + b + 0 + 1)∗ Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 9/2
  • 10. Exemples (cont.): The grammar Grammar G1 = ({E, I}, T, P, E) where: T = {+, ∗, (, ), a, b, 0, 1} and P is the set of productions: 1 E → I 2 E → E + E 3 E → E ∗ E 4 E → (E) 5 I → a 6 I → b 7 I → Ia 8 I → Ib 9 I → I0 10 I → I1 Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 10/2
  • 11. Derivations Using a Grammar We apply the productions of a CFG to infer that certain strings are in the language of a certain variable Two inference approaches: 1. Recursive inference, using productions from body to head 2. Derivations, using productions from head to body Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 11/2
  • 12. Recursive inference - an exemple We consider some inferences we can make using G1 Recall G1: E → I | E + E | E ∗ E | (E) I → a | b | Ia | Ib | I0 | I1 String Lang Prod String(s) used (i) a I 5 - (ii) b I 6 - (iii) b0 I 9 (ii) (iv) b00 I 9 (iii) (v) a E 1 (i) (vi) b00 E 1 (iv) (vii) a + b00 E 2 (v), (vi) (viii) (a + b00) E 4 (vii) (ix) a (a + b00) E 3 (v), (viii) Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 12/2
  • 13. Derivations Applying productions from head to body requires the definition of a new relational symbol: ⇒ Let: G = (V, T, P, S) be a CFG A ∈ V α, β ⊂ (V ∪ T)∗ and A → γ ∈ P Then we write αAβ ⇒G αγβ or, if G is understood αAβ ⇒ αγβ and say that αAβ derives αγβ. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 13/2
  • 14. Zero or more derivation steps We define ∗ ⇒ to be the reflexive and transitive closure of ⇒ (i.e., to denote zero or more derivation steps): Basis: Let α ∈ (V ∪ T)∗. Then α ∗ ⇒ α. Induction: If α ∗ ⇒ β and β ⇒ γ , then α ∗ ⇒ γ . Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 14/2
  • 15. Examples of derivation Derivation of a ∗ (a + b000) by G1 E ⇒ E ∗ E ⇒ I ∗ E ⇒ a ∗ E ⇒ a ∗ (E) ⇒ a ∗ (E + E) ⇒ a ∗ (I + E) ⇒ a ∗ (a + E) ⇒ a ∗ (a + I) ⇒ a ∗ (a + I0) ⇒ a ∗ (a + I00) ⇒ a ∗ (a + b00) Note 1: At each step we might have several rules to choose from, e.g. I ∗ E ⇒ a ∗ E ⇒ a ∗ (E), versus I ∗ E ⇒ I ∗ (E) ⇒ a ∗ (E). Note 2: Not all choices lead to successful derivations of a particular string, for instance E ⇒ E + E (at the first step) won’t lead to a derivation of a ∗ (a + b000). Important: Recursive inference and derivation are equivalent. A string of terminals w is infered to be in the language of some variable A iff A ∗ ⇒ w Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 15/2
  • 16. Leftmost and Rightmost derivation In other to restrict the number of choices we have in deriving a string, it is often useful to require that at each step we replace the leftmost (or rightmost) variable by one of its production rules Leftmost derivation ⇒lm : Always replace the left-most variable by one of its rule-bodies Rightmost derivation ⇒rm : Always replace the rightmost variable by one of its rule-bodies. EXAMPLES 1− Leftmost derivation: previous example 2− Rightmost derivation: E ⇒rm E ∗ E ⇒rm E ∗ (E) ⇒rm E ∗ (E + E) ⇒rm E ∗ (E + I) ⇒rm E ∗ (E + I0) ⇒rm E ∗ (E + I00) ⇒rm E ∗ (E + b00) ⇒rm E ∗ (I + b00) ⇒rm E ∗ (a + b00) ⇒rm I ∗ (a + b00) ⇒rm a ∗ (a + b00) We can conclude that E ∗ ⇒rm a ∗ (a + b00) Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 16/2
  • 17. The Language of the Grammar If G(V, T, P, S) is a CFG, then the language of G is L(G) = {w in T∗ | S ∗ ⇒G w} i.e., the set of strings over T derivable from the start symbol. If G is a CFG, we call L(G) a context-free language. Example: L(Gpal) is a context-free language. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 17/2
  • 18. Theorem A string w ∈ {0, 1}∗ is in L(Gpal) iff w = wR. Proof: (⊇-direction.) Suppose w = wR, i.e., that w is a palindrome. We show by induction on |w| that w ∈ L(Gpal) Basis: Basis: |w| = 0, or |w| = 1. Then w is ǫ , 0, or 1. Since P → ǫ, P → 0 , and P → 1 are productions, we conclude that P ∗ ⇒ w in all base cases. Induction: Suppose |w| ≥ 2. Since w = wR, we have w = 0x0, or w = 1x1, and x = xR. If w = 0x0 we know from the IH that P ∗ ⇒ x. Then P ⇒ 0P0 ∗ ⇒ 0x0 = w Thus w ∈ L(Gpal). The case for w = 1x1 is similar. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 18/2
  • 19. Proof (cont.) (⊆-direction.) We assume w ∈ L(Gpal) and we prove that w = wR. Since w ∈ L(Gpal), we have P ∗ ⇒ w. We do an induction of the length of ∗ ⇒. Basis: The derivation P ∗ ⇒ w is done in one step. Then w must be ǫ,0, or 1, all palindromes. Induction: Let n ≥ 1, and suppose the derivation takes n + 1 steps. Then we must have w = 0x0 ∗ ⇐ 0P0 ⇐ P or w = 1x1 ∗ ⇐ 1P1 ⇐ P where the second derivation is done in n steps. By the IH x is a palindrome, and the inductive proof is complete. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 19/2
  • 20. Sentential Forms Let G = (V, T, P, S) be a CFG, and α ∈ (V ∪ T)∗. If S ∗ ⇒ α we say α is a sentential form. If S⇒lmα we say that α is a left-sentential form, and if S⇒rmα we say that is a right-sentential form. Note: L(G) is those sentential forms that are in T∗. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 20/2
  • 21. Example Recall G1: E → I | E + E | E ∗ E | (E) I → a | b | Ia | Ib | I0 | I1 1− Then E ∗ (I + E) is a sentential form since E ⇒ E ∗ E ⇒ E ∗ (E) ⇒ E ∗ (E + E) ⇒ E(I + E) This derivation is neither leftmost, nor right-most. 2− a ∗ E left-sentential form, since E ⇒ E ∗ E ⇒ I ∗ E ⇒ a ∗ E 3− E ∗ (E + E) is a right-sentential form since E ⇒ E ∗ E ⇒ E ∗ (E) ⇒ E ∗ (E + E) Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 21/2
  • 22. Parse Trees If w ∈ L(G), for some CFG, then w has a parse tree, which tells us the (syntactic) struc- ture of w. w could be a program, a SQL-query, an XML- document, etc. Parse trees are an alternative representation to derivations and recursive inferences. There can be several parse trees for the same string. Ideally there should be only one parse tree (the "true" structure) for each string, i.e. the language should be unambiguous. Unfortunately, we cannot always remove the ambiguity. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 22/2
  • 23. Gosta Grahne... Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 23/2
  • 24. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 24/2
  • 25. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 25/2
  • 26. Automata Theory, Languages and Computation - M´ırian Halfeld-Ferrari – p. 26/2