SlideShare a Scribd company logo
Costas Busch - LSU 1
Context-Free Languages
Costas Busch - LSU 2
Regular Languages
}0:{ nba nn }{ R
ww
**ba *)( ba 
Context-Free Languages
Costas Busch - LSU 3
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
Costas Busch - LSU 4
Context-Free Grammars
Costas Busch - LSU 5
Grammars
Grammars express languages
Example: the English language grammar
verbpredicate
nounarticlephrasenoun
predicatephrasenounsentence



_
_
Costas Busch - LSU 6
sleepsverb
runsverb
dognoun
catnoun
thearticle
aarticle






Costas Busch - LSU 7
Derivation of string “the dog sleeps”:
sleepsdogthe
verbdogthe
verbnounthe
verbnounarticle
verbphrasenoun
predicatephrasenounsentence






_
_
Costas Busch - LSU 8
Derivation of string “a cat runs”:
runscata
verbcata
verbnouna
verbnounarticle
verbphrasenoun
predicatephrasenounsentence






_
_
Costas Busch - LSU 9
Language of the grammar:
L = { “a cat runs”,
“a cat sleeps”,
“the cat runs”,
“the cat sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
Costas Busch - LSU 10
catnoun 
Variables
Sequence of
Terminals (symbols)
Productions
predicatephrasenounsentence _
Sequence of Variables
Costas Busch - LSU 11
Another Example


S
aSbSGrammar:
Variable
Sequence of
terminals and variables
The right side
may be 
Costas Busch - LSU 12
Grammar:
Derivation of string :


S
aSbS
abaSbS 
ab
aSbS  S
Costas Busch - LSU 13
Grammar:
Derivation of string :
aabbaaSbbaSbS 
aSbS  S
aabb


S
aSbS
Costas Busch - LSU 14
aaabbbaaaSbbbaaSbbaSbS 
aaaabbbbaaaaSbbbb
aaaSbbbaaSbbaSbS


Other derivations:


S
aSbSGrammar:
Costas Busch - LSU 15


S
aSbS
}0:{  nbaL nn
Grammar:
Language of the grammar

Costas Busch - LSU 16
We write:
Instead of:
aaabbbS
*

aaabbbaaaSbbbaaSbbaSbS 
for zero or more derivation steps
A Convenient Notation
Costas Busch - LSU 17
nww
*
1 
nwwww  321
in zero or more derivation steps
In general we write:
If:
ww
*
Trivially:
Costas Busch - LSU 18


S
aSbS
aaabbbS
abS
S
*
*
*



Example Grammar Possible Derivations
baaaaaSbbbbaaSbbS


Costas Busch - LSU 19


S
aSbS
|aSbS 
thearticle
aarticle


theaarticle |
Another convenient notation:
Costas Busch - LSU 20
Formal Definitions
 PSTVG ,,,
Set of
variables
Set of
terminal
symbols
Start
variable
Set of
productions
Grammar:
Costas Busch - LSU 21
Context-Free Grammar:
All productions in are of the form
sA 
String of
variables and
terminals
),,,( PSTVG 
P
Variable
Costas Busch - LSU 22
|aSbS 
 PSTVG ,,,
}{SV 
},{ baT 
},{  SaSbSP
variables
terminals
productions
start variable
Example of Context-Free Grammar
Costas Busch - LSU 23
For a grammar with start variableG S
String of terminals or
*},:{)(
*
TwwSwGL 

Language of a Grammar:
Costas Busch - LSU 24
context-free grammar :
}0:{)(  nbaGL nn
nn
baS


G
Example:
Since, there is derivation
for any 0n
|aSbS 
Costas Busch - LSU 25
A language is context-free
if there is a context-free grammar
with
L
G
)(GLL 
Context-Free Language definition:
Costas Busch - LSU 26
since context-free grammar :
}0:{  nbaL nn
G
Example:
is a context-free language
generates LGL )(
|aSbS 
Costas Busch - LSU 27
||bSbaSaS 
abbaabSbaaSaS 
Context-free grammar :G
Example derivations:
abaabaabaSabaabSbaaSaS 
)(GL }*},{:{ bawwwR

Palindromes of even length
Another Example
Costas Busch - LSU 28
|| SSaSbS 
ababSaSbSSSS 
Context-free grammar :G
Example derivations:
abababaSbabSaSbSSSS 
}prefixanyin
)()(and
),()(:{
v
vnvn
wnwnw
ba
ba

)(GL
() ((( ))) (( ))
Describes
matched
parentheses: )b(, a
Another Example
Costas Busch - LSU 29
Derivation Order
and
Derivation Trees
Costas Busch - LSU 30
Derivation Order
Consider the following example grammar
with 5 productions:
ABS .1


A
aaAA
.3
.2


B
BbB
.5
.4
Costas Busch - LSU 31
aabaaBbaaBaaABABS
54321

Leftmost derivation order of string aab :
At each step, we substitute the
leftmost variable
ABS .1


A
aaAA
.3
.2


B
BbB
.5
.4
Derivation of aab
Costas Busch - LSU 32
aabaaAbAbABbABS
32541

Rightmost derivation order of string aab:
At each step, we substitute the
rightmost variable
ABS .1


A
aaAA
.3
.2


B
BbB
.5
.4
Derivation of aab
Costas Busch - LSU 33
aabaaAbAbABbABS
32541

Rightmost derivation of :aab
aabaaBbaaBaaABABS
54321

Leftmost derivation of :aab
ABS .1


A
aaAA
.3
.2


B
BbB
.5
.4
Costas Busch - LSU 34
Derivation Trees
Consider the same example grammar:
aabaaBbaaABbaaABABS 
And a derivation of :aab
ABS  |aaAA  |BbB 
Costas Busch - LSU 35
ABS 
S
BA
ABS  |aaAA  |BbB 
yield AB
Costas Busch - LSU 36
aaABABS 
a a A
S
BA
ABS  |aaAA  |BbB 
yield aaAB
Costas Busch - LSU 37
aaABbaaABABS 
S
BA
a a A B b
ABS  |aaAA  |BbB 
yield aaABb
Costas Busch - LSU 38
aaBbaaABbaaABABS 
S
BA
a a A B b

ABS  |aaAA  |BbB 
yield
aaBbBbaa 
Costas Busch - LSU 39
aabaaBbaaABbaaABABS 
yield
aabbaa 
S
BA
a a A B b
 
Derivation Tree
ABS  |aaAA  |BbB 
(parse tree)
Costas Busch - LSU 40
aabaaBbaaBaaABABS 
aabaaAbAbABbABS 
TRY IT OUT!
Sometimes, derivation order doesn’t matter
Leftmost derivation:
Rightmost derivation:
Costas Busch - LSU 41
aabaaBbaaBaaABABS 
aabaaAbAbABbABS 
S
BA
a a A B b
 
Give same
derivation tree
Sometimes, derivation order doesn’t matter
Leftmost derivation:
Rightmost derivation:
Costas Busch - LSU 42
Ambiguity
Costas Busch - LSU 43
Grammar for mathematical expressions
))(()( aaaaaaa 
Example strings:
Denotes any number
aEEEEEE |)(|| 
Costas Busch - LSU 44
A leftmost derivation
for aaa 
aaaEaa
EEaEaEEE
*

aEEEEEE |)(|| 
PARSE TREE ???
Costas Busch - LSU 45
A leftmost derivation
for aaa 
E
EE
EE

a
a a

aaaEaa
EEaEaEEE
*

aEEEEEE |)(|| 
Costas Busch - LSU 46
aaaEaa
EEaEEEEEE


Another
leftmost derivation
for
aEEEEEE |)(|| 
aaa 
PARSE TREE ???
Costas Busch - LSU 47
E
EE

a a

EE a
aaaEaa
EEaEEEEEE


Another
leftmost derivation
for
aEEEEEE |)(|| 
aaa 
Costas Busch - LSU 48
aaa  E
EE

a a

EE a
E
EE
EE

a
a a

Two derivation trees
for
aEEEEEE |)(|| 
Costas Busch - LSU 49
E
EE


EE
E
EE
EE

2
2 2 2 2
2
222  aaa
take 2a
Costas Busch - LSU 50
E
EE


EE
E
EE
EE


6222 
2
2 2 2 2
2
8222 
4
2 2
2
6
2 2
24
8
Good Tree Bad Tree
Compute expression result
using the tree
Costas Busch - LSU 51
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
Costas Busch - LSU 52
Ambiguous Grammar:
A context-free grammar is ambiguous
if there is a string which has:
two different derivation trees
or
two leftmost derivations
G
)(GLw
(Two different derivation trees give two
different leftmost derivations and vice-versa)
Costas Busch - LSU 53
E
EE

a a

EE a
E
EE
EE

a
a a

string aaa  has two derivation trees
aEEEEEE |)(|| 
this grammar is ambiguous since
Example:
Costas Busch - LSU 54
string aaa  has two leftmost derivations
aaaEaa
EEaEEEEEE


aaaEaa
EEaEaEEE
*

aEEEEEE |)(|| 
this grammar is ambiguous also because
Costas Busch - LSU 55
IF_STMT if EXPR then STMT
| if EXPR then STMT else STMT
Another ambiguous grammar:
Variables Terminals
Very common piece of grammar
in programming languages
Costas Busch - LSU 56
If expr1 then if expr2 then stmt1 else stmt2
TWO
Parse Trees
or
Derivation Trees
???
Costas Busch - LSU 57
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
expr1 then
elseif expr2 then
STMT
stmt1
if
IF_STMT
expr1 then else
if expr2 then
STMT stmt2if
stmt1
stmt2
Two derivation trees
Costas Busch - LSU 58
In general, ambiguity is bad
and we want to remove it
Sometimes it is possible to find
a non-ambiguous grammar for a language
But, in general ιt is difficult to achieve this
Costas Busch - LSU 59
aE
EE
EEE
EEE




)(
aEF
FFTT
TTEE
|)(
|
|



Ambiguous
Grammar
Non-Ambiguous
Grammar
Equivalent
generates the same
language
A successful example:
Costas Busch - LSU 60
aaaFaaFFa
FTaTaTFTTTEE


E
E T
T  F
F
a
T
F
a
a
aEF
FFTT
TTEE
|)(
|
|



Unique
derivation tree
for aaa 
Costas Busch - LSU 61
}{}{ mmnmnn
cbacbaL 
0, mn
An un-successful example:
every grammar that generates this
language is ambiguous
L is inherently ambiguous:
Costas Busch - LSU 62
}{}{ mmnmnn
cbacbaL 
|
|11
aAbA
AcSS


|
|22
bBcB
BaSS

21 | SSS 
Example (ambiguous) grammar for :L
Costas Busch - LSU 63
The string Lcba nnn

has always two different derivation trees
(for any grammar)
S
1S
S
2S
1S c 2Sa
For example

More Related Content

What's hot

context free language
context free languagecontext free language
context free language
khush_boo31
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
Ruchika Sinha
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
shah zeb
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Ronak Thakkar
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
Marina Santini
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
maamir farooq
 
Finite automata
Finite automataFinite automata
Finite automata
Dr. Abhineet Anand
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
Marina Santini
 
Finite automata
Finite automataFinite automata
Finite automata
Bipul Roy Bpl
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
parmeet834
 
Pumping lemma for cfl
Pumping lemma for cflPumping lemma for cfl
Pumping lemma for cfl
Muhammad Zohaib Chaudhary
 
TOC 5 | Regular Expressions
TOC 5 | Regular ExpressionsTOC 5 | Regular Expressions
TOC 5 | Regular Expressions
Mohammad Imam Hossain
 
Language
LanguageLanguage
Language
Mobeen Mustafa
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
Bipul Roy Bpl
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
shah zeb
 
L3 cfg
L3 cfgL3 cfg
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
TOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal FormTOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal Form
Mohammad Imam Hossain
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
Farooq Mian
 
Context free languages
Context free languagesContext free languages
Context free languages
Jahurul Islam
 

What's hot (20)

context free language
context free languagecontext free language
context free language
 
Regular Grammar
Regular GrammarRegular Grammar
Regular Grammar
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Finite automata
Finite automataFinite automata
Finite automata
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Finite automata
Finite automataFinite automata
Finite automata
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Pumping lemma for cfl
Pumping lemma for cflPumping lemma for cfl
Pumping lemma for cfl
 
TOC 5 | Regular Expressions
TOC 5 | Regular ExpressionsTOC 5 | Regular Expressions
TOC 5 | Regular Expressions
 
Language
LanguageLanguage
Language
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
L3 cfg
L3 cfgL3 cfg
L3 cfg
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
TOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal FormTOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal Form
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Context free languages
Context free languagesContext free languages
Context free languages
 

Viewers also liked

Context free langauges
Context free langaugesContext free langauges
Context free langauges
sudhir sharma
 
Class7
 Class7 Class7
Class7
issbp
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
danhumble
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
lavishka_anuj
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
deepinderbedi
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
lavishka_anuj
 
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
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?
Animesh Chaturvedi
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
Abhimanyu Mishra
 
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
 
Parsing
ParsingParsing
Parsing
Tech_MX
 
Push Down Automata (PDA)
Push Down Automata (PDA)Push Down Automata (PDA)
Push Down Automata (PDA)
Animesh Chaturvedi
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
Rajendran
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
Edhole.com
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause Grammars
CS, NcState
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
bolovv
 
Dfa example
Dfa   exampleDfa   example
Dfa example
saerrii
 
Grammar
GrammarGrammar
Grammar
lavishka_anuj
 
Ambiguty
AmbigutyAmbiguty
Ambiguty
Rajendran
 
Class8
 Class8 Class8
Class8
issbp
 

Viewers also liked (20)

Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Class7
 Class7 Class7
Class7
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
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
 
Parsing
ParsingParsing
Parsing
 
Push Down Automata (PDA)
Push Down Automata (PDA)Push Down Automata (PDA)
Push Down Automata (PDA)
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
Lecture 7: Definite Clause Grammars
Lecture 7: Definite Clause GrammarsLecture 7: Definite Clause Grammars
Lecture 7: Definite Clause Grammars
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Dfa example
Dfa   exampleDfa   example
Dfa example
 
Grammar
GrammarGrammar
Grammar
 
Ambiguty
AmbigutyAmbiguty
Ambiguty
 
Class8
 Class8 Class8
Class8
 

More from Shiraz316

FINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSIS
FINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSISFINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSIS
FINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSIS
Shiraz316
 
K neareast neighbor algorithm presentation
K neareast neighbor algorithm presentationK neareast neighbor algorithm presentation
K neareast neighbor algorithm presentation
Shiraz316
 
Khi 087 revised dt report
Khi 087 revised dt reportKhi 087 revised dt report
Khi 087 revised dt report
Shiraz316
 
Khi 117 revised dt report
Khi 117 revised dt reportKhi 117 revised dt report
Khi 117 revised dt report
Shiraz316
 
Khi 164 revised dt report
Khi 164 revised dt reportKhi 164 revised dt report
Khi 164 revised dt report
Shiraz316
 
Khi 054 revised dt report
Khi 054 revised dt reportKhi 054 revised dt report
Khi 054 revised dt report
Shiraz316
 
Khi 061 revised dt report
Khi 061 revised dt reportKhi 061 revised dt report
Khi 061 revised dt report
Shiraz316
 
Khi 022 revised dt report
Khi 022 revised dt reportKhi 022 revised dt report
Khi 022 revised dt report
Shiraz316
 
Khi 113 revised dt report
Khi 113 revised dt reportKhi 113 revised dt report
Khi 113 revised dt report
Shiraz316
 
Khi 156 revised dt report
Khi 156 revised dt reportKhi 156 revised dt report
Khi 156 revised dt report
Shiraz316
 
Khi 142 revised dt report
Khi 142 revised dt reportKhi 142 revised dt report
Khi 142 revised dt report
Shiraz316
 
Khi 316 revised dt report
Khi 316 revised dt reportKhi 316 revised dt report
Khi 316 revised dt report
Shiraz316
 
Khi 186 revised dt report
Khi 186 revised dt reportKhi 186 revised dt report
Khi 186 revised dt report
Shiraz316
 
Khi 052 revised dt report
Khi 052 revised dt reportKhi 052 revised dt report
Khi 052 revised dt report
Shiraz316
 
Khi 151 revised dt report
Khi 151 revised dt reportKhi 151 revised dt report
Khi 151 revised dt report
Shiraz316
 
Khi 150 revised dt report
Khi 150 revised dt reportKhi 150 revised dt report
Khi 150 revised dt report
Shiraz316
 
Khi 078 revised dt report
Khi 078 revised dt reportKhi 078 revised dt report
Khi 078 revised dt report
Shiraz316
 
LTE quick introduction session Training
LTE quick introduction session TrainingLTE quick introduction session Training
LTE quick introduction session Training
Shiraz316
 
LTE Sales Training for CPE Device
LTE Sales Training for CPE DeviceLTE Sales Training for CPE Device
LTE Sales Training for CPE Device
Shiraz316
 
Witribe Customer Premises Equipment Training for LTE
Witribe Customer Premises Equipment Training for LTEWitribe Customer Premises Equipment Training for LTE
Witribe Customer Premises Equipment Training for LTE
Shiraz316
 

More from Shiraz316 (20)

FINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSIS
FINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSISFINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSIS
FINGERNAIL DISORDER DETECTION FOR DISEASE ANALYSIS
 
K neareast neighbor algorithm presentation
K neareast neighbor algorithm presentationK neareast neighbor algorithm presentation
K neareast neighbor algorithm presentation
 
Khi 087 revised dt report
Khi 087 revised dt reportKhi 087 revised dt report
Khi 087 revised dt report
 
Khi 117 revised dt report
Khi 117 revised dt reportKhi 117 revised dt report
Khi 117 revised dt report
 
Khi 164 revised dt report
Khi 164 revised dt reportKhi 164 revised dt report
Khi 164 revised dt report
 
Khi 054 revised dt report
Khi 054 revised dt reportKhi 054 revised dt report
Khi 054 revised dt report
 
Khi 061 revised dt report
Khi 061 revised dt reportKhi 061 revised dt report
Khi 061 revised dt report
 
Khi 022 revised dt report
Khi 022 revised dt reportKhi 022 revised dt report
Khi 022 revised dt report
 
Khi 113 revised dt report
Khi 113 revised dt reportKhi 113 revised dt report
Khi 113 revised dt report
 
Khi 156 revised dt report
Khi 156 revised dt reportKhi 156 revised dt report
Khi 156 revised dt report
 
Khi 142 revised dt report
Khi 142 revised dt reportKhi 142 revised dt report
Khi 142 revised dt report
 
Khi 316 revised dt report
Khi 316 revised dt reportKhi 316 revised dt report
Khi 316 revised dt report
 
Khi 186 revised dt report
Khi 186 revised dt reportKhi 186 revised dt report
Khi 186 revised dt report
 
Khi 052 revised dt report
Khi 052 revised dt reportKhi 052 revised dt report
Khi 052 revised dt report
 
Khi 151 revised dt report
Khi 151 revised dt reportKhi 151 revised dt report
Khi 151 revised dt report
 
Khi 150 revised dt report
Khi 150 revised dt reportKhi 150 revised dt report
Khi 150 revised dt report
 
Khi 078 revised dt report
Khi 078 revised dt reportKhi 078 revised dt report
Khi 078 revised dt report
 
LTE quick introduction session Training
LTE quick introduction session TrainingLTE quick introduction session Training
LTE quick introduction session Training
 
LTE Sales Training for CPE Device
LTE Sales Training for CPE DeviceLTE Sales Training for CPE Device
LTE Sales Training for CPE Device
 
Witribe Customer Premises Equipment Training for LTE
Witribe Customer Premises Equipment Training for LTEWitribe Customer Premises Equipment Training for LTE
Witribe Customer Premises Equipment Training for LTE
 

Recently uploaded

Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...
Open Access Research Paper
 
DENR-PENRO-Bulacan-Presentation Philippine EIS
DENR-PENRO-Bulacan-Presentation Philippine EISDENR-PENRO-Bulacan-Presentation Philippine EIS
DENR-PENRO-Bulacan-Presentation Philippine EIS
MarlonJayBayag
 
Global Peatlands Map and Hotspot Explanation Atlas
Global Peatlands Map and Hotspot Explanation AtlasGlobal Peatlands Map and Hotspot Explanation Atlas
Global Peatlands Map and Hotspot Explanation Atlas
Global Landscapes Forum (GLF)
 
Enhanced action and stakeholder engagement for sustainable peatland management
Enhanced action and stakeholder engagement for sustainable peatland managementEnhanced action and stakeholder engagement for sustainable peatland management
Enhanced action and stakeholder engagement for sustainable peatland management
Global Landscapes Forum (GLF)
 
Top 8 Strategies for Effective Sustainable Waste Management.pdf
Top 8 Strategies for Effective Sustainable Waste Management.pdfTop 8 Strategies for Effective Sustainable Waste Management.pdf
Top 8 Strategies for Effective Sustainable Waste Management.pdf
Jhon Wick
 
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
zm9ajxup
 
Q&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service PlaybookQ&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service Playbook
World Resources Institute (WRI)
 
How about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shopHow about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shop
laozhuseo02
 
Summary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of AustraliaSummary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of Australia
yasmindemoraes1
 
Global Climate Change and global warming
Global Climate Change and global warmingGlobal Climate Change and global warming
Global Climate Change and global warming
ballkicker20
 
Microbial characterisation and identification, and potability of River Kuywa ...
Microbial characterisation and identification, and potability of River Kuywa ...Microbial characterisation and identification, and potability of River Kuywa ...
Microbial characterisation and identification, and potability of River Kuywa ...
Open Access Research Paper
 
Climate Change All over the World .pptx
Climate Change All over the World  .pptxClimate Change All over the World  .pptx
Climate Change All over the World .pptx
sairaanwer024
 
Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.
EpconLP
 
Drip Irrigation technology with solar power
Drip Irrigation technology with solar powerDrip Irrigation technology with solar power
Drip Irrigation technology with solar power
anikchanda4
 
Overview of the Global Peatlands Assessment
Overview of the Global Peatlands AssessmentOverview of the Global Peatlands Assessment
Overview of the Global Peatlands Assessment
Global Landscapes Forum (GLF)
 
Wildlife-AnIntroduction.pdf so that you know more about our environment
Wildlife-AnIntroduction.pdf so that you know more about our environmentWildlife-AnIntroduction.pdf so that you know more about our environment
Wildlife-AnIntroduction.pdf so that you know more about our environment
amishajha2407
 
different Modes of Insect Plant Interaction
different Modes of Insect Plant Interactiondifferent Modes of Insect Plant Interaction
different Modes of Insect Plant Interaction
Archita Das
 
Peatland Management in Indonesia, Science to Policy and Knowledge Education
Peatland Management in Indonesia, Science to Policy and Knowledge EducationPeatland Management in Indonesia, Science to Policy and Knowledge Education
Peatland Management in Indonesia, Science to Policy and Knowledge Education
Global Landscapes Forum (GLF)
 
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
p2npnqp
 
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Joshua Orris
 

Recently uploaded (20)

Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...Improving the viability of probiotics by encapsulation methods for developmen...
Improving the viability of probiotics by encapsulation methods for developmen...
 
DENR-PENRO-Bulacan-Presentation Philippine EIS
DENR-PENRO-Bulacan-Presentation Philippine EISDENR-PENRO-Bulacan-Presentation Philippine EIS
DENR-PENRO-Bulacan-Presentation Philippine EIS
 
Global Peatlands Map and Hotspot Explanation Atlas
Global Peatlands Map and Hotspot Explanation AtlasGlobal Peatlands Map and Hotspot Explanation Atlas
Global Peatlands Map and Hotspot Explanation Atlas
 
Enhanced action and stakeholder engagement for sustainable peatland management
Enhanced action and stakeholder engagement for sustainable peatland managementEnhanced action and stakeholder engagement for sustainable peatland management
Enhanced action and stakeholder engagement for sustainable peatland management
 
Top 8 Strategies for Effective Sustainable Waste Management.pdf
Top 8 Strategies for Effective Sustainable Waste Management.pdfTop 8 Strategies for Effective Sustainable Waste Management.pdf
Top 8 Strategies for Effective Sustainable Waste Management.pdf
 
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
一比一原版(UMTC毕业证书)明尼苏达大学双城分校毕业证如何办理
 
Q&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service PlaybookQ&A with the Experts: The Food Service Playbook
Q&A with the Experts: The Food Service Playbook
 
How about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shopHow about Huawei mobile phone-www.cfye-commerce.shop
How about Huawei mobile phone-www.cfye-commerce.shop
 
Summary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of AustraliaSummary of the Climate and Energy Policy of Australia
Summary of the Climate and Energy Policy of Australia
 
Global Climate Change and global warming
Global Climate Change and global warmingGlobal Climate Change and global warming
Global Climate Change and global warming
 
Microbial characterisation and identification, and potability of River Kuywa ...
Microbial characterisation and identification, and potability of River Kuywa ...Microbial characterisation and identification, and potability of River Kuywa ...
Microbial characterisation and identification, and potability of River Kuywa ...
 
Climate Change All over the World .pptx
Climate Change All over the World  .pptxClimate Change All over the World  .pptx
Climate Change All over the World .pptx
 
Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.Epcon is One of the World's leading Manufacturing Companies.
Epcon is One of the World's leading Manufacturing Companies.
 
Drip Irrigation technology with solar power
Drip Irrigation technology with solar powerDrip Irrigation technology with solar power
Drip Irrigation technology with solar power
 
Overview of the Global Peatlands Assessment
Overview of the Global Peatlands AssessmentOverview of the Global Peatlands Assessment
Overview of the Global Peatlands Assessment
 
Wildlife-AnIntroduction.pdf so that you know more about our environment
Wildlife-AnIntroduction.pdf so that you know more about our environmentWildlife-AnIntroduction.pdf so that you know more about our environment
Wildlife-AnIntroduction.pdf so that you know more about our environment
 
different Modes of Insect Plant Interaction
different Modes of Insect Plant Interactiondifferent Modes of Insect Plant Interaction
different Modes of Insect Plant Interaction
 
Peatland Management in Indonesia, Science to Policy and Knowledge Education
Peatland Management in Indonesia, Science to Policy and Knowledge EducationPeatland Management in Indonesia, Science to Policy and Knowledge Education
Peatland Management in Indonesia, Science to Policy and Knowledge Education
 
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
原版制作(Newcastle毕业证书)纽卡斯尔大学毕业证在读证明一模一样
 
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
Optimizing Post Remediation Groundwater Performance with Enhanced Microbiolog...
 

Context free grammars

  • 1. Costas Busch - LSU 1 Context-Free Languages
  • 2. Costas Busch - LSU 2 Regular Languages }0:{ nba nn }{ R ww **ba *)( ba  Context-Free Languages
  • 3. Costas Busch - LSU 3 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
  • 4. Costas Busch - LSU 4 Context-Free Grammars
  • 5. Costas Busch - LSU 5 Grammars Grammars express languages Example: the English language grammar verbpredicate nounarticlephrasenoun predicatephrasenounsentence    _ _
  • 6. Costas Busch - LSU 6 sleepsverb runsverb dognoun catnoun thearticle aarticle      
  • 7. Costas Busch - LSU 7 Derivation of string “the dog sleeps”: sleepsdogthe verbdogthe verbnounthe verbnounarticle verbphrasenoun predicatephrasenounsentence       _ _
  • 8. Costas Busch - LSU 8 Derivation of string “a cat runs”: runscata verbcata verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence       _ _
  • 9. Costas Busch - LSU 9 Language of the grammar: L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” }
  • 10. Costas Busch - LSU 10 catnoun  Variables Sequence of Terminals (symbols) Productions predicatephrasenounsentence _ Sequence of Variables
  • 11. Costas Busch - LSU 11 Another Example   S aSbSGrammar: Variable Sequence of terminals and variables The right side may be 
  • 12. Costas Busch - LSU 12 Grammar: Derivation of string :   S aSbS abaSbS  ab aSbS  S
  • 13. Costas Busch - LSU 13 Grammar: Derivation of string : aabbaaSbbaSbS  aSbS  S aabb   S aSbS
  • 14. Costas Busch - LSU 14 aaabbbaaaSbbbaaSbbaSbS  aaaabbbbaaaaSbbbb aaaSbbbaaSbbaSbS   Other derivations:   S aSbSGrammar:
  • 15. Costas Busch - LSU 15   S aSbS }0:{  nbaL nn Grammar: Language of the grammar 
  • 16. Costas Busch - LSU 16 We write: Instead of: aaabbbS *  aaabbbaaaSbbbaaSbbaSbS  for zero or more derivation steps A Convenient Notation
  • 17. Costas Busch - LSU 17 nww * 1  nwwww  321 in zero or more derivation steps In general we write: If: ww * Trivially:
  • 18. Costas Busch - LSU 18   S aSbS aaabbbS abS S * * *    Example Grammar Possible Derivations baaaaaSbbbbaaSbbS  
  • 19. Costas Busch - LSU 19   S aSbS |aSbS  thearticle aarticle   theaarticle | Another convenient notation:
  • 20. Costas Busch - LSU 20 Formal Definitions  PSTVG ,,, Set of variables Set of terminal symbols Start variable Set of productions Grammar:
  • 21. Costas Busch - LSU 21 Context-Free Grammar: All productions in are of the form sA  String of variables and terminals ),,,( PSTVG  P Variable
  • 22. Costas Busch - LSU 22 |aSbS   PSTVG ,,, }{SV  },{ baT  },{  SaSbSP variables terminals productions start variable Example of Context-Free Grammar
  • 23. Costas Busch - LSU 23 For a grammar with start variableG S String of terminals or *},:{)( * TwwSwGL   Language of a Grammar:
  • 24. Costas Busch - LSU 24 context-free grammar : }0:{)(  nbaGL nn nn baS   G Example: Since, there is derivation for any 0n |aSbS 
  • 25. Costas Busch - LSU 25 A language is context-free if there is a context-free grammar with L G )(GLL  Context-Free Language definition:
  • 26. Costas Busch - LSU 26 since context-free grammar : }0:{  nbaL nn G Example: is a context-free language generates LGL )( |aSbS 
  • 27. Costas Busch - LSU 27 ||bSbaSaS  abbaabSbaaSaS  Context-free grammar :G Example derivations: abaabaabaSabaabSbaaSaS  )(GL }*},{:{ bawwwR  Palindromes of even length Another Example
  • 28. Costas Busch - LSU 28 || SSaSbS  ababSaSbSSSS  Context-free grammar :G Example derivations: abababaSbabSaSbSSSS  }prefixanyin )()(and ),()(:{ v vnvn wnwnw ba ba  )(GL () ((( ))) (( )) Describes matched parentheses: )b(, a Another Example
  • 29. Costas Busch - LSU 29 Derivation Order and Derivation Trees
  • 30. Costas Busch - LSU 30 Derivation Order Consider the following example grammar with 5 productions: ABS .1   A aaAA .3 .2   B BbB .5 .4
  • 31. Costas Busch - LSU 31 aabaaBbaaBaaABABS 54321  Leftmost derivation order of string aab : At each step, we substitute the leftmost variable ABS .1   A aaAA .3 .2   B BbB .5 .4 Derivation of aab
  • 32. Costas Busch - LSU 32 aabaaAbAbABbABS 32541  Rightmost derivation order of string aab: At each step, we substitute the rightmost variable ABS .1   A aaAA .3 .2   B BbB .5 .4 Derivation of aab
  • 33. Costas Busch - LSU 33 aabaaAbAbABbABS 32541  Rightmost derivation of :aab aabaaBbaaBaaABABS 54321  Leftmost derivation of :aab ABS .1   A aaAA .3 .2   B BbB .5 .4
  • 34. Costas Busch - LSU 34 Derivation Trees Consider the same example grammar: aabaaBbaaABbaaABABS  And a derivation of :aab ABS  |aaAA  |BbB 
  • 35. Costas Busch - LSU 35 ABS  S BA ABS  |aaAA  |BbB  yield AB
  • 36. Costas Busch - LSU 36 aaABABS  a a A S BA ABS  |aaAA  |BbB  yield aaAB
  • 37. Costas Busch - LSU 37 aaABbaaABABS  S BA a a A B b ABS  |aaAA  |BbB  yield aaABb
  • 38. Costas Busch - LSU 38 aaBbaaABbaaABABS  S BA a a A B b  ABS  |aaAA  |BbB  yield aaBbBbaa 
  • 39. Costas Busch - LSU 39 aabaaBbaaABbaaABABS  yield aabbaa  S BA a a A B b   Derivation Tree ABS  |aaAA  |BbB  (parse tree)
  • 40. Costas Busch - LSU 40 aabaaBbaaBaaABABS  aabaaAbAbABbABS  TRY IT OUT! Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation:
  • 41. Costas Busch - LSU 41 aabaaBbaaBaaABABS  aabaaAbAbABbABS  S BA a a A B b   Give same derivation tree Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation:
  • 42. Costas Busch - LSU 42 Ambiguity
  • 43. Costas Busch - LSU 43 Grammar for mathematical expressions ))(()( aaaaaaa  Example strings: Denotes any number aEEEEEE |)(|| 
  • 44. Costas Busch - LSU 44 A leftmost derivation for aaa  aaaEaa EEaEaEEE *  aEEEEEE |)(||  PARSE TREE ???
  • 45. Costas Busch - LSU 45 A leftmost derivation for aaa  E EE EE  a a a  aaaEaa EEaEaEEE *  aEEEEEE |)(|| 
  • 46. Costas Busch - LSU 46 aaaEaa EEaEEEEEE   Another leftmost derivation for aEEEEEE |)(||  aaa  PARSE TREE ???
  • 47. Costas Busch - LSU 47 E EE  a a  EE a aaaEaa EEaEEEEEE   Another leftmost derivation for aEEEEEE |)(||  aaa 
  • 48. Costas Busch - LSU 48 aaa  E EE  a a  EE a E EE EE  a a a  Two derivation trees for aEEEEEE |)(|| 
  • 49. Costas Busch - LSU 49 E EE   EE E EE EE  2 2 2 2 2 2 222  aaa take 2a
  • 50. Costas Busch - LSU 50 E EE   EE E EE EE   6222  2 2 2 2 2 2 8222  4 2 2 2 6 2 2 24 8 Good Tree Bad Tree Compute expression result using the tree
  • 51. Costas Busch - LSU 51 Two different derivation trees may cause problems in applications which use the derivation trees: • Evaluating expressions • In general, in compilers for programming languages
  • 52. Costas Busch - LSU 52 Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations G )(GLw (Two different derivation trees give two different leftmost derivations and vice-versa)
  • 53. Costas Busch - LSU 53 E EE  a a  EE a E EE EE  a a a  string aaa  has two derivation trees aEEEEEE |)(||  this grammar is ambiguous since Example:
  • 54. Costas Busch - LSU 54 string aaa  has two leftmost derivations aaaEaa EEaEEEEEE   aaaEaa EEaEaEEE *  aEEEEEE |)(||  this grammar is ambiguous also because
  • 55. Costas Busch - LSU 55 IF_STMT if EXPR then STMT | if EXPR then STMT else STMT Another ambiguous grammar: Variables Terminals Very common piece of grammar in programming languages
  • 56. Costas Busch - LSU 56 If expr1 then if expr2 then stmt1 else stmt2 TWO Parse Trees or Derivation Trees ???
  • 57. Costas Busch - LSU 57 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1 then elseif expr2 then STMT stmt1 if IF_STMT expr1 then else if expr2 then STMT stmt2if stmt1 stmt2 Two derivation trees
  • 58. Costas Busch - LSU 58 In general, ambiguity is bad and we want to remove it Sometimes it is possible to find a non-ambiguous grammar for a language But, in general ιt is difficult to achieve this
  • 59. Costas Busch - LSU 59 aE EE EEE EEE     )( aEF FFTT TTEE |)( | |    Ambiguous Grammar Non-Ambiguous Grammar Equivalent generates the same language A successful example:
  • 60. Costas Busch - LSU 60 aaaFaaFFa FTaTaTFTTTEE   E E T T  F F a T F a a aEF FFTT TTEE |)( | |    Unique derivation tree for aaa 
  • 61. Costas Busch - LSU 61 }{}{ mmnmnn cbacbaL  0, mn An un-successful example: every grammar that generates this language is ambiguous L is inherently ambiguous:
  • 62. Costas Busch - LSU 62 }{}{ mmnmnn cbacbaL  | |11 aAbA AcSS   | |22 bBcB BaSS  21 | SSS  Example (ambiguous) grammar for :L
  • 63. Costas Busch - LSU 63 The string Lcba nnn  has always two different derivation trees (for any grammar) S 1S S 2S 1S c 2Sa For example